Java JLabel数组使用后删除元素

Java JLabel数组使用后删除元素,java,arrays,netbeans,jlabel,Java,Arrays,Netbeans,Jlabel,所以我在Netbeans中有一个JFrame,它包含20个数学方程标签 mathLabel1是“2+2” mathLabel2是“4*4” 等等 如果显示mathLabel1并且用户猜到了正确的答案(4),那么我想setVisible(false)并将该元素从数组中删除,这样就不会再次出现问题 基本上,没有重复 以下是我的代码的简短版本: //declare variables String strUserAnswer; int i; Random r = new Random(); int r

所以我在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);
        }
    }
对于
数学[1]
数学[2]
数学[3]
,等等,这是重复的


那我该怎么做呢?我尝试了remove()方法,但那是一个偶然的机会…

如果您的数据结构将不断变化,请尝试使用列表而不是数组:

List<JLabel> labels = new ArrayList<JLabel>();
int numLabels = 20;
for (int i = 0; i < numLabels; i++) {
    labels.add(new JLabel(i + " " + i));
}

然后重新验证你的JPanel

编辑2:

我可能误解了你的问题-似乎你想删除一个数字,而不再为它创建标签。这是正确的方法:

int numIntegers = 20;
Set<Integer> possibleNumbers = new HashSet<Integer>();
for (int i = 0; i < numIntegers; i++) {
    possibleNumbers.add(i);
}
然后,当您想要显示此数据时,可以使用:

panel.clear();
for (Integer number : possibleNumbers) {
    panel.add(new JLabel(number + "  " + number));
}

(请注意,我调用JLabels数据是不正确的-它们是演示文稿的一部分。)

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

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
}  
public void random(){
随机r=新随机();
randValue=r.nextInt(math.length);//确保索引始终在数组中
JLabel[]temp=newjlabel[math.length-1];//这就可以了
数学[randValue].setVisible(真);
对于(int i=0;i
这应该是一个使用阵列的解决方案。
希望能有所帮助。

哦,天哪,请更改代码设计!!我将创建一个
Question
类,该类可以有一个JLabel作为属性,不过最好总是使用相同的JLabel,并根据Question对象设置文本。问题对象包括问题本身和正确答案。然后你可以有一个问题对象的数组列表。我认为setText会是一个更好的主意,谢谢!那么,标签也会被删除吗?删除(4)
删除该标签,使其不再出现?您必须理解您的数据及其显示之间的差异。您的流程基本上是数据->表示->数据->表示,无限。更改数据后(例如,使用labels.remove(4)),仍然需要“显示”数据。因此,您必须清除您的JPanel并重新绘制JLabel。抱歉,我误解了,我已更改了答案。SDadas,有没有任何可能的方法可以做到这一点,而不必过多地偏离我已有的代码?这是唯一不起作用的部分。我从来没有完全理解过列表,也没有像您在编辑中那样理解for循环。事实并非如此,因为有两个原因:您的代码不会针对多个问题运行(这意味着您无论如何都要实现它),而且很难按原样查看代码。每当有大量if语句时,都要找到一种使用for循环的方法。我的for循环并不复杂,它只是遍历集合中的每个项。你可以用原来问题中一半的行数来编写程序,但你必须从基础开始——在反复被问问题的地方循环。然后添加JLabels。理论上,这应该是可行的,但每次我单击按钮时它都会替换它,我只希望在您单击“go”按钮并且答案正确时它会发生更改。在这种情况下,只需添加一个if语句来检查答案是否正确。您可能需要/想要创建一个布尔变量来跟踪它。您还可以将从列表中删除JLabel的代码分离到自己的方法中:remove(int-index)是一个不错的选择。这样,你就可以随时打开正确的标签,如果按下按钮,答案是正确的,你可以调用remove方法,每个人都很高兴。关于随机事件的评论+1-1表示脏阵列解决方案。总的来说是+/-0。
possibleNumbers.remove(14);
panel.clear();
for (Integer number : possibleNumbers) {
    panel.add(new JLabel(number + "  " + number));
}
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
}