有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

单击“确定”按钮时,java GUI登录系统不会做出反应

希望你今晚没事

我的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
}

共 (0) 个答案