gpt4 book ai didi

java - JOptionPane showOptionDialog 随机显示空白

转载 作者:行者123 更新时间:2023-11-30 09:12:56 25 4
gpt4 key购买 nike

我在大学里上过几门 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com