import javax.swing.*; import java.awt.event.*; public class WinCloseTest extends JFrame { public WinCloseTest(){ setTitle("WinCloseTest"); setSize(400,300); addWindowListener(new MyWinListener()); setVisible(true); } public static void main(String[] args){ new WinCloseTest(); } } class MyWinListener extends WindowAdapter{ public void windowClosing(WindowEvent e) { int ret = JOptionPane.showConfirmDialog(null, "确认退出吗?", "确认", JOptionPane.YES_NO_OPTION); System.out.println(ret); if (ret==JOptionPane.YES_OPTION) System.exit(0); } }