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

Java GUI登录系统在单击“确定”按钮时没有反应,java,swing,Java,Swing,希望你今晚没事 我的GUI登录系统有问题。我能够用标签和字段创建框架来输入用户名和密码,但是当我点击OK按钮时,它不会对我的动作做出反应,也不会改变我喜欢的颜色。 你能看一下吗 public class LoginPanel extends JPanel implements ActionListener { JFrame frame; // frame static JTex

希望你今晚没事

我的GUI登录系统有问题。我能够用标签和字段创建框架来输入用户名和密码,但是当我点击OK按钮时,它不会对我的动作做出反应,也不会改变我喜欢的颜色。 你能看一下吗

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
}
试着这样做

@覆盖
已执行的公共无效操作(操作事件e){
对象源=e.getSource();
if(新字符串(passwordField.getPassword()).equals(“1234”)&&(userField.getText())
.equals(“tomek”)){
frame.getContentPane().setBackground(颜色为.GREEN);
}否则{
frame.getContentPane().setBackground(颜色为.RED);
}
}
  • 您应该在
    JFrame
    ContentPane
    上调用setBackground,而不是在实际面板上,因为您没有使用它
  • 您应该将您的
    JTextField
    JPasswordField
    内容(而不是组件)与用户名
    tomek
    和密码
    1234

您是否尝试将SOP保留在
actionPerformed
方法中以了解是否调用了它?我希望这个
passwordField.equals(“1234”)
应该类似于
passwordField.getText().equals(“1234”)