这里打开JDialog的java代码:
public class AccountsInternalFrame extends JInternalFrame implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == applyFilterButton) {
} else if (e.getSource() == cleanFilterButton) {
} else if (e.getSource() == paymentAccountButton) {
EditPaymentAccountDialog editPaymentAccountDialog = new EditPaymentAccountDialog(owner);
editPaymentAccountDialog.setVisible(true);
}
}
}
这是我的对话框代码:
public class EditPaymentAccountDialog extends JDialog implements ActionListener {
public EditPaymentAccountDialog(Frame owner) {
super(owner, true);
initialize();
}
private void initialize() {
buildGUI();
initLogic();
setPreferredSize(new Dimension(680, 280));
pack();
setLocationRelativeTo(null); // center
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}
private void buildGUI() {
setTitle(SystemOptions.translate("edit.payment.account"));
Component mainPanel = createMainPanel();
add(mainPanel);
}
}
结果我的对话成功了。 但是当我单击 X(右上角)时,对话框成功隐藏,但再次显示。当我再次单击 X 后,对话框将永远隐藏。
为什么我需要点击两次 X 才能隐藏对话框?
最佳答案
您的对话框是模式对话框。因此它的 setVisible(true)
方法会阻塞,直到您关闭它。并且您指定调用 setVisible(true)
之后关闭时应该发生的情况:
setVisible(true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
顺便说一句,这也会阻止您的 initLogic()
代码被执行。
不要使对话框在构造函数中可见。让对话框的使用者选择何时使其可见。构建一个对话框就应该构建它。不使其可见,并阻止调用代码。
关于java - 第一次单击 X 时 JDialog 未关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47854153/