我是一个尝试使用基本GUI功能的新手,为前56个元素制作一个简单的问答程序。
然而,当我运行我的代码时,我在代码随机运行期间遇到错误。
将出现一个没有对话框的文本框,只有Message作为文本框的名称,以及一个要退出的X按钮。有时当我按X时它会进入我代码中的下一行,有时则会退出。我很难重现错误,因为它似乎是随机发生的。
我的主要方法发布在下面:
import java.math.*;
import java.text.DecimalFormat;
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
import java.util.Random;
//This lab is to study periodc table of elements with simple gui output
public class practiceFiftyEight
{
public static void main(String[] args)
{
String input;
boolean answer;
Random myRan = new Random();
int randInt = 0;
//random num from 0 - 56
String[] arrayElements = {"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Fluorine","Neon","Sodium","Mangnesium","Aluminium","Silicon","Phosphorus","Sulfur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chronium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc",
"Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium","Iodine","Xenon","Cesium","Barium"};
String[] arrayEleAbriv = {"H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As",
"Se","Br","Kr","Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe","Cs","Ba"};
int repeat;
do
{
randInt = myRan.nextInt(56);
JOptionPane.showMessageDialog(null, " sizes of arrays " + arrayElements.length + " " + arrayEleAbriv.length);
JOptionPane.showMessageDialog(null, " What is the symbol of " + arrayElements[randInt]);
input = JOptionPane.showInputDialog(null," Enter element symbol ( 1 - 56 ) of");
answer = input.equalsIgnoreCase(arrayEleAbriv[randInt]);
if(answer)
{
JOptionPane.showMessageDialog(null, " Correct " + arrayElements[randInt] + " is represented by " + arrayEleAbriv[randInt] );
}
else
{
JOptionPane.showMessageDialog(null, " WRONG!!!!! " + arrayElements[randInt] + " is represented by " + arrayEleAbriv[randInt] + " !!!!! " );
}
repeat = JOptionPane.showConfirmDialog(null, "Press Yes to repeat, No to quit ", "please confirmm", JOptionPane.YES_NO_OPTION);
}while(repeat == JOptionPane.YES_OPTION);
}
}
答案 0 :(得分:3)
你应该从EDT调用swing组件。
如果用户选择取消,还有可能抛出NullPointerException的地方。
以下代码应该无需冻结。
import java.text.DecimalFormat;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import java.util.Random;
//This lab is to study periodc table of elements with simple gui output
public class PracticeFiftyEight {
final static String[] arrayElements = {"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Fluorine","Neon","Sodium","Magnesium",
"Aluminium","Silicon","Phosphorus","Sulfur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium",
"Chromium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine",
"Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium",
"Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium","Iodine","Xenon","Cesium","Barium"};
final static String[] arrayEleAbriv = {"H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As",
"Se","Br","Kr","Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe","Cs","Ba"};
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
String input;
boolean answer;
Random myRan = new Random();
int randInt = 0;
//random num from 0 - 56
int repeat;
do {
randInt = myRan.nextInt(56);
JOptionPane.showMessageDialog(null, " sizes of arrays " + arrayElements.length + " " + arrayEleAbriv.length);
JOptionPane.showMessageDialog(null, " What is the symbol of " + arrayElements[randInt]);
input = JOptionPane.showInputDialog(null," Enter element symbol ( 1 - 56 ) of");
if(input != null) { // if user press cancel, input is null
answer = input.equalsIgnoreCase(arrayEleAbriv[randInt]);
if(answer) {
JOptionPane.showMessageDialog(null, " Correct " + arrayElements[randInt] + " is represented by " + arrayEleAbriv[randInt] );
} else {
JOptionPane.showMessageDialog(null, " WRONG!!!!! " + arrayElements[randInt] + " is represented by " + arrayEleAbriv[randInt] + " !!!!! " );
}
} else {
System.out.println("input is null: " + input);
}
repeat = JOptionPane.showConfirmDialog(null, "Press Yes to repeat, No to quit ", "please confirmm", JOptionPane.YES_NO_OPTION);
} while(repeat == JOptionPane.YES_OPTION);
}
});
}
}