Java Swing ActionListener无法正常工作并停止构造函数

时间:2017-05-05 09:39:13

标签: java swing actionlistener

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

当我在控制器类中注册ActionListener时,它不起作用。正如调试似乎显示的那样,构造函数在调用JDialog的构造函数后甚至都没有finsish。

由于描述没有太多内容,这是我的课程:

这是视图:

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

这是负责视图的控制器:

   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 =父行,则程序仅在关闭作为AuthenticationWindow的JDialoge时停止。

有趣的是我还有另外两个以这种方式工作的课程。如果需要,我会在这里发布。请告诉我。

谢谢你的帮助!

0 个答案:

没有答案