小编use*_*568的帖子

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

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

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

我怎样才能做到这一点?

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"); …
Run Code Online (Sandbox Code Playgroud)

java swing joptionpane

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×1

joptionpane ×1

swing ×1