java - JOptionPane showOptionDialog 随机显示空白

标签 java swing

我在大学里学过一些 Java 编程类(class),但已经过去几年了,所以我已经很生疏了。我决定向哥哥请求一项编程任务。他让我写一个程序来测试他在口袋妖怪游戏中元素类型的有效性。该代码在大多数情况下都有效,但由于某种原因,选项对话框有时是空白的。我有一个标题,但其余的是一个灰色框。最让我困惑的部分是我在它之前有一个 println() ,当 Pane 为空时它会打印正确的短语。

这是我的代码输出:

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

}

最佳答案

问题的间歇性表明存在线程问题。尝试在 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 。但重申一下,每当您发现自己遇到间歇性的程序错误行为(这种情况并不总是发生)时,请考虑“线程问题”。

关于java - JOptionPane showOptionDialog 随机显示空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21268480/

相关文章:

java - 如何扩展不可修改的模型以与 JPA 一起使用?

java - 如何摆脱 em.persist();

java - <Java> 棋盘棋子移动

java - java.lang.OutOfMemory : GC Overhead limit exceeded vs. java.lang.OutOfMemory: Multicast listener 有什么区别

java - 如何在字符串上突出显示

java - 一种更好的方法来处理文件上传中在套接字上读取的意外 EOF?

java - 在 Java JTextPane 中写入 UTF-8 字符

java - JProgressBar 未触发 setProgress 上的 propertyChange

java - 整个窗口的按钮 - Java

java - 在数据库获取操作期间显示进度条