62,614
社区成员
发帖
与我相关
我的任务
分享
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.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
if (JOptionPane.showConfirmDialog((JFrame) arg0.getSource(),
"你确定退出程序嘛?", "Exit Confirm", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
System.out.println("exit program");
System.exit(0);
}
}
});
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainFrm thisClass = new MainFrm();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
protected void processWindowEvent(WindowEvent e){
if(e.getID() == WindowEvent.WINDOW_CLOSING)
{
int value = JOptionPane.showConfirmDialog(null,"是否退出系统?","退出系统",JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
if(value == JOptionPane.OK_OPTION)
{
CloseSystem();
System.exit(0);
}
}
else
{
super.processWindowEvent(e);
}
}
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) {
final 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) {
if (JOptionPane.showConfirmDialog(frame,
"你确定退出程序嘛?",
"Exit Confirm",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
System.out.println("exit program");
System.exit(0);
}
}
});
}
}