如何在 JList 上输出文本输入和组合框 (java)

How do I output the text input and combobox on JList (java)

import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JList;

public class PetUI extends JFrame implements ActionListener{
JButton btnAdd = new JButton("Add");
JButton btnCancel = new JButton("Cancel");
JTextField txtbox1 = new JTextField();
JTextField txtbox2 = new JTextField();
public PetUI(){
  super("Pet Shop");
  JPanel panel1 = new JPanel();
  JPanel panel2 = new JPanel();

  //inputs
  panel1.setLayout(new GridLayout(3,4));
  add(panel1,BorderLayout.NORTH);
  panel1.add(new JLabel("Pet Type: "));
  String [] animals = {"Cat","Dog","Rabbit"};
  JComboBox petlist = new JComboBox(animals);
  panel1.add(petlist);
  panel1.add(new JLabel("Breed: "));
  panel1.add(txtbox1);
  panel1.add(new JLabel("Name: "));
  panel1.add(txtbox2);

  //buttons
  add(panel2,BorderLayout.CENTER);
  panel2.add(btnAdd);
  btnAdd.addActionListener(this);
  panel2.add(btnCancel);      

  setVisible(true);
  setSize(300,300);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
 }
 public void actionPerformed(ActionEvent e){

 }
 public static void main(String [] args){
  new PetUI();
 }
}

我该怎么做?将其输出到 JList。 我的老师没有教给我们这个。 我试图在网上搜索它,但没有成功。 我已经通过了很多 YT 教程,但没有成功。

欢迎来到 SO。这是让您入门的非常基本的东西:

public class PetUI extends JFrame implements ActionListener{

    JButton btnAdd = new JButton("Add");
    JButton btnCancel = new JButton("Cancel");
    JTextField txtbox1 = new JTextField();
    JTextField txtbox2 = new JTextField();
    JList<String> list;
    DefaultListModel<String> listModel;
    JComboBox<String> petList;

    public PetUI(){

        super("Pet Shop");

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();

        //inputs
        panel1.setLayout(new GridLayout(3,4));
        add(panel1,BorderLayout.NORTH);
        panel1.add(new JLabel("Pet Type: "));
        String [] animals = {"Cat","Dog","Rabbit"};
        petList = new JComboBox<>(animals);
        panel1.add(petList);
        panel1.add(new JLabel("Breed: "));
        panel1.add(txtbox1);
        panel1.add(new JLabel("Name: "));
        panel1.add(txtbox2);

        //buttons
        add(panel2,BorderLayout.CENTER);
        panel2.add(btnAdd);
        btnAdd.addActionListener(this);
        panel2.add(btnCancel);

        //JList
        listModel = new DefaultListModel<>();

        list = new JList<>(listModel);
        list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        list.setVisibleRowCount(-1);

        JPanel panel3 = new JPanel();
        panel3.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel3.add(list);
        add(panel3,BorderLayout.SOUTH);

        setVisible(true);
        setSize(300,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    @Override
    public void actionPerformed(ActionEvent e){

        StringBuilder sb = new StringBuilder((String)petList.getSelectedItem());
        sb.append(" ")  //add space
          .append(txtbox1.getText())
          .append(" ")
          .append(txtbox2.getText());

        listModel.addElement(sb.toString());
    }
    public static void main(String [] args){
        new PetUI();
    }
}