2012-02-12 53 views
6

在我的一个程序中,我想在用户尝试退出应用程序时出现对话框。用户必须选择保存程序的某个状态,而不是保存或取消退出操作。Java:如何取消应用程序退出

我试图第一和ONY找到一个解决办法写了这个,然后实现它:

import javax.swing.*; 
import java.awt.Dimension; 
import java.awt.event.*; 

class WL implements WindowListener 
{ 
    private boolean statussaved; 
    private JFrame tframe; 

    WL (JFrame frame) 
    { 
     statussaved = false; 
     tframe = frame; 
    } 

    @Override public void windowActivated (WindowEvent w) { } 
    @Override public void windowClosed (WindowEvent w) { } 
    @Override public void windowDeactivated (WindowEvent w) { } 
    @Override public void windowDeiconified (WindowEvent w) { } 
    @Override public void windowIconified (WindowEvent w) { } 
    @Override public void windowOpened (WindowEvent w) { } 

    @Override public void windowClosing (WindowEvent w) 
    { 
     if (statussaved) 
     { 
      return; 
     } 

     final JDialog diag = new JDialog (tframe, "Save Progress", true); 
     diag.setPreferredSize (new Dimension (500, 100)); 
     diag.setResizable (false); 
     diag.setDefaultCloseOperation (JDialog.DISPOSE_ON_CLOSE); 

     JPanel notifypanel = new JPanel(); 
     notifypanel.add (new JLabel ("Do you want to save the current status ?")); 

     JButton yesbutton = new JButton ("Yes"); 
     JButton nobutton = new JButton ("No"); 
     JButton cancelbutton = new JButton ("Cancel"); 

     yesbutton.addActionListener (new ActionListener() 
     { 
      @Override public void actionPerformed (ActionEvent a) 
      { 
       //SAVE THE STATUS 

       System.out.println ("Saving status..."); 
       statussaved = true; 

       diag.dispose(); 
       tframe.dispose(); 
      } 
     }); 

     nobutton.addActionListener (new ActionListener() 
     { 
      @Override public void actionPerformed (ActionEvent a) 
      { 
       //just exit/close the application 

       diag.dispose(); 
       tframe.dispose(); 
      } 
     }); 

     cancelbutton.addActionListener (new ActionListener() 
     { 
      @Override public void actionPerformed (ActionEvent a) 
      { 
       //DON'T EXIT !!! 

       diag.dispose(); 
      } 
     }); 

     notifypanel.add (yesbutton); 
     notifypanel.add (nobutton); 
     notifypanel.add (cancelbutton); 

     diag.setContentPane (notifypanel); 

     diag.pack(); 
     diag.setVisible (true); 
    } 
} 

public class SaveTest 
{ 
    public static void main (String[] args) 
    { 
     SwingUtilities.invokeLater (new Runnable() 
     { 
      @Override public void run() 
      { 
       JFrame frame = new JFrame ("Save Test"); 
       frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE); 
       frame.addWindowListener (new WL (frame)); 

       JLabel lab = new JLabel ("just some information"); 

       frame.setPreferredSize (new Dimension (400, 300)); 
       frame.setResizable (false); 
       frame.add (lab); 
       frame.pack(); 
       frame.setVisible (true); 
      } 
     }); 
    } 
} 

它编译并没有任何变化运行,所以你可以测试它。

“是”和“否”选项按预期工作,但我绝对不知道要在“取消”按钮的ActionListener中写什么内容。我想要的是,当用户点击“取消”按钮时,对话消失,但主窗口保持可见(即程序保持运行)。

现在,因为这一切都是在windowClosing方法来实现,它是一种明显,某种处置信号是为了摧毁JFrame发送。这意味着目前的设计可能无法做到这一点。我不介意重组/重新设计所有这些以使其发挥作用。只是...我不知道如何。

任何想法?

回答

9

更换

mainframe.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE); 

mainframe.setDefaultCloseOperation (JFrame.DO_NOTHING_ON_CLOSE); 

如果用户取消关闭 - 什么都不做。如果同意 - 手动拨打dispose()

+0

谢谢,我只是这样做,它的作品:)。 PS:其实这个改变是针对'大型机'而不是'diag'。 – 2012-02-12 14:51:44

6

看看JOptionPane,它为你处理大部分东西,例如

JOptionPane.showConfirmDialog(frame, "please choose one", 
     "information",  
     OptionPane.YES_NO_CANCEL_OPTION, 
     JOptionPane.INFORMATION_MESSAGE); 

DefaultCloseOperation需求是DO_NOTHING_ON_CLOSE让你的对话可以处理的事情 - 否则窗口将得到安置之前,用户可以取消它。然后你手动关闭窗口或退出应用程序或根据用户的选择。

+0

谢谢!我所要做的就是将'mainframe'上的'DefaultCloseOperation'设置为'DO_NOTHING_ON_CLOSE'并且它工作。你从哪里来 ?我应该给你几杯啤酒:)。 – 2012-02-12 14:49:11

+0

很高兴帮助。我在欧洲的另一边,所以啤酒也许有点远,但是,无论如何感谢;-) – DNA 2012-02-12 14:52:02

+0

请[接受](http://meta.stackexchange.com/a/65088/155831)答案时你有机会。 – 2012-02-12 14:58:21