Java JOptionPane 处理确定、取消和 x 按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24970176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:04:35  来源:igfitidea点击:

JOptionPane handling ok, cancel and x button

javaswingjoptionpane

提问by user3879568

There is one problem I am facing with my JOptionPane if I don't key in anything,no matter what I press(the ok,cancel or the x button(the top right button in the JOptionPane)), it will prompt me until the I key in a positive value. but I only want the prompt to happen when i pressed ok.

如果我不输入任何内容,我的 JOptionPane 将面临一个问题,无论我按什么(确定、取消或 x 按钮(JOptionPane 中的右上角按钮)),它都会提示我,直到我输入一个正值。但我只希望在我按下确定时出现提示。

If I click on cancel or the x button(the top right button in the JOptionPane), it will close the JOptionPane

如果我单击取消或 x 按钮(JOptionPane 中的右上角按钮),它将关闭 JOptionPane

How can I do this?

我怎样才能做到这一点?

import javax.swing.JOptionPane;

public class OptionPane {
    public static void main(final String[] args) {
        int value = 0;
        boolean isPositive = false , isNumeric = true;
        do {
            try {
                value = Integer.parseInt(JOptionPane.showInputDialog(null,
                    "Enter value?", null));
            } catch (NumberFormatException e) {
                System.out.println("*** Please enter an integer ***");
                isNumeric = false;
            }

             if(isNumeric) {
                if(value <= 0) {
                    System.out.println("value cannot be 0 or negative");
                }

                else {
                    System.out.println("value is positive");
                    isPositive = true;
                }
            }
        }while(!isPositive);
    }
}

采纳答案by bluevoxel

The basic approach for this can look like I've showed below:

对此的基本方法可能如下所示:

Updated after @MadProgrammer comment.

在@MadProgrammer 评论后更新。

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

public class DemoJOption {
    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");
        }
    }
}

回答by Braj

Simply move the code in tryblock and there is no need to use any flag just breakthe infinite loop once positive number is entered.

只需在try块中移动代码,break一旦输入正数,就无需使用任何标志,只需无限循环即可。

Sample code:

示例代码:

public static void main(final String[] args) {
    int value = 0;
    while (true) {
        try {
            value = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter value?", null));

            if (value <= 0) {
                System.out.println("value cannot be 0 or negative");
            } else {
                System.out.println("value is positive");
                break;
            }
        } catch (NumberFormatException e) {
            System.out.println("*** Please enter an integer ***");
        }
    }
}

回答by Sanjeev

JoptionPane#showInputDialog returns user's input, or null meaning the user canceled the input.

So instead of directly parsing the return value. First check if it is null (User Cancelled) then do nothing if not null then parse for the integer value

所以不是直接解析返回值。首先检查它是否为空(用户已取消),如果不是空则不执行任何操作,然后解析整数值