Ayush Practical No 12
Ayush Practical No 12
X. Program Code.
Code :-
import javax.swing.*;
public class PasswordFieldExample
{
public static void main(String[] args)
{
JFrame F=new JFrame("Password Field Example");
JPasswordField value = new JPasswordField();
value.setEchoChar('#');
JLabel l1=new JLabel(" Password:");
l1.setBounds(20,100, 80,30);
value.setBounds(110,110,110,40);
F.add(value);
F.add(l1);
F.setSize(330,350);
F.setLayout(null);
F.setVisible(true);
}
}
1
Output :-
XIII. Exercise.
1 Write a Program Using JPasswordField And JTextField To Demonstrate The Use
Of User Authetication.
Code :-
import javax.swing.*;
public class PasswordFieldExample1 {
public static void main(String[] args)
{
JFrame F=new JFrame("Password Field Example");
final JLabel label = new JLabel();
label.setBounds(20,150, 200,50);
final JPasswordField value = new JPasswordField();
value.setBounds(100,75,100,30);
JLabel L1=new JLabel("Username:");
L1.setBounds(20,20, 80,30);
JLabel L2=new JLabel("Password:");
L2.setBounds(20,75, 80,30);
JButton B = new JButton("Login");
B.setBounds(100,120, 80,30);
2
final JTextField text = new JTextField();
text.setBounds(100,20, 100,30);
F.add(value);
F.add(L1);
F.add(label);
F.add(L2);
F.add(B);
F.add(text);
F.setSize(300,350);
F.setLayout(null);
F.setVisible(true);
}
}
Output :-
3
2. Write a Program Using JTextField To Perform The Addition Of Two Numbers.
Code :-
import javax.swing.*;
import java.awt.event.*;
JTextField tf1,tf2,tf3;
JButton b1,b2;
TextFieldExample(){
tf1=new JTextField();
tf1.setBounds(50,50,150,20);
tf2=new JTextField();
tf2.setBounds(50,100,150,20);
tf3=new JTextField();
tf3.setBounds(50,150,150,20);
4
tf3.setEditable(false); b1=new
JButton("+");
b1.setBounds(50,200,50,50);
b1.addActionListener(this);
f.add(tf1);f.add(tf2);f.add(tf3);f.add(b1);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
String s1=tf1.getText();
String s2=tf2.getText();
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=0;
if(e.getSource()==b1){
c=a+b;
String result=String.valueOf(c);
tf3.setText(result);
new TextFieldExample();
5
}
Output :-
6
3. Write a Program Using JPasswordField To Accept Password From User And If
The Length Must Be Less Than 6 Character Then Error Message Should Be
Displayed “Password Length Must Be >6 Character”
Code :-
import javax.swing.*;
import java.awt.event.*;
public class PasswordFieldeEx1 {
public static void main(String[] args)
{
JFrame F=new JFrame("Password Field Example");
});
}
}
Output:- :-
8
9