单击“确定”按钮时,GUI登录系统没有反应

时间:2019-02-01 18:37:55

标签: java swing

希望你今晚没事。

我的GUI登录系统有问题。我能够创建带有标签和字段的框架来输入用户名和密码,但是,当我单击“确定”按钮时,它不会对我的操作做出反应,也不会更改我喜欢的颜色。 你能看看吗?

public class LoginPanel  extends JPanel implements ActionListener {

    JFrame frame;                                                   // frame
    static JTextField userField;                                    // field to get user name
    JLabel userLabel;                                               // using for printing label on frame
    static JPasswordField passwordField;                            // field where you put your passowrd
    JButton loginButton;                                            // add OK button below login

    public LoginPanel () {
        super();
        frame = new JFrame ("Login");                               // initial frame, add title
        frame.setSize(500, 500);                                    // frame size
        frame.setLocation(300, 200);                                // set where program window should start
        frame.setLayout(null);                                      // set layout; you can use (new FlowLayout (FlowLayout.LEFT));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       // closing the program by clicking X

        userLabel = new JLabel("enter user name label");            // create Label next to the user field
        userLabel.setLocation(10, 10);                              // set location where label will start to appear
        userLabel.setSize (userLabel.getPreferredSize());           //
        frame.add(userLabel);                                       // add userLabel to the frame

        userField = new JTextField ();                              // initial text field for user name
        userField.setColumns(25);
        userField.setSize(userField.getPreferredSize());            // set text field size                          // 
        userField.setLocation(150, 10);                             // set where text field will apear on frame;
        userField.setToolTipText("enter user name");                // when you move the mouse on the field, you will get a hint
        frame.add(userField);                                       // add userfield to the frame

        userLabel = new JLabel("enter password label");             // create Label next to the password field
        userLabel.setLocation(10, 40);                              // set location where label will start to appear
        userLabel.setSize (userLabel.getPreferredSize());           //
        frame.add(userLabel);                                       // add label to the frame

        passwordField = new JPasswordField ();                      // add password field next to the label
        passwordField.setColumns(25);                               // 
        passwordField.setSize(userField.getPreferredSize());        // set text field size          
        passwordField.setLocation(150, 40);                         // set location where password field will apear
        passwordField.setToolTipText("enter password");             // when you move the mouse on the field, you will get a hint
        frame.add(passwordField);                                   // add password field to the frame

        loginButton = new JButton("OK");                            // add OK button
        loginButton.setSize(loginButton.getPreferredSize());        // 
        loginButton.setLocation(150, 80);                           // set where ok button appears
        loginButton.addActionListener(this);                        // add action listener to the button when click to the button then method actionPerformed
        frame.add(loginButton);                                     // add button to the frame

        frame.setVisible(true);                                     // frame visibility; ALWAYS at the end, because will not show entire content of frame


    }                                                               // end of Login Panel code

    @Override
    public void actionPerformed(ActionEvent e) {
                Object source = e.getSource();
        if(passwordField.equals("1234") && (userField.equals("tomek"))) {
setBackground(Color.GREEN);
}
    else {
        setBackground(Color.RED);
    }

    }   
    public static void main (String [] args) {                      // adding at the end as every program need to have main method
        new LoginPanel();                                           // run method LoginPanel
}

1 个答案:

答案 0 :(得分:1)

尝试这样做

 @Override
  public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();

    if (new String(passwordField.getPassword()).equals("1234") && (userField.getText()
        .equals("tomek"))) {
      frame.getContentPane().setBackground(Color.GREEN);
    } else {
      frame.getContentPane().setBackground(Color.RED);
    }
  }
  • 您应该在ContentPane的{​​{1}}的{​​{1}}上调用setBackground而不是在实际面板上,因为您没有使用它
  • 您应该将JFrameJTextField的内容(而不是组成部分)与用户名JPasswordField和密码tomek进行比较