如何在已从 JTextField 输入到数组列表中的每个名称旁边添加 JCheckBox?

How do I add JCheckBox next to each name that has been entered into an arraylist from a JTextField?

我正在创建一个如下所示的 ManageUsers GUI:

我希望复选框位于从“添加新用户”部分输入的列表中的名称旁边。我知道其中大部分尚未完成,但这是我到目前为止:

添加用户

public class AddUsers {

   private String userName;

   public AddUsers(String userName) {
      this.userName = userName;
   }

   public AddUsers() {
      userName = "";
   }   

   public void setUserName(String userName) {
      this.userName = userName;   
   }

   public String getUserName() {
      return userName;
   }

   public String toString() {
      return userName + "\n";
   }

}      

管理用户GUI

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.awt.TextField;


public class ManageUsersGUI1 extends JPanel {
    public static ArrayList<AddUsers> users = new ArrayList<>();

    private JLabel addNewUserLabel;
    private JTextField addNewUserTextField;
    private JLabel deleteUsersLabel;
    private JCheckBox jcomp4;
    private JCheckBox jcomp5;
    private JCheckBox jcomp6;
    private JButton addButton;
    private JButton deleteButton;

    public ManageUsersGUI1() {         
        //construct components
        addNewUserLabel = new JLabel ("Add new User here:");
        addNewUserTextField = new JTextField (0);
        deleteUsersLabel = new JLabel ("Select which User(s) you would like to delete:");
        jcomp4 = new JCheckBox ("newCheckBox");
        jcomp5 = new JCheckBox ("newCheckBox");
        jcomp6 = new JCheckBox ("newCheckBox");
        addButton = new JButton ("Add");
        deleteButton = new JButton ("Delete");

        //set components properties
        addNewUserTextField.setToolTipText ("Enter name and click on Add button.");
        addButton.setToolTipText ("Click here to Add new user.");
        deleteButton.setToolTipText ("Click here to delete User(s) selected.");

        //adjust size and set layout
        setPreferredSize (new Dimension (580, 485));
        setLayout (null);

        //add components
        add (addNewUserLabel);
        add (addNewUserTextField);
        add (deleteUsersLabel);
        add (jcomp4);
        add (jcomp5);
        add (jcomp6);
        add (addButton);
        add (deleteButton);

        //set component bounds (only needed by Absolute Positioning)
        addNewUserLabel.setBounds (85, 130, 120, 25);
        addNewUserTextField.setBounds (235, 130, 125, 25);
        deleteUsersLabel.setBounds (135, 225, 281, 25);
        jcomp4.setBounds (225, 270, 140, 25);
        jcomp5.setBounds (225, 300, 140, 25);
        jcomp6.setBounds (225, 330, 140, 25);
        addButton.setBounds (385, 130, 100, 25);
        deleteButton.setBounds (230, 410, 100, 25);

        addButton.addActionListener(new AddButtonListener());

    }

    private class AddButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
           String text = addNewUserTextField.getText();
           users.add(new AddUsers(text));

           // Display the charges.
           JOptionPane.showMessageDialog(null, text + " has been added.");
        }
   }


    public static void main (String[] args) {
        JFrame frame = new JFrame ("AddUsersPanel1");
        frame.setTitle("Manage Users");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new ManageUsersGUI1());
        frame.pack();
        frame.setVisible (true);
    }
}

您可以使用 JPanelBoxLayout 作为布局管理器。

这样,对于在 JTextField 中输入的每个新名称,您可以向 JPanel 添加一个新的 JCheckBox,它会自动按垂直顺序列出它们。

我已经对你的代码做了一些修改来举例说明,所以你可以根据你的需要进行调整:

public class ManageUsersGUI1 extends JPanel {
    public static ArrayList<AddUsers> users = new ArrayList<>();

    private JLabel addNewUserLabel;
    private JTextField addNewUserTextField;
    private JLabel deleteUsersLabel;
    private JButton addButton;
    private JButton deleteButton;
    private JPanel namePanel;

    public ManageUsersGUI1() {
        //construct components
        addNewUserLabel = new JLabel ("Add new User here:");
        addNewUserTextField = new JTextField (0);
        deleteUsersLabel = new JLabel ("Select which User(s) you would like to delete:");
        addButton = new JButton ("Add");
        deleteButton = new JButton ("Delete");
        namePanel = new JPanel();
        namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.Y_AXIS));

        //set components properties
        addNewUserTextField.setToolTipText ("Enter name and click on Add button.");
        addButton.setToolTipText ("Click here to Add new user.");
        deleteButton.setToolTipText ("Click here to delete User(s) selected.");

        //adjust size and set layout
        setPreferredSize (new Dimension (580, 485));
        setLayout (null);

        //add components
        add (addNewUserLabel);
        add (addNewUserTextField);
        add (deleteUsersLabel);
        add (namePanel);
        add (addButton);
        add (deleteButton);

        //set component bounds (only needed by Absolute Positioning)
        addNewUserLabel.setBounds (85, 130, 120, 25);
        addNewUserTextField.setBounds (235, 130, 125, 25);
        deleteUsersLabel.setBounds (135, 225, 281, 25);
        addButton.setBounds (385, 130, 100, 25);
        namePanel.setBounds(225, 270, 140, 0);
        deleteButton.setBounds (230, 335, 100, 25);

        addButton.addActionListener(new AddButtonListener());

    }

    private class AddButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String text = addNewUserTextField.getText();
            users.add(new AddUsers(text));

            // Display the charges.
            JOptionPane.showMessageDialog(null, text + " has been added.");

            JCheckBox nameCheckBox = new JCheckBox();
            nameCheckBox.setText(addNewUserTextField.getText());
            namePanel.add(nameCheckBox);
            namePanel.setBounds(225, 270, 140, namePanel.getHeight() + 25);
            deleteButton.setBounds(230, deleteButton.getY() + 25, 100, 25);
            JFrame frame = (JFrame) getRootPane().getParent();
            frame.setSize(frame.getWidth(), frame.getHeight() + 25);
            frame.pack();
        }
    }


    public static void main (String[] args) {
        JFrame frame = new JFrame ("AddUsersPanel1");
        frame.setTitle("Manage Users");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new ManageUsersGUI1());
        frame.pack();
        frame.setVisible (true);
    }
}