【问题标题】:showInputDialog - cancel option clickedshowInputDialog - 单击取消选项
【发布时间】:2021-02-12 14:37:27
【问题描述】:

如果用户在inputDialog上点击了Cancel,如何处理?我已经尝试过以前发布的解决方案,但都没有奏效。

这是我的代码

String bidString = JOptionPane.showInputDialog(null, "Place your bid ", "Bid",
                JOptionPane.PLAIN_MESSAGE);

    while(bidString.isEmpty())
    {
        JOptionPane.showMessageDialog(null,"You must enter a value",
                    "The Numbers Game", JOptionPane.ERROR_MESSAGE);

        bidString = JOptionPane.showInputDialog(null, "How much do you wish to bid?", "Initial funds"
                ,JOptionPane.QUESTION_MESSAGE);

        if(bidString != null)
        {
            return;
        }

    }
    int bid = Integer.parseInt(bidString);

【问题讨论】:

    标签: java swing user-interface joptionpane


    【解决方案1】:

    试试这样:

    String bidString;
    while (true) {
        bidString = JOptionPane.showInputDialog(null, "Place your bid ", "Bid",
                JOptionPane.PLAIN_MESSAGE);
        if (bidString == null) {
            System.out.println("cancelled");
            break;
        }
        if (bidString.isEmpty()) {
            System.out.println("wrong entry");
            JOptionPane.showMessageDialog(null, "You must enter a value",
                    "The Numbers Game", JOptionPane.ERROR_MESSAGE);
            continue;
        }else{
            try {
                int bid = Integer.parseInt(bidString);
            } catch (NumberFormatException nfe) {
                System.out.println("no number entered");
                continue;
            }
            break;
        }
    }
    

    【讨论】:

    • 代码肯定可以工作..请更具体点什么不工作?
    • If(bidString == null)
    猜你喜欢
    • 2013-05-02
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 2011-04-26
    • 2016-02-27
    相关资源
    最近更新 更多