gpt4 book ai didi

Java Swing ActionListener 不工作并停止构造函数

转载 作者:行者123 更新时间:2023-11-30 07:54:44 25 4
gpt4 key购买 nike

我正在使用(或试图)使用 MVC 设计模式。所以我有一个只关注自己业务的 UI 类和一个控制 View 的 Controller 。

当我在 Controller 类中注册一个 ActionListener 时,它不起作用。正如调试显示的那样,构造函数在调用 JDialog 的构造函数后甚至没有完成。

由于描述没有多大意义,这是我的类(class):

这是 View :

    package view;

import javax.swing.*;
import java.awt.*;

/**
* Created by Timbo on 04.05.17.
* This window will be used to authenticate library members and allow them to access staff functions
*/
public class AuthenticationWindow
{
/**
* The window that holds the authentication content
*/
private JDialog _window;

/**
* This Panel holds the fields required for the input of username and password
*/
private JPanel _textFieldPanel;

/**
* This panel will hold the buttons
*/
private JPanel _buttonPanel;

/**
* The textfield in which the username can be entered
*/
private JTextField _userName;

/**
* The field, in which the password can be entered
*/
private JPasswordField _password;

/**
* Holds a string to hint for the username
*/
private JLabel _usernameHint;

/**
* Holds a string to hint for the password
*/
private JLabel _passwordHint;

/**
* A Button that will trigger the login process
*/
private JButton _confirm;

/**
* A Button that will cancel the login process
*/
private JButton _cancel;


/**
* Creates a new Authentication Window in which a user can authenticate to get staff clearance
* @param parent The parent frame of this Window
*/
public AuthenticationWindow(Frame parent)
{
//Initialize all the components
_window = new JDialog(parent, "Please Authenticate", true);
_textFieldPanel = new JPanel(new GridLayout(2, 2));
_buttonPanel = new JPanel(new FlowLayout());
_userName = new JTextField();
_password = new JPasswordField();
_passwordHint = new JLabel("Password");
_usernameHint = new JLabel("Username");
_confirm = new JButton("Confirm");
_cancel = new JButton("Cancel");

//Assemble the textfield panel
_textFieldPanel.add(_usernameHint);
_textFieldPanel.add(_userName);
_textFieldPanel.add(_passwordHint);
_textFieldPanel.add(_password);

//Assemble the button panel
_buttonPanel.add(_cancel);
_buttonPanel.add(_confirm);

//Assemble the window
_window.setLayout(new BoxLayout(_window.getContentPane(), BoxLayout.PAGE_AXIS));
_window.add(_textFieldPanel);
_window.add(_buttonPanel);

//Configure the window
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
_window.setLocation(dim.width/2-_window.getSize().width/2, dim.height/2-_window.getSize().height/2);
_window.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
_window.pack();

//Show window
_window.setVisible(true);
}

//All the getter methods for this class
public JDialog getWindow()
{
return _window;
}

public JTextField getUsernameInput()
{
return _userName;
}

public JPasswordField getPasswordInput()
{
return _password;
}

public JButton getConfirm()
{
return _confirm;
}

public JButton getCancel()
{
return _cancel;
}
}

这是负责 View 的 Controller :

   package controller.start.viewController;

import view.AuthenticationWindow;
import view.ErrorScreen;

import javax.swing.*;
import java.awt.*;
import java.util.Observable;

/**
* Created by Timbo on 05.05.17.
* This is the controller class for the Authentication window. It handles updates and functionality for the view
*/
public class AuthenticationWindowController extends Observable
{
private static final char[] PASSWORD = {'T', 'o', 'r', 'v', 'a', 'l', 'd', 's'};
/**
* The view that holds the components
*/
private AuthenticationWindow _ui;

/**
* The parent frame of this view
*/
private Frame _parent;

public AuthenticationWindowController(Frame parent)
{
_ui = new AuthenticationWindow(parent);
_parent = parent;
registerListener();
}

private void registerListener()
{
_ui.getConfirm().addActionListener(e -> {checkInput();});
_ui.getCancel().addActionListener(e -> {_ui.getWindow().dispose(); System.out.println("Window disposed");});
}

/**
* This method checks the input and gives feedback to the user in case the input was wrong
* If the input was correct the user is being authenticated as staff and can access the staff view in the start window
*/
private void checkInput()
{
String username = _ui.getUsernameInput().getText();
char[] password = _ui.getPasswordInput().getPassword();

if (username.isEmpty())
{
new ErrorScreenController(_parent, ErrorScreen.USERNAME_ERROR);
}
else if (password.length == 0)
{
new ErrorScreenController(_parent, ErrorScreen.PASSWORD_ERROR);
}
else if ((username.equalsIgnoreCase("linus")) || (password.equals(PASSWORD)))
{
this.setChanged();
this.notifyObservers();
}
else
{
new ErrorScreenController(_parent, ErrorScreen.WRONG_CREDENTIALS);
}
}
}

在构造函数中,actionListeners 只是不触发。如果我在 _parent = parent 行设置断点,程序只会在我关闭作为 AuthenticationWindow 的 JDialoge 时停止。

有趣的是,我还有另外两个类以这种方式工作。如果需要,我会把它们张贴在这里。请告诉我。

谢谢你的帮助!

最佳答案

您的问题是模态对话框的显示“停止”了当前线程,直到对话框关闭。为避免此问题,您应该按照以下方式重新设计您的应用程序:

1) 从 AuthenticationWindow

的构造函数中删除行 _window.setVisible(true);

2) 在 AuthenticationWindow 中创建一个名为 show 的新方法

public void show() {
_window.setVisible(true);
}

3) 更改AuthenticationWindowController的构造函数

public AuthenticationWindowController(Frame parent)
{
_ui = new AuthenticationWindow(parent);
_parent = parent;
registerListener();
_ui.show();
}

一些进一步的提示:

首先喜欢Action到 Action 监听器。次要:您的 Controller 不应与 GUI 小部件一起使用。因此,最好为您的 UI 提供一些高级方法,例如 setValidator() 以将您的验证功能从 Controller 转移到 UI(在这种情况下,您可以直接在 UI 中创建和注册操作)。还要创建访问密码和登录的方法(而不是输入它们的字段)。

关于Java Swing ActionListener 不工作并停止构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43801581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com