如何将 JTextField 和 JCheckBox 添加到 ArrayList Object

How to Add JTextField & JCheckBox to ArrayList Object

我正在开发我的第一个 GUI 应用程序。

我有 Parent Class PlantArrayList 其中我有四个 child 类.

我有一些 JTextField 的一些信息和一些 JCheckBox 的其他信息...

JTextField应该转换成StringJCheckBox应该转换成boolean

我需要将所有这些组件添加到 ArrayList,然后显示 Plant 的列表。

我该怎么做?

示例:

public class Flower extends Plant{

   private boolean thorns;
   private boolean smell;   

   public Flower(String name, String id, String color, boolean blnThorns, boolean blnSmell){
      super(name, id, color);
      thorns = blnThorns;
      smell = blnSmell;

   }

   public boolean isThorns(){

      return thorns;
   }

   public void setThorns(boolean blnThorns){
      thorns = blnThorns;  
   }

   public boolean isSmell(){

      return smell;
   }

   public void setSmell(boolean blnSmell){
      smell = blnSmell;  
   }


}

这是我的 ArrayList

ArrayList<Plant> plantList = new ArrayList<Plant>();

这里我尝试为Flower添加参数:

private void addFlower(ArrayList<Plant> plantList){

    //Adding window
    JFrame addingFrame = new JFrame();
    addingFrame.setTitle("Plant Database Interface");
    addingFrame.setSize(600, 200);
    addingFrame.setLocationRelativeTo(null);
    addingFrame.setVisible(true);

    //ADDING FRAME LAYOUT
    addingFrame.setLayout(new BorderLayout());

    //TRAITS
    JPanel fields = new JPanel();
    addingFrame.add(fields, BorderLayout.NORTH);

    JLabel nameLabel = new JLabel("Name:");      
    JTextField  nameField = new JTextField(15);
    String name = nameField.getText();


    JLabel idLabel = new JLabel("ID:");      
    JTextField  idField  = new JTextField(10);
    String id = idField.getText();

    JLabel colorLabel = new JLabel("Color:");      
    JTextField  colorField = new JTextField(10);
    String color = colorField.getText();

    fields.add(nameLabel);
    fields.add(nameField);

    fields.add(idLabel);
    fields.add(idField);

    fields.add(colorLabel);
    fields.add(colorField);

    JPanel traits = new JPanel();
    addingFrame.add(traits , BorderLayout.CENTER );

    JCheckBox thornsBox = new JCheckBox("Thorns Present");
    thornsBox.setSelected(false);
    traits.add(thornsBox);

    JCheckBox smellBox = new JCheckBox("Smell Present");
    smellBox.setSelected(false);
    traits.add(smellBox);

    JPanel southPanel = new JPanel();
    addingFrame.add(southPanel, BorderLayout.SOUTH);

    JButton addPlantBtn = new JButton("Add Plant");
    southPanel.add(addPlantBtn, BorderLayout.EAST);

    JButton cancelBtn = new JButton("Cancel");
    southPanel.add(cancelBtn, BorderLayout.WEST);

    boolean blnThorns;
    boolean blnSmell;

    //I REALLY DON'T KNOW HOW TO DO THIS PART
    thornsBox.addItemListener(new ItemListener(){
        public void itemStateChanged(ItemEvent e){
            if(e.getStateChange() == ItemEvent.SELECTED){
                blnThorns =true;
            }
            else{
                blnThorns = false;
            }
        }
    });

    smellBox.addItemListener(new ItemListener(){
        public void itemStateChanged(ItemEvent e){
            if(e.getStateChange() == ItemEvent.SELECTED){
                blnSmell =true;
            }
            else{
                blnSmell = false;
            }
        }
    });               

    addPlantBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //plantList.add(new Flower(name, id, color, blnThorns, blnSmell)); //HOW TO DO????................
        }
    });


    cancelBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            addingFrame.dispatchEvent(new WindowEvent(addingFrame, WindowEvent.WINDOW_CLOSING));
        }
    });
}

使用您的 addPlantBtn ActionListener 在调用时收集您需要的信息

addPlantBtn.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
        Flower flower = new Flower(nameField.getText(), idField.getText(), colorField.getText(), thornsBox.isSelected(), smellBox.isSelected());
        plantList.add(flower);

如果您创建了专门的 JPanel,旨在收集用户详细信息并在您请求时生成 Flower(根据字段)。然后,您可以在需要时在某种对话框中使用此面板。

查看 How to Make Dialogs 了解更多详情

例如...

public class FlowerPane extends JPanel {

    JTextField nameField = new JTextField(15);
    JTextField idField = new JTextField(10);
    JTextField colorField = new JTextField(10);
    JCheckBox smellBox = new JCheckBox("Smell Present");
    JCheckBox thornsBox = new JCheckBox("Thorns Present");

    public FlowerPane() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(2, 2, 2, 2);

        JLabel nameLabel = new JLabel("Name:");
        JLabel idLabel = new JLabel("ID:");
        JLabel colorLabel = new JLabel("Color:");

        add(nameLabel, gbc);
        gbc.gridy++;
        add(idLabel, gbc);
        gbc.gridy++;
        add(idLabel, gbc);

        gbc.gridx++;
        gbc.gridy = 0;
        add(nameField, gbc);
        gbc.gridy++;
        add(idField, gbc);
        gbc.gridy++;
        add(colorField, gbc);

        gbc.gridx = 0;
        gbc.gridwidth = GridBagConstraints.REMAINDER;

        add(thornsBox, gbc);
        gbc.gridy++;
        add(smellBox, gbc);
    }

    public Flower create() {
        return new Flower(nameField.getText(), idField.getText(), colorField.getText(), thornsBox.isSelected(), smellBox.isSelected());
    }

}

然后您可以通过执行以下操作来使用它...

FlowerPane flowerPane = new FlowerPane();
switch (JOptionPane.showConfirmDialog(null, flowerPane, "Flower", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)) {
    case JOptionPane.OK_OPTION:
        Flower flower = flowerPane.create();
        plantList.add(flower);
        break;
}

或将其添加到其他容器