JOptionPane showOptionDialog随机显示空白

时间:2014-01-21 20:31:31

标签: java swing

我在大学里学了几门Java编程课,但是已经有几年了,所以我很生气。我决定问我哥哥的编程任务。他让我写了一个程序来测试口袋妖怪游戏中元素类型的有效性。该代码大部分都适用,但由于某种原因,选项对话框有时是空白的。我有一个标题,但其余的是一个灰色的盒子。对我来说最让人困惑的部分是我之前有一个println(),它在窗格为空时打印正确的短语。

这是我的代码输出:

import javax.swing.JOptionPane;
public class Quiz {


public static void main(String[] args) {
    try{
        /*creates the matrix of the different types 
         *and their effectiveness to each other. 0
         *represents "Not very effective", 1 is "Neutral"
         *2 is "Super effective", 3 is "No Damage"
        */

        int[][] myTypeArray = 
            {{1, 1, 1, 1, 1, 0, 1, 3, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                {2, 1, 0, 0, 1, 2, 0, 3, 2, 1, 1, 1, 1, 0, 2, 1, 2, 0},
                {1, 2, 1, 1, 1, 0, 2, 1, 0, 1, 1, 2, 0, 1, 1, 1, 1, 1},
                {1, 1, 1, 0, 0, 0, 1, 0, 3, 1, 1, 2, 1, 1, 1, 1, 1, 2},
                {1, 1, 3, 2, 1, 2, 0, 1, 2, 2, 1, 0, 2, 1, 1, 1, 1, 1},
                {1, 0, 2, 1, 0, 1, 2, 1, 0, 2, 1, 1, 1, 1, 2, 1, 1, 1},
                {1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 2, 1, 2, 1, 1, 2, 0},
                {3, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 0, 1},
                {1, 1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 1, 0, 1, 2, 1, 1, 2},
                {1, 1, 1, 1, 1, 0, 2, 1, 2, 0, 0, 2, 1, 1, 2, 0, 1, 1},
                {1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 0, 0, 1, 1 ,1, 0, 1, 1},
                {1, 1, 0, 0, 2, 2, 0, 1, 0, 0, 2, 0, 1, 1, 1, 0, 1, 1},
                {1, 1, 2, 1, 3, 1, 1, 1, 1, 1, 2, 0, 0, 1, 1, 0, 1, 1},
                {1, 2, 1, 2, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 3, 1},
                {1, 1, 2, 1, 2, 1, 1, 1, 0, 0, 0, 2, 1, 1, 0, 2, 1, 1},
                {1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 2, 1, 3},
                {1, 0, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 0, 0},
                {1, 2, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 2, 2, 1}};

        //Names for the types
        String[] myTypeNamesArray = {"Normal", "Fighting", "Flying", "Poison", "Ground", 
                "Rock", "Bug", "Ghost", "Steel", "Fire", "Water", 
                "Grass", "Electric", "Psychic", "Ice", "Dragon", 
                "Dark", "Fairy"};


        //loops the message panes until they get a wrong answer
        for(int i = 0; i > -1; i++){

            //Two integers to randomly select one of the 18 different types
            int num1 = (int)(Math.random()*18);
            int num2 = (int)(Math.random()*18);

            //Creates JOptionPane to show the two randomed types and get input
            //Name of the buttons
            Object[] buttons = { "Not Very Effective", "Neutral", "Super Effective", "No Damage" };

            //Test output for asking them how effective type 1 is vs type 2
            System.out.println(num1 + " " + num2 + " " + "How effective is " + 
                    myTypeNamesArray[num1] + " against " + myTypeNamesArray[num2] + "?    " + 
                    "the answer was " + (String)buttons[myTypeArray[num1][num2]] + ".   " + i);

            // **HERE IS WHERE THE MESSAGE IS BLANK SOMETIMES**
            int answer = JOptionPane.showOptionDialog(null, "How effective is " + 
                    myTypeNamesArray[num1] + " against " + myTypeNamesArray[num2] + "?",
                    "Pokemon Type Effectiveness Quiz", JOptionPane.DEFAULT_OPTION, 
                    JOptionPane.QUESTION_MESSAGE, null, buttons, buttons[0]);


            //Test their answers                     
            if(answer == myTypeArray[num1][num2]){
            } else if(!(answer == myTypeArray[num1][num2])) {
                // **THIS IS ALSO BLANK SOMETIMES **
                JOptionPane.showMessageDialog(null, "Sorry, the answer was " + 
                        (String)buttons[myTypeArray[num1][num2]] + ". You said " + 
                        buttons[answer] + "\nYou got " + i + " correct.");
                break;
            } else {
                break;
            }
        }
    } catch (Exception e) {
        System.out.println("The error was " + e);
    }

}//end main

}

1 个答案:

答案 0 :(得分:2)

问题的间歇性表明存在线程问题。尝试在Swing事件线程上运行代码。类似的东西:

public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {
     public void run() {
        doOnEventThread();
     }
  });
}

