packagecom.test.jframe;importjava.awt.Color;importjava.awt.EventQueue;importjava.awt.Label;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JFormattedTextField;importjavax.swing.JFrame;importjavax.swing.JOptionPane;importjavax.swing.JPasswordField;public classJFrameTest {privateJFrame frame;privateJPasswordField passwordField;private boolean isLogin = false;/*** Launch the application.*/
public static voidmain(String[] args) {
EventQueue.invokeLater(newRunnable() {public voidrun() {try{
JFrameTest window= newJFrameTest();
window.frame.setVisible(true);
}catch(Exception e) {
e.printStackTrace();
}
}
});
}/*** Create the application.*/
publicJFrameTest() {
initialize();
}/*** Initialize the contents of the frame.*/
private voidinitialize() {
String userName= "111";
String userPwd= "111";
frame= newJFrame();
frame.setBounds(100, 100, 667, 453);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
Label label= new Label("账号:");
label.setAlignment(Label.CENTER);
label.setBounds(116, 49, 50, 23);
frame.getContentPane().add(label);
Label label_1= new Label("密码:");
label_1.setAlignment(Label.CENTER);
label_1.setBounds(116, 85, 50, 23);
frame.getContentPane().add(label_1);
Label label_2= new Label("用户状态:");
label_2.setBounds(433, 49, 60, 23);
frame.getContentPane().add(label_2);
Label label_3= new Label("未登录");
label_3.setForeground(new Color(255, 0, 0));
label_3.setBounds(499, 49, 40, 23);
frame.getContentPane().add(label_3);
JFormattedTextField formattedTextField= newJFormattedTextField();
formattedTextField.setBounds(172, 49, 166, 23);
frame.getContentPane().add(formattedTextField);
passwordField= newJPasswordField();
passwordField.setBounds(172, 85, 166, 23);
frame.getContentPane().add(passwordField);
JButton button= new JButton("login");
button.setBackground(new Color(255, 255, 255));
button.setBounds(126, 121, 212, 23);
frame.getContentPane().add(button);
button.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
String getUserName=formattedTextField.getText();
String getUserPwd=passwordField.getText();if (userName.equals(getUserName) &&userPwd.equals(getUserPwd)) {
isLogin= true;
}else{
isLogin= false;
}if(isLogin) {
JOptionPane.showMessageDialog(null, "登录成功!", "消息", JOptionPane.PLAIN_MESSAGE);
label_3.setText("已登录");
label_3.setForeground(Color.BLUE);
}else{
JOptionPane.showMessageDialog(null, "登录失败!", "消息", JOptionPane.WARNING_MESSAGE);
label_3.setText("未登录");
label_3.setForeground(Color.RED);
}
}
});
}
}