javax.swing.JOptionPane取消按钮循环

时间:2014-03-10 07:36:31

标签: java swing joptionpane

我正在尝试编写一个while循环,以允许我的应用继续运行,直到用户在cancel上推送JOptionsPane。我该怎么做呢?我正在使用javax.swing.JOptionPane cancel按钮循环。

到目前为止这是代码,取消按钮不起作用。

 import java.io.*;
 import javax.swing.JOptionPane;

public class MyType2
{

    public static void main(String[] args)
    {

    String strChoice, strTryStrig, strTryInt, strTryDouble;
    int choice, tryInt;
    double tryDouble;
    boolean done = false;

    while(!done)
    {
        try
        {
            strChoice = JOptionPane.showInputDialog(null,"Whats My Type?" + "\n\n1) String\n2) Interger\n3) Double\n4) Quit the program\n ");
            choice = Integer.parseInt(strChoice);

                    switch(choice)
                    {
                    case 1:
                        if (choice == 1)
                            JOptionPane.showMessageDialog(null,"Correct , Any input can be saved as a String.", "Result",JOptionPane.INFORMATION_MESSAGE);
                        break;

                        case 2:
                        if (choice == 2);
                            JOptionPane.showMessageDialog(null,"Correct ", "Result",JOptionPane.INFORMATION_MESSAGE);

                        break;

                        case 3:
                        if (choice == 3)
                            JOptionPane.showMessageDialog(null,"Correct .", "Result",JOptionPane.INFORMATION_MESSAGE);

                        break;

                        case 4:
                        if (choice == 4)
                            done = true;
                        break;

                        default:
                        if(choice<=0 || choice>=5) throw new NumberFormatException();
                        break;
                    }
        }

    catch (NumberFormatException e)
    {
        JOptionPane.showMessageDialog(null,"Your number was not in the proper format.", "Error",JOptionPane.INFORMATION_MESSAGE);
    }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

javax.swing GUI元素“运行”直到你关闭它们。如果您想要通过按钮取消无限循环,我认为您将无法这样做,因为gui没有响应,除非您明确地处理线程。

如果是这样,您可以通过buttonclick ..

将循环条件切换为false