首页javajoptionpaneJava Swing - 如何显示关闭JFrame的确认对话框

Java Swing - 如何显示关闭JFrame的确认对话框

我们想知道如何显示关闭JFrame的确认对话框。
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Main {
  public static void main(String... args) {
    JFrame frame = new JFrame();

    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        int result = JOptionPane.showConfirmDialog(frame,
            "Do you want to Exit ?", "Exit Confirmation : ",
            JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION)
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        else if (result == JOptionPane.NO_OPTION)
          frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
      }
    });

    frame.setSize(300, 300);
    frame.setVisible(true);

  }
}