public class Test extends JFrame { public Test() { setSize(100, 200); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); addWindowListener(new WindowAdapter(){ @Override public void windowClosing(WindowEvent e) { int option= JOptionPane.showConfirmDialog( Test.this, "确定退出系统? ", "提示 ",JOptionPane.YES_NO_CANCEL_OPTION); if(option==JOptionPane.YES_OPTION) if(e.getWindow() == Test.this) { System.exit(0); } else { return; } } }); } public static void main(String[] args) { new Test().setVisible(true); } }