【发布时间】:2014-11-24 14:04:03
【问题描述】:
我有一个 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);
}
});
请给我一些指导,谢谢
【问题讨论】:
-
感谢@user2408578,System.exit(0) 工作正常,但在关闭应用程序之前需要一个确认窗口,谢谢
-
没听懂我说试试 if (result == JOptionPane.YES_OPTION){ } ....
-
谢谢@user2408578,它给了error,谢谢
标签: java swing user-interface