Java – 如何防止WindowClosing实际closures窗口

我似乎对大多数人都有相反的问题。 我有以下相当标准的代码来查看用户是否想在closures窗口之前进行一些保存:

frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent ev) { boolean close = true; // check some files, asking if the user wants to save // YES and NO handle OK, but if the user hits Cancel on any file, // I want to abort the close process // So if any of them hit Cancel, I set "close" to false if (close) { frame.dispose(); System.exit(0); } } }); 

无论我尝试什么,当我离开windowClosing时窗口总是closures。 将WindowAdapter更改为WindowListener没有任何区别。 奇怪的是,文档中明确提到“如果程序在处理这个事件的时候没有明确地隐藏或者处理这个窗口,窗口closures操作将会被取消”,但是这对我来说并不适用。 有没有其他方式来处理框架上的x? TIA

我刚刚试过这个最小的testing用例:

 import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.WindowConstants; public class test { public static void main(String[] args) { final JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent ev) { //frame.dispose(); } }); frame.setVisible(true); } } 

如果我保持dispose调用注释,并点击closuresbutton,窗口不会退出。 取消注释并点击closuresbutton,窗口closures。

我不得不猜测你的逻辑中有什么错误来设置你的“closures”variables。 尝试双重检查。

这是关键,methinks: frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 在我制作的testing用例中有所不同。

不知道你的问题在哪里,

 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ClosingFrame extends JFrame { private JMenuBar MenuBar = new JMenuBar(); private JFrame frame = new JFrame(); private static final long serialVersionUID = 1L; private JMenu File = new JMenu("File"); private JMenuItem Exit = new JMenuItem("Exit"); public ClosingFrame() { File.add(Exit); MenuBar.add(File); Exit.addActionListener(new ExitListener()); WindowListener exitListener = new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { int confirm = JOptionPane.showOptionDialog(frame, "Are You Sure to Close this Application?", "Exit Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null); if (confirm == JOptionPane.YES_OPTION) { System.exit(1); } } }; frame.addWindowListener(exitListener); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setJMenuBar(MenuBar); frame.setPreferredSize(new Dimension(400, 300)); frame.setLocation(100, 100); frame.pack(); frame.setVisible(true); } private class ExitListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { int confirm = JOptionPane.showOptionDialog(frame, "Are You Sure to Close this Application?", "Exit Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null); if (confirm == JOptionPane.YES_OPTION) { System.exit(1); } } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ClosingFrame cf = new ClosingFrame(); } }); } } 

对于这个事情的处理呢:
如果用户select是,则使用setDefaultCloseOperation(DISPOSE_ON_CLOSE); if else的花括号内

如果select取消,则使用setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

考虑一下例子:

 int safe = JOptionPane.showConfirmDialog(null, "titleDetails!", "title!!", JOptionPane.YES_NO_CANCEL_OPTION); if(safe == JOptionPane.YES_OPTION){ setDefaultCloseOperation(DISPOSE_ON_CLOSE);//yes } else if (safe == JOptionPane.CANCEL_OPTION) { setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);//cancel } else { setDefaultCloseOperation(DISPOSE_ON_CLOSE);//no } 

不知道你的问题在哪里,但这对我有用!

frame.addWindowListener(new WindowAdapter(){

  public void windowClosing(WindowEvent evt){ int res=JOptionPane.showConfirmDialog(null, "Do you want to exit.?"); if(res==JOptionPane.YES_OPTION){ Cal.this.dispose(); } } }); 

为了解决同样的问题,我尝试了这篇文章的第一个答案。 作为单独的应用程序,但不是在我的情况。 也许差异是在JFrame(在答案)和FrameView(我的情况)。

 public class MyApp extends SingleFrameApplication { // application class of my project ... protected static MyView mainForm; // main form of application ... } public class MyView extends FrameView { ... //Adding this listener solves the problem. MyApp.getInstance().addExitListener(new ExitListener() { @Override public boolean canExit(EventObject event) { boolean res = false; int reply = JOptionPane.showConfirmDialog(null, "Are You sure?", "", JOptionPane.YES_NO_OPTION); if (reply == JOptionPane.YES_OPTION) { res = true; } return res; } @Override public void willExit(EventObject event) { } }); ... } 

setDefaultCloseOperation()方法有助于解决问题。 https://chortle.ccsu.edu/java5/Notes/chap56/ch56_9.html

查看此链接