gpt4 book ai didi

java - GUI 循环问题 Java

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:46:55 26 4
gpt4 key购买 nike

我是一个新手,尝试使用基本的GUI功能制作一个简单的前56个元素的问答程序作为练习。

但是,当我运行我的代码时,我在代码运行期间随机出现错误。

将出现一个没有对话的文本框,只有消息作为文本框的名称,以及一个用于退出的 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);
}
}

最佳答案

您应该从 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);
}
});
}
}

关于java - GUI 循环问题 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25770447/

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