gpt4 book ai didi

Java JLabel 数组在使用后删除元素

转载 作者:行者123 更新时间:2023-11-29 05:51:17 27 4
gpt4 key购买 nike

所以我在 Netbeans 中有一个 JFrame,它包含 20 个数学方程式标签。

mathLabel1 是“2 + 2”

mathLabel2 是 "4 * 4"

等...

如果显示 mathLabel1 并且用户猜对了答案 (4),那么我想 setVisible(false) 并从我的数组中删除该元素,因此它不会作为再提问。

基本上没有重复

这是我的代码的简短版本:

//declare variables
String strUserAnswer;
int i;
Random r = new Random();
int randvalue = r.nextInt(19);
JLabel[] math = {mathLabel1, mathLabel2, mathLabel3, mathLabel4, mathLabel5, mathLabel6, mathLabel7, mathLabel8, mathLabel9, mathLabel10,
mathLabel11, mathLabel12, mathLabel13, mathLabel14, mathLabel15, mathLabel16, mathLabel17, mathLabel18, mathLabel19, mathLabel20};
JLabel test;

//method that chooses random math equation
public void random(JLabel test) {
r = new Random();
randvalue = r.nextInt(19);
test = math[randvalue];

if (test == math[0]) {
mathLabel1.setVisible(true);
}
else if (test == math[1]){
mathLabel2.setVisible (true);
}
else if (test == math[2]){
mathLabel3.setVisible (true);
}
else if (test == math[3]){
mathLabel4.setVisible (true);
}
else if (test == math[4]){
mathLabel5.setVisible (true);
}
else if (test == math[5]){
mathLabel6.setVisible (true);
}
else if (test == math[6]){
mathLabel7.setVisible (true);
}
else if (test == math[7]){
mathLabel8.setVisible (true);
}
else if (test == math[8]){
mathLabel9.setVisible (true);
}
else if (test == math[9]){
mathLabel10.setVisible (true);
}
else if (test == math[10]){
mathLabel11.setVisible (true);
}
else if (test == math[11]){
mathLabel12.setVisible (true);
}
else if (test == math[12]){
mathLabel13.setVisible (true);
}
else if (test == math[13]){
mathLabel14.setVisible (true);
}
else if (test == math[14]){
mathLabel15.setVisible (true);
}
else if (test == math[15]){
mathLabel16.setVisible (true);
}
else if (test == math[16]){
mathLabel17.setVisible (true);
}
else if (test == math[17]){
mathLabel18.setVisible (true);
}
else if (test == math[18]){
mathLabel19.setVisible (true);
}
else if (test == math[19]){
mathLabel20.setVisible (true);
}
}

private void guessButtonActionPerformed(java.awt.event.ActionEvent evt) {
// User clicks guess to enter answer, if correct part of puzzle appears

strUserAnswer = answerText.getText();
test = math[randvalue];

//if the math equation chosen is 2+2...
if (test == math[0]) {

//show math equation
mathLabel1.setVisible(true);

//if answer is right...
if (strUserAnswer.equals("4")) {
JOptionPane.showMessageDialog(null, "Yay!! That is right!");
//show puzzle piece, hide equation, and choose a new one
label1.setVisible(true);
mathLabel1.setVisible(false);
//test.remove(math[0]);
test = math[randvalue];
answerText.setText(null);
random(test);

//if answer is wrong...
} else {
JOptionPane.showMessageDialog(null, " Sorry, try again!");
answerText.setRequestFocusEnabled(true);
}
}

对于 math[1]math[2]math[3] 等重复此操作...

那我该怎么做呢?我尝试了 remove() 方法,但那是在黑暗中拍摄的...

最佳答案

好的,所以这可能会让您开心或心碎,但是您使用 random() 方法所做的远远超过您需要做的。首先,你似乎不需要接受参数因为看起来您在使用之前手动更改了该值。另外,因为数组中的每个值实际上都是一个 JLabel,所以您可以只说 math[randValue].setVisible(true) 而不是遍历整个 if 语句。为了解决您删除内容的问题,我将向您展示一种快速而肮脏的方法,但您最好使用 ArrayList 而不是 Array。

public void random() {
Random r = new Random();
randValue = r.nextInt(math.length); //make sure the index is always within the array
JLabel[] temp = new JLabel[math.length - 1]; //this will do the trick
math[randValue].setVisible(true);
for (int i = 0; i < randvalue; i++) {
temp[i] = math[i]; //fill the new array up to the chosen label
}
for (int i = randValue; i < temp.length; i++) {
temp[i] = math[i + 1]; //fill the rest, omitting the chosen label
}
math = new JLabel[temp.length]; //math is now shorter
math = temp; //put everything back in the original array
}

这应该作为使用数组的解决方案。希望对您有所帮助。

关于Java JLabel 数组在使用后删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13772607/

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