【问题标题】:Java JLabel array remove element after useJava JLabel 数组使用后删除元素
【发布时间】:2012-12-07 23:23:28
【问题描述】:

所以我在 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() 方法,但那是在黑暗中拍摄...

【问题讨论】:

  • 天哪,请更改代码设计!
  • 我会创建一个Question 类,它可以有一个 JLabel 作为属性,尽管最好始终使用相同的 JLabel 并根据 Question 对象设置文本。 Question 对象包括问题本身及其正确答案。然后你可以有一个 Question 对象的 ArrayList。
  • 我认为 setText 会是一个更好的主意,谢谢!

标签: java arrays netbeans jlabel


【解决方案1】:

好的,所以这可能会让你开心或伤心,但你用 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
}  

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

【讨论】:

  • 理论上这应该可以,但是每次我点击按钮时它都会替换它,我只希望它在你点击“开始”按钮并且答案正确时改变
  • 在这种情况下,只需添加一个 if 语句来检查答案是否正确。您可能需要/想要创建一个布尔变量来跟踪它。您还可以将用于从列表中删除 JLabel 的代码分离到它自己的方法中:remove(int index) 将是一个不错的选择。这样,你总是可以拉出正确的标签,如果按下按钮并且答案是正确的,你调用 remove 方法,每个人都很高兴。
  • +1 对 Random 事物的评论。 -1 表示脏数组解决方案。所以总共 +/- 0。
【解决方案2】:

如果您的数据结构会不断变化,请尝试使用 List 而不是 Array:

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

从那里你可以随时调用:

labels.get(4).setVisible(false);

labels.remove(4);

然后重新验证您的 JPanel。

编辑 2:

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

int numIntegers = 20;
Set<Integer> possibleNumbers = new HashSet<Integer>();
for (int i = 0; i < numIntegers; i++) {
    possibleNumbers.add(i);
}

当你想删除一个项目时,使用:

possibleNumbers.remove(14);

那么当你想呈现这些数据时,你可以使用:

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

(请注意,我错误地调用了 JLabels 数据 - 它们是演示文稿的一部分。)

【讨论】:

  • 那么labels.remove(4) 会不会再删除该标签?
  • 你必须了解你的数据和它的表现形式之间的区别。您的流程基本上是数据 -> 展示 -> 数据 -> 展示,无限循环。更改数据后(例如,使用 labels.remove(4)),您仍然必须“呈现”数据。因此,您必须清除 JPanel 并再次重新绘制 JLabels。
  • 对不起,我误会了,我已经改变了答案。
  • sdasdadas,有没有什么可能的方法可以做到这一点,而不会从我已经拥有的代码中转移太多?这是唯一不起作用的部分。而且我从来没有完全理解列表,也没有像你在编辑中那样理解 for 循环。
  • 不是真的,因为两件事:你的代码不能运行超过一个问题(这意味着你无论如何都会实现它),而且很难看到你的代码是。每当您有大量 if 语句时,请找到一种使用 for 循环的方法。我的 for 循环并不复杂,它只是遍历集合中的每个项目。您可能可以用原始问题中一半的行数编写程序,但您必须从基础开始 - 在反复询问问题的地方进行循环。然后添加 JLabels。
猜你喜欢
  • 2015-12-02
  • 2022-01-13
  • 2014-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-09
  • 2022-06-23
  • 2021-03-25
相关资源
最近更新 更多