1import javax.swing.JFrame;
2import javax.swing.JLabel;
3import javax.swing.JButton;
4import javax.swing.JPasswordField;
5import javax.swing.JTextField;
6import javax.swing.JOptionPane;
7import java.awt.FlowLayout;
8import java.awt.event.ActionListener;
9import java.awt.event.ActionEvent;
10
11class Frame extends JFrame implements ActionListener{
12
13 private JPasswordField jpf;
14 private JTextField jtf;
15 private JButton jb;
16 private String name = "",pass = "";
17 private char getpass[];
18
19 Frame()
20 {
21 setTitle("JPasswordField with ActionListener");
22 setLayout(new FlowLayout());
23 setJTextFieldandJPasswordField();
24 setAction();
25 setSize(700, 200);
26 setVisible(true);
27 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
28 }
29
30 private void setJTextFieldandJPasswordField()
31 {
32 JLabel username = new JLabel("User name");
33 jtf = new JTextField("User name",10);
34 JLabel password = new JLabel("Password");
35 jpf = new JPasswordField(10);
36 jb = new JButton("Enter");
37 JLabel nameAndPass = new JLabel("User name : HAJSOF Password : 123456");
38 add(username);
39 add(jtf);
40 add(password);
41 add(jpf);
42 add(jb);
43 add(nameAndPass);
44 }
45
46 private void setAction()
47 {
48 jb.addActionListener(this);
49 }
50
51 public void actionPerformed(ActionEvent eve)
52 {
53 name = jtf.getText();
54 getpass = jpf.getPassword();
55 pass = String.valueOf(getpass);
56
57 if(name.equals("HAJSOF") && pass.equals("123456"))
58 JOptionPane.showMessageDialog(rootPane, "Login Successful");
59 else
60 JOptionPane.showMessageDialog(rootPane, "Username or Password incorrect");
61 }
62}
63
64public class Javaapp {
65
66 public static void main(String[] args) {
67
68 Frame frame = new Frame();
69 }
70}