JButton听众没有解雇

时间:2015-06-15 07:09:48

标签: java swing button actionlistener

我将我的代码分成MVC模型,现在我的确认按钮动作监听器没有打印用户名和密码,即使我为它添加了一个Actionlistener。请帮助谢谢。

代码

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);
    }
}

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:2)

您有两个LoginDialog级实例。

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

让我们列出它们:

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

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

loginDialog.showLoginDialog()  

从方法中创建另一个对象ldJDialog引用设置为可见:

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

最终结论:

  1. 对象ld可见,但其中的按钮 ActionListener
  2. 对象loginDialog NOT 尚可见。但其中的按钮有一个ActionListener
  3. 您单击的按钮是ld对象的一部分,该对象具有与之关联的 NO 动作侦听器。
  4. ActionListener相关联的按钮是loginDialog对象的一部分, NOT 可见。
  5. 想检查一下我是否正确?

    只需在控制器构造函数中添加以下行:

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

    我不会给你完整的解决方案,因为我们不会在堆栈溢出时把勺子送到这里。因此,您需要相应地调整代码。 :)