如何在关闭对话框时终止Java应用程序?

时间:2015-12-23 12:42:09

标签: java

我想在用户关闭下面的对话框时终止执行

String firstNumber = JOptionPane.showInputDialog("Enter the first Number");

1 个答案:

答案 0 :(得分:0)

首先, JOptionPane 没有关闭事件。

幸运的是 JOptionPane showInputDialog (“message”)如果用户按下其中一个关闭按钮,则返回 null

import javax.swing.JOptionPane;

public class TestClass
{
    public static void main (String[] args)
    {
        String firstNumber = JOptionPane.showInputDialog("Enter the first Number");

        if (firstNumber == null)
        {
            System.out.println("X or Close was pressed.");
            System.exit(0);
        }
        else 
        {
            System.out.println("OK was pressed.");
        }
    }
}