Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java arrayList中的项从面板中删除,但不从实际arrayList中删除?_Java_Arraylist_Jcombobox - Fatal编程技术网

Java arrayList中的项从面板中删除,但不从实际arrayList中删除?

Java arrayList中的项从面板中删除,但不从实际arrayList中删除?,java,arraylist,jcombobox,Java,Arraylist,Jcombobox,以前我写过一个程序,可以添加用户,然后在用户名旁边列出复选框,然后你可以从中进行选择,然后按delete按钮删除用户名及其旁边的复选框现在,在构建和组合我的实际程序之后,我意识到这些名称实际上并没有从实际列表中删除,而只是从面板中删除(因此它只是变得不可见)。如何使它实际上从arraylist而不仅仅是面板中删除?还有没有办法使其不接受相同的用户名? 我的完整程序代码位于此处: 在这里,当我运行菜单窗口并单击管理用户时,我的ManageUsersGUI窗口会弹出,它允许我添加和删除用户。当我关闭

以前我写过一个程序,可以添加用户,然后在用户名旁边列出复选框,然后你可以从中进行选择,然后按delete按钮删除用户名及其旁边的复选框现在,在构建和组合我的实际程序之后,我意识到这些名称实际上并没有从实际列表中删除,而只是从面板中删除(因此它只是变得不可见)。如何使它实际上从arraylist而不仅仅是面板中删除?还有没有办法使其不接受相同的用户名?

我的完整程序代码位于此处:

在这里,当我运行菜单窗口并单击管理用户时,我的ManageUsersGUI窗口会弹出,它允许我添加和删除用户。当我关闭它并按下View Available Chores(查看可用的家务)时,它会打开另一个窗口,在该窗口中,我可以从JComboBox中选择某个用户查看家务。但它显示了所有重复的名字和我删除的名字。如果我返回并按下管理用户按钮,窗口将再次弹出,并且没有以前的用户列表。它再次为空,如果您添加相同的名称,它将允许您添加一次又一次,当您删除时,它实际上不会删除

以下是我的ManageUsersGUI代码:

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

public class ManageUsersGUI extends JFrame {
    public ArrayList<User> users;
    private JLabel title;
    private JLabel addNewUserLabel;
    private JTextField addNewUserTextField;
    private JLabel deleteUsersLabel;
    private JButton addButton;
    private JButton deleteButton;
    private JPanel namePanel;
    final int WINDOW_WIDTH = 500;
    final int WINDOW_HEIGHT = 400;


    public ManageUsersGUI() {
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

        //construct components
        title = new JLabel ("Manage Users");
        title.setFont(new Font("Serif", Font.PLAIN, 28));

        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 (500, 600));
        setLayout (null);

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

        //set component bounds (only needed by Absolute Positioning)
        title.setBounds (170, 10, 300, 100);
        addNewUserLabel.setBounds (65, 90, 120, 25);
        addNewUserTextField.setBounds (200, 90, 125, 25);
        deleteUsersLabel.setBounds (135, 120, 281, 25);
        addButton.setBounds (350, 90, 90, 25);
        namePanel.setBounds(225, 270, 140, 0);
        deleteButton.setBounds (200, 300, 100, 25);

        addButton.addActionListener(new AddButtonListener());

        deleteButton.addActionListener(new DeleteButtonListener());

        setVisible(true);
    }      

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

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

            JCheckBox nameCheckBox = new JCheckBox();
            nameCheckBox.setText(addNewUserTextField.getText());
            namePanel.add(nameCheckBox);
            namePanel.setBounds(225, 140, 140, namePanel.getHeight() + 25);
            deleteButton.setBounds(200, 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) {
            for(Component component : namePanel.getComponents()) {
               if(component instanceof JCheckBox) {
                  if(((JCheckBox)component).isSelected())
                     namePanel.remove(component);
                     for (User user: users) {
                       users.remove(user);
                     }
            }
            namePanel.revalidate();
            namePanel.repaint();
         }   
    }

    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 ManageUsersGUI());
        frame.pack();
        frame.setVisible (true);
    }
}
现在给我一个很长的错误:

         ----jGRASP exec: java MenuWindow

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ManageUsersGUI$DeleteButtonListener.actionPerformed(ManageUsersGUI.java:96)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
    at java.awt.Component.processMouseEvent(Component.java:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3322)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:702)
    at java.awt.EventQueue$3.run(EventQueue.java:696)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:724)
    at java.awt.EventQueue$4.run(EventQueue.java:722)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

 ----jGRASP: operation complete.

