java windowclosing_Java – 如何防止WindowClosing实际closures窗口

我刚刚试过这个最小的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

查看此链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值