【问题标题】:Ending the while loop using Cancel button or X button使用取消按钮或 X 按钮结束 while 循环
【发布时间】:2016-02-27 12:27:22
【问题描述】:

当用户单击 X 按钮或 CANCEL 按钮然后单击消息对话框的确定​​或 X 时,我需要帮助来结束循环。

这是我的代码:

import javax.swing.*;
public class SimpleCalculator1 
{

    public static void main(String [] args)
    {
        int error1, error2;
        JTextField field1 = new JTextField();
           JTextField field2 = new JTextField();
           JTextField field3 = new JTextField();
        Object[] message = 
            {
            "Enter first integer:", field1,
            "Input second integer:", field2,
            "Choose the operation to be used\nAddition(+)\nSubtraction(-)\nMultiplication(*)\nDivision(/):", field3,
            };
        int option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);

        while(true)
        {
            if(option == JOptionPane.OK_OPTION)
            {
                String value1 = field1.getText();
                String value2 = field2.getText();         
                String operation = field3.getText();

                try
                {
                   int num1 = Integer.parseInt(value1);
                   int num2 = Integer.parseInt(value2);
                }
                catch(NumberFormatException ne)
                {
                    JOptionPane.showConfirmDialog(null,"Invalid! Please input an integer","Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); //When I press ok, I need help to bring me back to input entries
                }

                switch(operation = field3.getText())
                {
                   case "+":
                    {
                        int num1 = Integer.parseInt(value1);
                        int num2 = Integer.parseInt(value2);
                        int result = num1+num2;
                        JOptionPane.showMessageDialog(null,"The sum is: "+result); 
                        break;
                    }
                case "-":
                    {
                        int num1 = Integer.parseInt(value1);
                        int num2 = Integer.parseInt(value2);
                        int result = num1-num2;
                        JOptionPane.showMessageDialog(null,"The difference is: "+result); 
                        break;
                    }
                 case "*":
                    {
                        int num1 = Integer.parseInt(value1);
                        int num2 = Integer.parseInt(value2);
                        int result = num1*num2;
                        JOptionPane.showMessageDialog(null,"The product is: "+result); 
                        break;
                    }
                case "/":
                    {
                        int num1 = Integer.parseInt(value1);
                        int num2 = Integer.parseInt(value2);
                        double result = (double)num1/num2;
                        JOptionPane.showMessageDialog(null,"The quotient is: "+result); 
                        break;
                    }

                default: 
                    error1 = JOptionPane.showConfirmDialog(null,"Invalid operation, please try again","Error",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE); //I need help here for creating a loop that will take me back to the beginning.
                }
                option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);

            }
            else if (option == JOptionPane.CANCEL_OPTION)
                JOptionPane.showMessageDialog(null,"Thank you for using our program!");

            else 
                JOptionPane.showMessageDialog(null,"Thank you for using our program!");    
        }
    }
}

【问题讨论】:

    标签: java swing loops


    【解决方案1】:

    如果用户点击关闭或取消,使用Break退出循环

    while(true)
    {
        if(option == JOptionPane.OK_OPTION)
        {
            String value1 = field1.getText();
    
    
            String value2 = field2.getText();
    
    
            String operation = field3.getText();
    
            try
            {
               int num1 = Integer.parseInt(value1);
               int num2 = Integer.parseInt(value2);
            }
            catch(NumberFormatException ne)
            {
                JOptionPane.showConfirmDialog(null,"Invalid! Please input an integer","Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); //When I press ok, I need help to bring me back to input entries
            }
        }
        else if(option == JOptionPane.CANCEL_OPTION || option == JOptionPane.CLOSED_OPTION){
            break;
        }
    }
    

    【讨论】:

    • @JoshuaEnriquez,我建议"accepting" 是正确答案之一。 :)
    • 我确实接受了 Clyme 的回答,我只是感谢您回答我的问题 :)
    【解决方案2】:

    我不确定java,但简单的想法是你需要在全局位置放置一个用真值初始化的变量,并在每次迭代中检查该变量是否仍然为真,如果变量为假则停止/中断/返回循环。 并且您可以使用 onclick 事件(如 onclick="variable=false;"

    )通过按钮设置该变量的值

    【讨论】:

      【解决方案3】:

      在进入循环之前创建一个布尔值:

      boolean shouldExit = false;
      while (!shouldExit) ...
      

      如果用户想取消程序,您可以将布尔值设置为 true:

      if (option == JOptionPane.CANCEL_OPTION) {
      shouldExit = true;
      }
      

      【讨论】:

      • 非常感谢。现在我知道当用户单击 JOptionPane 上的某个按钮时如何取消无限循环
      • 没问题,我很高兴它帮助了你:D
      猜你喜欢
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-03
      相关资源
      最近更新 更多