JFrame:通过按钮删除JTextField?

问题描述 投票:0回答:1

我的GUI有一个按钮,允许我将新的JTextField放入arraylist,然后将其放在我的GUI上。我还想创建另一个按钮,以删除arraylist上的最后一个JTextField。我的“删除”按钮的ActionListener具有以下代码

   private class Remover implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            remove(listOfTextFields.get(listOfTextFields.size()));
            pack();
            invalidate();
            repaint();
        }
    }

我希望它只是删除JTextField的最后一个实例,但是我得到一个IndexOutofBoundsException,它的索引#超出了长度#的范围。 #但是我添加了许多JTextField。我以为我会陷入困境,它将删除我的arraylist上的最后一个JTextField,但我想我的逻辑是不正确的。

我尝试写下remove(listOfTextFields.get(listOfTextFields.size()-1));,这使我可以删除一个框,但是我不能一直单击“删除”按钮来做更多的事情。我想念什么?

这是完整的代码供您参考:

public class TheGUI extends JFrame {
    List<JTextField> listOfTextFields = new ArrayList<JTextField>();
    List<String> wordsToChoose = new ArrayList<String>();

    private JTextField instruct;
    private JButton submit;
    private JButton addNew;
    private TheGUI frame;
    private Random r = new Random();
    private JButton remove;

    public TheGUI() { //My GUI with the default fields & buttons that should be on there.
        super("Chili");
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        instruct = new JTextField("Choose your words");
        instruct.setEditable(false);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 0;
        add(instruct, c);

        addNew = new JButton("Add");
        c.weightx = 0.0;
        c.gridx = 1;
        c.gridy = 0;
        add(addNew, c);


        remove = new JButton("Remove");
        c.weightx = 0.0;
        c.gridx= 2;
        c.gridy = 0;
        add(remove, c);

        submit = new JButton("Submit!");
        c.weightx = 0.5;
        c.gridwidth = 3;
        c.gridx = 0;
        c.gridy = GridBagConstraints.PAGE_END;
        add(submit, c);

        addNew.addActionListener(new Adder());
        submit.addActionListener(new Randomizer());
        remove.addActionListener(new Remover());

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 300);
        setVisible(true);

    }

    private class Adder implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {

            JTextField textfield = new JTextField();

            listOfTextFields.add(textfield);
            GridBagConstraints textFieldConstraints = new GridBagConstraints();
            textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
            textFieldConstraints.weightx = 0.5;
            textFieldConstraints.gridx = 0;
            textFieldConstraints.gridwidth = 4;
            textFieldConstraints.gridy = listOfTextFields.size();

            add(textfield, textFieldConstraints);
            pack();
            revalidate();
            repaint();

        }
    }

    private class Remover implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            remove(listOfTextFields.get(listOfTextFields.size()));
            pack();
            invalidate();
            repaint();
        }
    }
java user-interface dynamic jframe jpanel
1个回答
0
投票

您已经忘记从卸妆器的列表中移除JTextField。这样,他希望每次您再次单击时都删除相同的元素,但该元素已不再在GUI中,仅在列表中。

IndexOutOfBoundsException来自列表的索引从0开始的事实。例如,如果列表包含一个值,则.size()返回1。因此,使用.size() - 1的方法已经正确。

尝试此实现,它应该可以工作。

private class Remover implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        remove(listOfTextFields.get(listOfTextFields.size()-1));
        listOfTextFields.remove(listOfTextFields.size()-1)
        pack();
        invalidate();
        repaint();
    }
} 
© www.soinside.com 2019 - 2024. All rights reserved.