【问题标题】:Updated State of JDialog not reflected after dispose()在 dispose() 后未反映 JDialog 的更新状态
【发布时间】:2018-11-09 23:00:59
【问题描述】:

我创建了一个自定义 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();
    }                
}

}

【问题讨论】:

  • I am not sure what I am doing wrong here - 我们也不知道,因为我们不知道您的代码实际上在做什么。发布一个正确的minimal reproducible example 来证明问题。因此,您只需要一个带有按钮的简单 JFrame。当您单击该按钮时,将显示一个模式对话框。该对话框应包含一个组件,以便您可以更改其属性。然后,当您关闭对话框时,框架应显示该属性的值。所以重点是首先证明您可以更改单个属性。一旦成功,您就可以添加更改更多属性的能力。在测试新概念时保持简单。
  • 登录按钮侦听器将成功状态从 false 更改为 true,然后调用 dispose 以便 FileOpen Action 侦听器可以读取其状态。它应该是真的,但它总是回来是假的。
  • 请阅读或重新阅读mcve 链接。您的代码既不编译也不运行为我们,这意味着我们无法对其进行测试,也无法直接体验您遇到的问题。此外,您发布的代码比重现问题以及编译和运行所需的代码多得多,这意味着我们必须通过大量不相关的代码来找出发生了什么。请帮助我们发布有效的 MCVE。

标签: java swing jframe jdialog


【解决方案1】:

LogInDialoginit 方法中,在调用setVisible 之后将_success 设置为false...

this.setVisible(true);  

this._success = false;

这意味着对话框关闭后,_success 将设置为false:/

【讨论】:

  • 谢谢。很难相信事情就这么简单。
  • @NeilPittman hard to believe it was that simple - 如果你按照建议创建了一个正确的minimal reproducible example,你可能已经注意到了这个问题,或者甚至没有将该声明包含在“MCVE”中,因为“ MCVE”是为了简化问题并删除不相关的代码,以便您可以准确地看到自己在做什么。这是一个有价值的问题解决工具。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 2016-02-02
  • 1970-01-01
  • 2019-05-03
  • 2021-03-26
  • 2021-04-23
相关资源
最近更新 更多