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:
57:
58:
59:
60:
61:
62:
63:
64:
|
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class Test {
private static JTextField jTFlog, jTFpass;
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(0,1));
frame.add(panel);
//Add the ubiquitous "Hello World" label.
JLabel label = new JLabel("Hello World");
panel.add(label);
jTFlog = new JTextField();
panel.add(jTFlog);
jTFpass = new JTextField();
panel.add(jTFpass);
JButton loginButton = new JButton("OK");
loginButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
boolean correctLogin = false;
String login = jTFlog.getText();
String pass = jTFpass.getText();
if (!(login.equals("You") && pass.equals("Meow")))
{
correctLogin = true;
JOptionPane.showMessageDialog(null, " Please enter your Username and Password! ");
}
else {
JOptionPane.showMessageDialog(null, " Username and Password is correct! ");
}
}
}
);
panel.add(loginButton);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
} |