dispose()后未反映JDialog的更新状态

时间:2018-11-09 23:00:59

标签: java swing jframe jdialog

我创建了一个自定义JDialog,该JDialog的内部状态要在JDialog关闭后在父JFrame中查询。我将JDialog的模态设置为“ true”,它将阻塞JFrame直到JDialog被处置。

在JDialog中,我更新状态并使用用户交互来调用dispose。我验证状态已更新。但是,当关闭JDialog后父框架恢复时,查询的状态不是我设置JDialog的状态。我不确定我在做什么错。任何帮助表示赞赏。

这是我的代码的快照:

公共类IndexFrame扩展了JFrame {

private static final long serialVersionUID = 9084040250695639818L;

private JMenu _filemenu;

private JMenuBar _menubar;

public IndexFrame() {
    super("Index");
    init();
}

private boolean init() {

    this._new_filemenu_item = new JMenuItem("New");
    this._open_filemenu_item = new JMenuItem("Open");
    FileMenuOpenActionListener open_filemenu_actionlistener = new FileMenuOpenActionListener(this);
    this._open_filemenu_item.addActionListener(open_filemenu_actionlistener);
    this._close_filemenu_item = new JMenuItem("Close");
    FileMenuCloseActionListener close_filemenu_actionlistener = new FileMenuCloseActionListener(this);
    this._close_filemenu_item.addActionListener(close_filemenu_actionlistener);
    this._exit_filemenu_item = new JMenuItem("Exit");
    CloseButtonActionListener exit_filemenu_actionlistener = new CloseButtonActionListener(this);
    this._exit_filemenu_item.addActionListener(exit_filemenu_actionlistener);

    this._filemenu = new JMenu("File");
    this._filemenu.add(this._new_filemenu_item);
    this._filemenu.add(this._open_filemenu_item);
    this._filemenu.add(this._close_filemenu_item);
    this._filemenu.add(this._exit_filemenu_item);

    this._menubar = new JMenuBar();
    this._menubar.add(this._filemenu);

    this.setJMenuBar(this._menubar);

    this.pack();
    this.setVisible(true);        

    return true;        
}

}

公共类FileMenuOpenActionListener实现ActionListener {

private IndexFrame _frame;

public FileMenuOpenActionListener(IndexFrame frame) {
    this._frame = frame;
}

@Override
public void actionPerformed(ActionEvent arg0) {
    File file;
    JFileChooser fc;
    LogInDialog ld;
    fc = new JFileChooser();
    if (fc.showOpenDialog(this._frame) == JFileChooser.APPROVE_OPTION) {
        file = fc.getSelectedFile();

        ld = new LogInDialog(this._frame);
        if (ld.isSuccess()) {
            this._frame.refresh();
        }
    }
    else {
    }
}

}

公共类LogInDialog扩展了JDialog {

private JFrame _paraentframe;

private JLabel _userlabel;
private JTextField _userfield;

private JPanel _userpanel;

private JLabel _pwdlabel;
private JPasswordField _pwdfield;

private JPanel _pwdpanel;

private JPanel _inputpanel;

private JButton _loginbutton;
private JButton _cancelbutton;

private JPanel _buttonpanel;

private JPanel _dialogpanel;

private boolean _success;

public LogInDialog(JFrame pf) {
    super(pf, "Log In", true);

    this._paraentframe = pf;

    init();
}

private boolean init() {

    this._userlabel = new JLabel("Username");
    this._userfield = new JTextField(20);

    this._userpanel = new JPanel();
    this._userpanel.add(this._userlabel);
    this._userpanel.add(this._userfield);

    this._pwdlabel = new JLabel("Password");
    this._pwdfield = new JPasswordField(20);

    this._pwdpanel = new JPanel();
    this._pwdpanel.add(this._pwdlabel);
    this._pwdpanel.add(this._pwdfield);

    this._inputpanel = new JPanel();
    this._inputpanel.add(this._userpanel);
    this._inputpanel.add(this._pwdpanel);

    this._loginbutton = new JButton("Log In");
    LogInDialogLogInButtonActionListener loginbuttonlistener = new LogInDialogLogInButtonActionListener(this);
    this._loginbutton.addActionListener(loginbuttonlistener);
    this._cancelbutton = new JButton("Cancel");
    CloseButtonActionListener cancelbuttonlistener = new CloseButtonActionListener(this);
    this._cancelbutton.addActionListener(cancelbuttonlistener);

    this._buttonpanel = new JPanel();
    this._buttonpanel.add(this._loginbutton);
    this._buttonpanel.add(this._cancelbutton);

    this._dialogpanel = new JPanel();
    this._dialogpanel.setLayout(new BorderLayout());
    this._dialogpanel.add(this._inputpanel, BorderLayout.CENTER);
    this._dialogpanel.add(this._buttonpanel, BorderLayout.SOUTH);

    this.setContentPane(this._dialogpanel);    

    this.pack();
    this.setVisible(true);  

    this._success = false;

    return true;
}

public boolean isSuccess() {
    return this._success;
}

public void setSuccess(boolean s) {
    this._success = s;
}

public String getUserName() {
    return this._userfield.getText();
}

public String getPassWord() {
    return String.valueOf(this._pwdfield.getPassword());
}

}

公共类LogInDialogLogInButtonActionListener实现ActionListener {

private LogInDialog _dialog;

public LogInDialogLogInButtonActionListener(LogInDialog dialog) {
    this._dialog = dialog;
}

@Override
public void actionPerformed(ActionEvent arg0) {
    String username;
    String password;
    username = this._dialog.getUserName();
    password = this._dialog.getPassWord();
    if (true) {
        this._dialog.setSuccess(true);
        this._dialog.dispose();
    }                
}

}

1 个答案:

答案 0 :(得分:0)

init的{​​{1}}方法中,在调用LogInDialog ...之后,将_success设置为false ...

setVisible

这意味着在对话框关闭后,this.setVisible(true); this._success = false; 将被设置为_success:/