首页 > 解决方案 > ArrayList 中的 JTextField 仅显示最后一个

问题描述

amount = new JTextField(40);
        amount.setFont(new Font("Arial", Font.PLAIN, 20));
        amount.setBounds(_width / 2  + 25, (_height / 5) * 2 - (_height / 5) / 4, 50, 50);
        content.add(amount);

        urlBox = new JTextField(40);
        ArrayList<JTextField> urlBoxes = new ArrayList<JTextField>();

        JButton generate = new JButton("Generate");
        generate.setFont(new Font("Arial", Font.PLAIN, 20));
        generate.setBounds(_width / 2 + 25 + 55, (_height / 5) * 2 - (_height / 5) / 4, 200, 50);
        generate.setBackground(Color.gray);
        generate.setForeground(Color.white);
        generate.setFocusPainted(false);
        generate.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                urlBoxes.clear();

                for(int i = 0; i < Integer.parseInt(amount.getText()); i++) {
                    urlBoxes.add(urlBox);
                    urlBoxes.get(i).setFont(new Font("arial", Font.PLAIN, 20));

                }

                for(int i = 1; i < Integer.parseInt(amount.getText()) + 1; i++) {
                    urlBoxes.get(i - 1).setBounds(20, 3 * _height / 10 + 55 * i, _width / 2, 50);
                    content.add(urlBoxes.get(i - 1));
                    urlBoxes.get(i - 1).setVisible(true);
                    System.out.println("Added at " + urlBoxes.get(i - 1).getBounds().y);
                }

            }

        });
        content.add(generate);

    }

根据System.out.println("Added at " + urlBoxes.get(i - 1).getBounds().y); 我认为他们已经创建并放置在正确的位置,但他们只是没有显示

标签: javaswingarraylistjtextfield

解决方案


推荐阅读