清除JLabel无法正常工作

时间:2013-12-01 23:04:59

标签: java swing jlabel settext

我已经阅读了几篇帖子,建议使用setText(“”)在文本面板(显示屏)上清除JLabel(displayEntered)。但是,我尝试了这个,结果是它只是发布了两次输入的数组,并没有清除第一组。当按下按钮两次时,我有一个动作如下所示;第一个是输入输入的数据(我有相同的代码4次,4个不同的可能对象输入,但只是放入一个,因为它基本相同),这工作正常,第二个是删除显示的特定一个。我的代码很长,所以我只是把它放进去。如果有人想要别的东西,请告诉我。谢谢,我很感激任何意见!

 //adds the Herb data to the Array and list
 enterHerbData.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e){
        if(e.getActionCommand().equals("Enter")){
            Name = NameTxt.getText();
            Colors = ColorTxt.getText();
            ID = (int) IDCmbo.getSelectedItem();
            Flavor = FlavorTxt.getText();
            if(((String) MedicinalCmbo.getSelectedItem()).equals("Yes"))
                Medicinal = true; 
            else
                Medicinal = false;
            if(((String) SeasonalCmbo.getSelectedItem()).equals("Yes"))
                Seasonal = true; 
            else
                Seasonal = false;

            plants[count] = new Herb(Name, ID, Colors, Flavor, Medicinal, Seasonal);
            String displayArraytemp = " ";

                if(plants[count] != null){
                    if(plants[count] instanceof Flower){
                        displayArraytemp = ((count + 1) + ": " + plants[count].getID() + ", " + plants[count].getName() + ", " + ((Flower)plants[count]).getColor() + ", " + ((Flower)plants[count]).getSmell() + ", Thorny: " + ((Flower)plants[count]).getThorns() + "\n");
                        }
                        else if(plants[count] instanceof Fungus){
                        displayArraytemp = ((count + 1) + ": " + plants[count].getID() + ", " + plants[count].getName() + ", " + ((Fungus)plants[count]).getColor() + ", Poisonous: " + ((Fungus)plants[count]).getPoisonous() + "\n");
                        }
                        else if(plants[count] instanceof Weed){
                        displayArraytemp = ((count + 1) + ": " + plants[count].getID() + ", " + plants[count].getName() + ", " + ((Weed)plants[count]).getColor() + ", Edible: " + ((Weed)plants[count]).getEdible() + ", Medicinal: " + ((Weed)plants[count]).getMedicinal() + ", Poisonous: " + ((Weed)plants[count]).getPoisonous() + "\n");
                        }
                        else if(plants[count] instanceof Herb){
                        displayArraytemp = ((count + 1) + ": " + plants[count].getID() + ", " + plants[count].getName() + ", " + ((Herb)plants[count]).getColor() + ", " + ((Herb)plants[count]).getFlavor() + ", Medicinal: " + ((Herb)plants[count]).getMedicinal() + ", Poisonous: " + ((Herb)plants[count]).getSeasonal() + "\n");
                        }
                    sb.append("<html>" + displayArraytemp).
                        append("<br> ");
                    displayArray = sb.toString();
                }

            displayEntered.setText(displayArray);
            count++;
            frameB.setVisible(false);
        }
    }
});

 //removes the data to the Array and panel
 ActionListener RemoveAction = new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent RemoveAction){
        if(RemoveAction.getActionCommand().equals("Enter")){
        if((Btn1).isSelected()){
            String displayArraytemp2 = " ";
            if(count >= 1){
                for(int n = 0; n < count; n++){
                    plants[n] = plants[n+1];
                }
                count--;
                frameB.setVisible(false);
                displayEntered.setOpaque(true);
                for(int n = 0; n < 25; n++){
                    if(plants[n] != null){
                        if(plants[n] instanceof Flower){
                            displayArraytemp2 = ((n + 1) + ": " + plants[n].getID() + ", " + plants[n].getName() + ", " + ((Flower)plants[n]).getColor() + ", " + ((Flower)plants[n]).getSmell() + ", Thorny: " + ((Flower)plants[n]).getThorns() + "\n");
                            }
                            else if(plants[n] instanceof Fungus){
                            displayArraytemp2 = ((n + 1) + ": " + plants[n].getID() + ", " + plants[n].getName() + ", " + ((Fungus)plants[n]).getColor() + ", Poisonous: " + ((Fungus)plants[n]).getPoisonous() + "\n");
                            }
                            else if(plants[n] instanceof Weed){
                            displayArraytemp2 = ((n + 1) + ": " + plants[n].getID() + ", " + plants[n].getName() + ", " + ((Weed)plants[n]).getColor() + ", Edible: " + ((Weed)plants[n]).getEdible() + ", Medicinal: " + ((Weed)plants[n]).getMedicinal() + ", Poisonous: " + ((Weed)plants[n]).getPoisonous() + "\n");
                            }
                            else if(plants[n] instanceof Herb){
                            displayArraytemp2 = ((n + 1) + ": " + plants[n].getID() + ", " + plants[n].getName() + ", " + ((Herb)plants[n]).getColor() + ", " + ((Herb)plants[n]).getFlavor() + ", Medicinal: " + ((Herb)plants[n]).getMedicinal() + ", Poisonous: " + ((Herb)plants[n]).getSeasonal() + "\n");
                            }
                        sb.append("<html>" + displayArraytemp2).
                            append("<br> ");
                        displayArray = sb.toString();
                    }
                }
            }

            displayEntered.setText(" ");
            displayEntered.setText(displayArray);
        }
        }
    }};

1 个答案:

答案 0 :(得分:1)

您真正的问题是您正在重新使用sb而不清除它。