我想添加一个退出确认支票的JFrame,但我想对话是未装饰。 我已经想通,我需要使用自定义的JDialog和定制的JOptionPane。
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
JDialog dialog = new JDialog();
dialog.setUndecorated(true);
JOptionPane pane = new JOptionPane("Are you sure that you want to exit?",
JOptionPane.QUESTION_MESSAGE,JOptionPane.YES_NO_OPTION);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); //I don't even know if this line does anything
dialog.setContentPane(pane);
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
System.out.println("Next Line"); //this line does not work.
}
});
出现的对话框正是我想要的,但点击yes或no什么都不做。 对话框不消失,我无法找到一个方法来检查被点击了哪个按钮。 “下一行”从不打印到控制台。