2014-11-24 65 views
0

我有一个Java应用程序,想要关闭的GUI与确认窗口关闭应用程序在按一下按钮,关闭应用程序的Java GUI

例如: -

 frmViperManufacturingRecord.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
     frmViperManufacturingRecord.addWindowListener(new WindowAdapter(){ 
      public void windowClosing(WindowEvent e){ 
       JFrame frame = (JFrame)e.getSource(); 

       int result = JOptionPane.showConfirmDialog(frame, "Are you sure you want to close the application?", "Please Confirm",JOptionPane.YES_NO_OPTION); 
       if (result == JOptionPane.YES_OPTION) 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      } 
      }); 

这是工作fine,当我按下窗口关闭(x)按钮,但我想把这个事件带到一个按钮来执行动作'点击',因为我是新手发现困难将它带入'actionPerformed'

到目前为止我已经尝试了下面的代码,它没有工作...

 //close window button 
     JButton btnCloseWindow = new JButton("Close Window"); 
     btnCloseWindow.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       //frmViperManufacturingRecord.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       //frmViperManufacturingRecord.dispose(); 
       //System.exit(0); 
       JFrame frame = (JFrame)e.getSource(); 

       int result = JOptionPane.showConfirmDialog(frame, "Are you sure you want to close the application?", "Please Confirm",JOptionPane.YES_NO_OPTION); 
       if (result == JOptionPane.YES_OPTION) 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      } 
     }); 

请给我这方面的一些方向,感谢

+0

尝试System.exit(0) – user2408578 2014-11-24 14:07:37

+0

感谢@ user2408578,系统。退出(0)工作正常,但要关闭应用程序之前确认窗口,谢谢 – 2014-11-24 14:09:02

+0

没有得到它我说试试它在if(result == JOptionPane.YES_OPTION){} .... – user2408578 2014-11-24 14:11:25

回答

1

你可以试试:

if (result == JOptionPane.YES_OPTION){ 
        frame.dispose(); 
      } 

还要注意CastException上线122

而不是

JFrame frame = (JFrame)e.getSource(); 

变化:

JFrame frame = new JFrame(); 
+0

感谢@ user2902894,我确实尝试了你的两个选项System.exit(0)和frame.dispose();都抛出[错误](http://i.imgur.com/VwOT4oY。PNG),谢谢 – 2014-11-24 14:22:46

+0

它似乎在其他行上抛出一个错误,你可以发布这个异常抛出线吗? – drgPP 2014-11-24 14:27:51

+0

谢谢@ user2902894,错误行122是“JFrame frame =(JFrame)e.getSource();”并已发布完整的[代码](http://jsfiddle.net/yL9coavw/)在这里,感谢您的时间和帮助 – 2014-11-24 14:32:04

3

更改此:

if (result == JOptionPane.YES_OPTION) 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

if (result == JOptionPane.YES_OPTION) 
    System.exit(0); 
} 

frame.setDefaultCloseOperation是当有人点击了 'X' 到时会发生什么关闭窗户。其他所有退出方式均由您控制。最好的方法是让窗口关闭侦听器和动作侦听器调用与弹出对话框相同的方法,并在用户想要退出时调用System.exit(0)。这也将帮助您进行清理操作。

示例代码:

public class Test extends JPanel implements WindowListener { 
    public Test() { 
     setLayout(new BorderLayout()); 
     add(new JLabel("This is a test."), BorderLayout.CENTER); 
     JButton b = new JButton("Exit"); 
     b.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       exit(); 

      } 
     }); 
     add(b, BorderLayout.SOUTH); 
    } 
    public static void main(String[] args) throws Exception { 
     JFrame f = new JFrame(); 
     Test t = new Test(); 
     f.add(t, BorderLayout.CENTER); 
     f.addWindowListener(t); 
     f.pack(); 
     f.setVisible(true); 
    } 
    public void windowClosing(WindowEvent e) { 
     exit(); 

    } 

    private void exit() { 
     System.exit(0); 
    } 
} 
+0

谢谢@markbernard ,已尝试过,但会在线程“AWT-EventQueue-0”中引发异常,异常。 [error](http://i.imgur.com/VwOT4oY.png) – 2014-11-24 14:19:13

+1

'System.exit(0)'不会导致ClassCastException。一定还有其他的错误。我已经添加了一些代码来显示一个工作示例。 – markbernard 2014-11-24 15:32:33

+0

谢谢@markbernard,错误不在System.exit(0)中,但@ 122行是由_user2902894_“JFrame frame =(JFrame)e.getSource();”指出的错误,已更改为“JFrame frame = new JFrame ();“,现在工作,非常感谢你的时间和帮助。 – 2014-11-24 16:14:05

1

如果仍无法正常工作,然后尝试

frame.setVisible(false); 
frame.dispose(); 

中,如果(结果== JOptionPane.YES_OPTION){}块

+0

谢谢@ user2408578,仍然给出同样的错误。谢谢 – 2014-11-24 14:34:51

+0

什么是异常...请粘贴堆栈跟踪 – user2408578 2014-11-24 14:41:33

+0

谢谢@ user2408578,已粘贴完整[stackTrace](http://jsfiddle.net/v52re3x7/),谢谢 – 2014-11-24 14:45:39

相关问题