首页javajoptionpaneJava Swing - 如何使JOptionPane处理确定,取消和x按钮

Java Swing - 如何使JOptionPane处理确定,取消和x按钮

我们想知道如何使JOptionPane处理确定,取消和x按钮。
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Main {
  public static void main(String args[]) {
    int n = JOptionPane.showOptionDialog(new JFrame(), "Message", "Title",
        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
        new Object[] { "Yes", "No" }, JOptionPane.YES_OPTION);

    if (n == JOptionPane.YES_OPTION) {
      System.out.println("Yes");
    } else if (n == JOptionPane.NO_OPTION) {
      System.out.println("No");
    } else if (n == JOptionPane.CLOSED_OPTION) {
      System.out.println("Closed by hitting the cross");
    }
  }
}