这里打开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才能隐藏对话框?
答案 0 :(得分:1)
您的对话框是一个模态对话框。所以它的setVisible(true)
方法会阻塞,直到你关闭它。并指定在对setVisible(true)
的调用后关闭时会发生什么。
setVisible(true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
顺便说一下,这也会阻止您执行initLogic()
代码。
不要从构造函数中看到对话框。让对话框的使用选择何时使其可见。构造一个对话框应该构建它。不要让它可见,并阻止调用代码。