-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_password_field.java
More file actions
56 lines (46 loc) · 1.44 KB
/
04_password_field.java
File metadata and controls
56 lines (46 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import javax.swing.*;
import java.awt.event.*;
public class GUIApp {
public static void main(String[] args) {
JFrame frame = new JFrame("Cupertinii Swing GUI");
frame.setSize(1280,800);
frame.setLayout(null);
JLabel label = new JLabel();
label.setBounds(200, 200, 200, 50);
JPasswordField pwd = new JPasswordField("Input your password");
pwd.setBounds(200, 100, 200, 50);
JButton btn = new JButton("Login");
btn.setBounds(200, 150, 200, 150);
// This function will handle action like pressing enter in text field.
pwd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Call login function
loginFunction(pwd, label, frame);
}
});
// Btn click
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Call login function
loginFunction(pwd, label, frame);
}
});
// add label and txt field to app frame.
frame.add(pwd);
frame.add(btn);
frame.add(label);
frame.setVisible(true);
}
public static void loginFunction(JTextField txt, JLabel label, JFrame frame) {
String textFieldData = txt.getText();
System.out.println(textFieldData);
String stored_password = "123456";
if( textFieldData.equals(stored_password) ) {
label.setText("Login successful");
}
else {
label.setText("Login unsuccessful");
}
return;
}
}