/**
* 初始化窗休面板
* @return void
*/
private void initialize() {
this.setSize(600, 400);
this.setContentPane(getJContentPane());
this.setTitle(XmlConfig.getValue("title"));
this.setLocation();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
super.windowClosing(arg0);
JOptionPane op=new JOptionPane();
int ret=op.showConfirmDialog(null,"确定要关闭应用程序吗?","关闭提示",JOptionPane.OK_CANCEL_OPTION);
if(JOptionPane.OK_OPTION==ret){
ScanThread.flag = true; // 设置线程类,状态
System.exit(0);
}else{
op.showMessageDialog(null, "当前已经在windowclosing中,要退出去,似乎不行了,寻高手解决它。");
}
}
});
this.setVisible(true);
}以上方法完成初始化一个Frame窗体.我现在想要实现点关闭的X时,提示一下,如果用户取消关闭.我就不想关窗体了.请问高手怎么解决它?
frame.addWindowListener(new WindowAdapter() {})import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JFrame;
import javax.swing.JOptionPane;public class FrameTest { public static void main(String[] args) {
JFrame frame = new JFrame("test");
frame.setSize(500, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
int k = JOptionPane.showConfirmDialog((JFrame) arg0.getSource(), "你确定退出程序嘛?",
"Exit Confirm", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (k == 0) {
System.out.println("exit program");
System.exit(0);
}else {
return ;
}
}
});
}
}
* 重载该方法,以便在窗口关闭时,能退出
* @param e WindowEvent windows事件
*/
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
JOptionPane op = new JOptionPane();
int ret = op.showConfirmDialog(null, "确定要关闭应用程序吗?", "关闭提示",JOptionPane.OK_CANCEL_OPTION);
if (JOptionPane.OK_OPTION == ret) {
System.exit(0);
} else {
op.showMessageDialog(null,"当前已经在windowclosing中,要退出去,似乎不行了,寻高手解决它。");
return;
}
}
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
Runtime.getRuntime().halt(0);
}
}