JButton 侦听器未触发

JButton listener not firing

我将我的代码分离到 MVC 模型中,现在我的确认按钮动作侦听器不打印用户名和密码,即使我为它添加了一个动作侦听器。请帮忙谢谢。

代码

LoginDialog.java

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class LoginDialog extends JDialog {

    private JLabel nameLabel;
    private JLabel passwordLabel;
    private JTextField usernameTF;
    private JPasswordField passwordTF;
    private JButton confirmBtn;
    private JButton cancelBtn;
    private JPanel topPanel;
    private JPanel buttonPanel;
    private GridBagConstraints gbc;

    public LoginDialog() {
        this.setTitle("Authentication");

        topPanel = new JPanel(new GridBagLayout());
        buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        nameLabel = new JLabel("Name : ");
        passwordLabel = new JLabel("Password : ");
        usernameTF = new JTextField();
        passwordTF = new JPasswordField();
        confirmBtn = new JButton("Confirm");
        cancelBtn = new JButton("Cancel");
        buttonPanel.add(confirmBtn);
        buttonPanel.add(cancelBtn);
        gbc = new GridBagConstraints();

        gbc.insets = new Insets(4, 4, 4, 4);

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0;
        topPanel.add(nameLabel, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1;
        topPanel.add(usernameTF, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0;
        topPanel.add(passwordLabel, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.weightx = 1;
        topPanel.add(passwordTF, gbc);

        this.add(topPanel);
        this.add(buttonPanel, BorderLayout.SOUTH);
    }

    public void showLoginDialog() {
        LoginDialog ld = new LoginDialog();
        ld.setSize(400, 150);
        ld.setVisible(true);
        ld.setLocationRelativeTo(null);
    }

    public String getUsername() {
        return usernameTF.getText();
    }

    public String getPassword() {
        return new String(passwordTF.getPassword());
    }

    public void confirmBtnListner(ActionListener listener) {
        confirmBtn.addActionListener(listener);
    }
}

Controller.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Controller {

    private LoginDialog loginDialog;

    public Controller(LoginDialog loginDialog) {
        this.loginDialog = loginDialog;
        loginDialog.showLoginDialog();
        loginDialog.confirmBtnListner(new BtnListener());
    }

    class BtnListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(loginDialog.getUsername());
            System.out.println(loginDialog.getPassword());
        }
    }

    public static void main(String[] args) {
        LoginDialog loginDialog = new LoginDialog();
        new Controller(loginDialog);
    }
}

方法 showLoginDialog() 创建对话框的一个新实例,它没有 actionlistener

修复:不要创建新实例 - 使用已有的实例。

    public void showLoginDialog() {
        setSize(400, 150);
        setVisible(true);
        setLocationRelativeTo(null);
    }

您有两个 LoginDialog class 实例。

一个是您在控制器中创建的,另一个是在您的 LoginDialog#showLoginDialog() 方法中创建的。

让我们列出它们:

1st instance - In the controller class named `loginDialog`
2nd instance - In the `LoginDialog` class named `ld`  

这里ld是在loginDialog对象中创建的对象。但它们是两个不同的 JDialog 对象。当你打电话给

loginDialog.showLoginDialog()  

另一个对象 ld 从该方法中创建。引用的 JDialog 设置为可见:

ld.setVisible(true)

所以现在,

Object `ld` is visible
And Object `loginDialog` is NOT yet visible as you havent done `loginDialog.setVisible(true)` yet.

现在正在将 ActionListener 添加到 loginDialog 对象中的按钮,该按钮尚不可见。虽然没有 ActionListener 添加到 ld 对象中的 Button。

最终结论:

  1. 对象 ld 可见,但其中的按钮 NO ActionListener
  2. 对象 loginDialog 尚可见。但是其中的按钮有一个 ActionListener
  3. 您单击的按钮是 ld 对象的一部分,该对象具有 NO 关联的动作侦听器。
  4. ActionListener 关联的按钮是 loginDialog 对象的一部分,该对象 可见。

想看看我说的对不对?

只需在您的控制器构造函数中添加这些行:

loginDialog.setVisible(true);
loginDialog.setSize(400, 150);
loginDialog.setLocationRelativeTo(null);  

我不会给你完整的解决方案,因为我们不会在堆栈溢出时在这里提供信息。因此,相应地调整代码对您来说是一个挑战。 :)