有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

当我有用户输入数据并按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);

上面的代码用于在用户将数据输入文本字段和组合框后按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");
        }
    }

下面是创建标签的其余部分,包括main方法

    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) 个答案

  1. # 1 楼答案

    我觉得这很可疑:

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

    如果您希望此ActionListener对按下enter按钮作出响应,则此操作将失败,因为actionCommand字符串不是“enter”。我甚至不确定它将是什么,也不在乎,因为我通常对每个组件使用ActionListener,所以通常不测试actionCommand字符串

    至于你凌乱的数组代码,考虑一下,给你的花一个体面的^ {< CD1>}方法或方法,返回一个可以显示的有用字符串。这样,您就可以摆脱所有这些instanceof操作,并获得更简单、更小的代码


    编辑

    我应该闭嘴读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);
       }
    }