public static void doOnEventThread() {
    try{
        /*creates the matrix of the different types 
         *and their effectiveness to each other. 0
         *represents "Not very effective", 1 is "Neutral"
         *2 is "Super effective", 3 is "No Damage"
        */

        int[][] myTypeArray = 
            {{1, 1, 1, 1, 1, 0, 1, 3, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                {2, 1, 0, 0, 1, 2, 0, 3, 2, 1, 1, 1, 1, 0, 2, 1, 2, 0},
                {1, 2, 1, 1, 1, 0, 2, 1, 0, 1, 1, 2, 0, 1, 1, 1, 1, 1},
                {1, 1, 1, 0, 0, 0, 1, 0, 3, 1, 1, 2, 1, 1, 1, 1, 1, 2},
                {1, 1, 3, 2, 1, 2, 0, 1, 2, 2, 1, 0, 2, 1, 1, 1, 1, 1},
                {1, 0, 2, 1, 0, 1, 2, 1, 0, 2, 1, 1, 1, 1, 2, 1, 1, 1},
                {1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 2, 1, 2, 1, 1, 2, 0},
                {3, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 0, 1},
                {1, 1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 1, 0, 1, 2, 1, 1, 2},
                {1, 1, 1, 1, 1, 0, 2, 1, 2, 0, 0, 2, 1, 1, 2, 0, 1, 1},
                {1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 0, 0, 1, 1 ,1, 0, 1, 1},
                {1, 1, 0, 0, 2, 2, 0, 1, 0, 0, 2, 0, 1, 1, 1, 0, 1, 1},
                {1, 1, 2, 1, 3, 1, 1, 1, 1, 1, 2, 0, 0, 1, 1, 0, 1, 1},
                {1, 2, 1, 2, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 3, 1},
                {1, 1, 2, 1, 2, 1, 1, 1, 0, 0, 0, 2, 1, 1, 0, 2, 1, 1},
                {1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 2, 1, 3},
                {1, 0, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 0, 0},
                {1, 2, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 2, 2, 1}};

        //Names for the types
        String[] myTypeNamesArray = {"Normal", "Fighting", "Flying", "Poison", "Ground", 
                "Rock", "Bug", "Ghost", "Steel", "Fire", "Water", 
                "Grass", "Electric", "Psychic", "Ice", "Dragon", 
                "Dark", "Fairy"};


        //loops the message panes until they get a wrong answer
        for(int i = 0; i > -1; i++){

            //Two integers to randomly select one of the 18 different types
            int num1 = (int)(Math.random()*18);
            int num2 = (int)(Math.random()*18);

            //Creates JOptionPane to show the two randomed types and get input
            //Name of the buttons
            Object[] buttons = { "Not Very Effective", "Neutral", "Super Effective", "No Damage" };

            //Test output for asking them how effective type 1 is vs type 2
            System.out.println(num1 + " " + num2 + " " + "How effective is " + 
                    myTypeNamesArray[num1] + " against " + myTypeNamesArray[num2] + "?    " + 
                    "the answer was " + (String)buttons[myTypeArray[num1][num2]] + ".   " + i);

            // **HERE IS WHERE THE MESSAGE IS BLANK SOMETIMES**
            int answer = JOptionPane.showOptionDialog(null, "How effective is " + 
                    myTypeNamesArray[num1] + " against " + myTypeNamesArray[num2] + "?",
                    "Pokemon Type Effectiveness Quiz", JOptionPane.DEFAULT_OPTION, 
                    JOptionPane.QUESTION_MESSAGE, null, buttons, buttons[0]);


            //Test their answers                     
            if(answer == myTypeArray[num1][num2]){
            } else if(!(answer == myTypeArray[num1][num2])) {
                // **THIS IS ALSO BLANK SOMETIMES **
                JOptionPane.showMessageDialog(null, "Sorry, the answer was " + 
                        (String)buttons[myTypeArray[num1][num2]] + ". You said " + 
                        buttons[answer] + "\nYou got " + i + " correct.");
                break;
            } else {
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

修改
有关Swing的事件线程和线程问题的更多信息,请查看文章Concurrency in Swing。但是要重新进行迭代,每当你发现自己遇到间歇性的程序错误行为时,这种情况不会一直发生,请考虑“线程问题”。