调整字符串后,框架不会重新绘制

时间:2013-11-29 21:14:18

标签: java arrays swing actionlistener repaint

当我有用户输入数据并按Enter键调整设置为在框架上显示的字符串时,我不确定我的框架是否出错。我只是要包含我觉得适用的代码,因为整个代码很长,但如果有人想看到更多的东西,请告诉我,我可以发布更多内容。谢谢你的帮助!

     //adds the Flower data to the Array and list
 ActionListener flowerAddAction = new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent flowerAddAction){
        if(flowerAddAction.getActionCommand().equals("Enter")){
            Name = NameTxt2.getText();
            Colors = ColorTxt2.getText();
            Smell = SmellTxt.getText(); 
            ID = (int) IDCmbo.getSelectedItem();
            if(((String) ThornCmbo.getSelectedItem()).equals("Yes"))
                Thorns = true; 
            else
                Thorns = false;
            plants[count] = new Flower(Name, ID, Colors, Smell, Thorns);
            displayEntered.setText(displayArray);
            count++;
            frame.repaint();
            frameB.setVisible(false);
        }
        }
};
 enterFlrData.addActionListener(flowerAddAction);

上面的代码是将操作添加到用户在将数据输入textFields和ComboBoxes后按Enter键时。下面创建一个由输入创建的数组的长字符串。 (如果有人有更好的方法在JLabel上显示数组,我很想知道,因为我知道这有点草率。

    //create a string of all values for the array
    displayArray = " ";
    String displayArraytemp = " ";
    for(int n = 0; n < 25; n++){
        if(plants[n] != null){
            if(plants[n] instanceof Flower){
                displayArraytemp = (n + ": " + 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){
                displayArraytemp = (n + ": " + plants[n].getID() + ", " + plants[n].getName() + ", " + ((Fungus)plants[n]).getColor() + ", Poisonous: " + ((Fungus)plants[n]).getPoisonous() + "/n");
                }
                else if(plants[n] instanceof Weed){
                displayArraytemp = (n + ": " + 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){
                displayArraytemp = (n + ": " + 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");
                }
            displayArray += (displayArraytemp + "/n");
        }
    }

下面显示了创建标签的其余部分,并包含主要方法。

    final JPanel p2Base = new JPanel();
    displayEntered = new JLabel(displayArray);
     //entire constant GUI put together
p2Base.setLayout(new BorderLayout(10,10));
p2Base.add(menuBar, BorderLayout.NORTH);
p2Base.add(p1Right, BorderLayout.EAST);
p2Base.add(displayEntered, BorderLayout.WEST);

        public static void main(String[] args) {
    frame = new GUI();
    frame.setTitle("Plant Database");
    frame.setSize(900,700);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

1 个答案:

答案 0 :(得分:1)

这看起来很可疑:

flowerAddAction.getActionCommand().equals("Enter")

如果您希望此ActionListener响应按Enter按钮,那么这将失败,因为actionCommand String不会是“Enter”。我甚至不确定它会是什么,并且不关心,因为我通常对每个组件使用ActionListener,所以通常不测试actionCommand String。

至于你凌乱的数组代码,请考虑为你的花提供一个体面的toString()方法或类似想法的方法,它返回一个可以显示的有用字符串。这样你就可以摆脱所有这些操作的实例,并拥有更简单的更小代码。


修改

我应该闭嘴并阅读API。除非您明确设置,否则JTextField的action命令是它包含的文本。

import java.awt.event.*;
import javax.swing.*;

public class EnterActionCommand {
   public static void main(String[] args) {
      JTextField field1 = new JTextField(10);
      JTextField field2 = new JTextField(10);

      // **** set the action command explicitly for field2 ****
      field2.setActionCommand("Field 2");

      ActionListener actionListener = new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            System.out.printf("action command: \"%s\"%n", e.getActionCommand());
         }
      };

      field1.addActionListener(actionListener);
      field2.addActionListener(actionListener);

      JPanel panel = new JPanel();
      panel.add(new JLabel("Field 1:"));
      panel.add(field1);
      panel.add(new JLabel("Field 2:"));
      panel.add(field2);

      JOptionPane.showMessageDialog(null, panel);
   }
}