我想停止意外关闭我的项目。我使用 JFrame 窗体作为主页。当我单击主窗口的关闭按钮时,我将 Exit cord 置于 Yes Option 中。我想在单击无选项时停止关闭。有什么办法吗。这是我的绳索。我正在使用 netbeans 7.3
private void formWindowClosing(java.awt.event.WindowEvent evt) {
int i= JOptionPane.showConfirmDialog(null, "Are you sure to exit?");
if (i == JOptionPane.YES_OPTION) {
System.exit(0);
} else{
new Home().setVisible(true);
}
}
最佳答案
怎么样
class MyGUI extends JFrame {
public MyGUI() {
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);// <- don't close window
// when [X] is pressed
final MyGUI gui = this;
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int i = JOptionPane.showConfirmDialog(gui,
"Are you sure to exit?", "Closing dialog",
JOptionPane.YES_NO_OPTION);
if (i == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
setSize(200, 200);
setVisible(true);
}
//demo
public static void main(String[] args) {
new MyGUI();
}
}
关于java - 如何停止关闭 JFrame 窗体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19301510/