Java JButton侦听器未启动
我将代码分离到MVC模型中,现在我的确认按钮操作侦听器没有打印用户名和密码,尽管我为其添加了一个Actionlistener。请帮忙,谢谢 代码 LoginDialog.javaJava JButton侦听器未启动,java,swing,button,actionlistener,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
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
类的实例
一个是在控制器中创建的,另一个是在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
refered设置为可见:
ld.setVisible(true)
那么现在,
Object `ld` is visible
And Object `loginDialog` is NOT yet visible as you havent done `loginDialog.setVisible(true)` yet.
现在,我们正在将ActionListener
添加到loginDialog
对象中的按钮中,该按钮尚不可见。而ld
对象中的按钮没有添加ActionListener
最后结论:
ld
可见,但其中的按钮没有ActionListener
李>
loginDialog
尚不可见。但是它里面的按钮有一个ActionListener
李>
ld
对象的一部分,该对象没有关联的操作侦听器李>
ActionListener
的按钮是loginDialog
对象的一部分,该对象不可见李>
想看看我是否正确吗?
只需在控制器构造函数中添加以下行:
loginDialog.setVisible(true);
loginDialog.setSize(400, 150);
loginDialog.setLocationRelativeTo(null);
我不会给你完整的解决方案,因为我们这里不会在堆栈溢出时使用勺子。因此,对您来说,相应地调整代码是一项挑战。:) 为了更快地获得更好的帮助,可以发布一个(最小完整的可验证示例)或(简短、自包含、正确的示例)。在这种情况下,它需要一个源文件,最多包含一个
public
类(+其他访问更受限的类)和一个main(String[])
方法。我有一个main(String[]args)方法,我的帖子已经是MCVE??这不是一个合适的方法MVC@samlee我错过了main
方法。似乎你错过了我关于“一个源文件”的第一点。你在上面的评论中提到我的代码不是一个合适的MVC。那么,我应该如何使它成为一个合适的MVC呢?GUI部分在视图中。模型中包含了业务逻辑。还有控制器,你知道的。这个应用程序对于MVC模式来说非常小。你会把事情复杂化。最好不要在这样的小应用程序中使用MVC。