没有从arraylist中删除任何内容,因为您没有从arraylist中删除任何内容。我可以看到您正在从swing组件中删除组件,但从未从arraylist中删除

编辑:您发布了从arraylist中删除的代码,您正在从迭代的集合中删除,这总是个坏消息。 您应该使用迭代器,如下所示:

for (Iterator it = users.iterator(); it.hasNext();) {
    it.next();
    it.remove();
}
再次编辑:既然您似乎要从arraylist中删除所有内容,为什么不简单地使用

users.clear();

没有从arraylist中删除任何内容,因为您没有从arraylist中删除任何内容。我可以看到您正在从swing组件中删除组件,但从未从arraylist中删除

编辑:您发布了从arraylist中删除的代码,您正在从迭代的集合中删除,这总是个坏消息。 您应该使用迭代器,如下所示:

for (Iterator it = users.iterator(); it.hasNext();) {
    it.next();
    it.remove();
}
再次编辑:既然您似乎要从arraylist中删除所有内容,为什么不简单地使用

users.clear();

我看不到实际从arraylist中删除用户的代码。类似于
users.remove(index)
哪一行对应于第95行?(关于你的ManageUsersGUI.java)@Sparta
for(User-User:users){
我看不到你从数组列表中删除用户的代码。类似于
users.remove(index)
哪一行对应于第95行?(关于你的ManageUsersGUI.java)@Sparta
for(User-User:users){
这可以编译,但是当我在我的GitHub中运行第三段中描述的实际整个程序时,我得到了相同的错误。上面更新了。您发布的代码可以编译?它可以,但是您不能从基础集合中删除增强的for循环中的元素。或者,在我发布更新之前,您回答得太快了。@pyuntae you可以通过比较您感兴趣的字段手动写入它,或者如果使用IDE它将允许您生成它。对于用户对象,它可以是
@Override public boolean equals(object obj){if(this==obj)返回true;if(obj==null)返回false;if(getClass()!=obj.getClass())返回false;user other=(user)obj;if(userName==null){if(other.userName!=null)返回false;}else if(!userName.equals(other.userName))返回false;返回true;}
。请参阅dbrown93观察用户添加到“ChoreSetData”中的列表中在不同的服务器上删除(&D)list@pyuntae这是因为users对象从未在ManageUsersGUI类中初始化。请检查DeleteButtonListener类,我认为您应该为(Iterator It=users.Iterator();It.hasNext();){It.next();It.remove();}替换为
ChoreScoreData.getUserList().remove(新用户)((JCheckBox)组件);
如果目的是删除所选用户。这会编译,但当我在我的GitHub中运行第3段中描述的实际整个程序时,我会收到相同的错误。上面已更新。您发布的代码会编译?会,但您无法从基础集合中删除增强for循环中的元素。或者您也回答了fast,然后我才能发布更新。@pyuntae您可以通过比较感兴趣的字段手动编写它,或者如果使用IDE,它将允许您生成它。对于用户对象,它可以是
@Override public boolean equals(object obj){if(this==obj)返回true;if(obj==null)返回false;if(getClass()!=obj.getClass())返回false;User other=(User)obj;if(userName==null){if(other.userName!=null)返回false;}else if(!userName.equals(other.userName))返回false;return true;}
。请参阅dbrown93观察用户被添加到“ChoreSetData”中的列表中在不同的服务器上删除(&D)list@pyuntae这是因为users对象从未在ManageUsersGUI类中初始化。请检查DeleteButtonListener类,我认为您应该为(Iterator It=users.Iterator();It.hasNext();){It.next();It.remove();}替换为
ChoreScoreData.getUserList().remove(新用户)((JCheckBox)组件)。getText());
如果目的是删除所选用户。