我怎样才能让我的删除按钮真正删除名称和它旁边的复选框?

How can I get my delete button to actually delete the name and the checkbox next to it?

我正在尝试让我的程序让用户选中旁边带有名称的复选框,然后在按下删除按钮时能够删除整行(从 arrayList)。 我的程序看起来像这样:

这是我的代码:

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 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());

        deleteButton.addActionListener(new DeleteButtonListener());


    }

    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();
        }
    }

    private class DeleteButtonListener implements ActionListener {
         @Override
         public void actionPerformed(ActionEvent e) {
            ///STUCK HERE///   
         }
    }      


    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);
    }
}

添加用户代码:

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";
   }

}     

如果您可以成功地将 JComponents 添加到屏幕,那么以下内容应该会有所帮助:

 add(myComponent);
 //your component should be there, if not you may call 
 /*repaint()*/
 //then when you want to remove, call
 remove(myComponent);
 //remember that you need to actually properly set the text for the JComboBox items.

在伪代码中

  1. 遍历所有 children 一次获取一个 child 的引用
  2. 检查是否 child 是 JCheckBox 的实例
  3. 如果是,请确认检查 方框 child 已勾选(选中)
  4. 如果是调用namePanel.remove(child);
  5. 完成删除所有符合条件的操作后 children,调用 revalidate();repaint();(按此顺序) 名称面板以确保 UI 正确刷新

这是我完成的代码:

private class DeleteButtonListener implements ActionListener {
     @Override
     public void actionPerformed(ActionEvent e) {
        for(Component component : namePanel.getComponents()) {
           if(component instanceof JCheckBox) {
              if(((JCheckBox)component).isSelected())
                 namePanel.remove(component);
           }
        }
        namePanel.revalidate();
        namePanel.repaint();
     }   
}