我有一个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);
}
});
请给我这方面的一些方向,感谢
尝试System.exit(0) – user2408578 2014-11-24 14:07:37
感谢@ user2408578,系统。退出(0)工作正常,但要关闭应用程序之前确认窗口,谢谢 – 2014-11-24 14:09:02
没有得到它我说试试它在if(result == JOptionPane.YES_OPTION){} .... – user2408578 2014-11-24 14:11:25