将未知数量的 JButton 添加到 JScrollPane

Adding unknown number of JButton to a JScrollPane

我(几乎)到处(特别是here and here)寻找问题的答案。我正在尝试根据 JComboBox 的选择将未知数量的 JButton 添加到 JScrollPane。我所知道的是你必须将元素添加到 JPanel 才能在你的 GUI 中获得它们。

这是一个示例代码: (没用),就是一个例子)

public class test {

ArrayList<JButton> button = new ArrayList<JButton>();
String[] stringArray = {"Goat", "Cow", "Dog", "Cat", "Human"};
JComboBox comboBox = new JComboBox(stringArray);
JPanel containerPanel = new JPanel();
JScrollPane scroller = new JScrollPane(containerPanel);

public void addButton(String btnName) {
    button.add(new JButton(btnName));
    containerPanel.add(button.get(button.size() - 1));
}

public class Action implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        addButton(comboBox.getSelectedItem().toString());
    }
}

}

我想根据用户的需要添加任意数量的 JButton

如果我像这样添加它们:

containerPanel.add(new JButton("test"));

它有效,但这不是我想要实现的。

你能帮帮我吗?

编辑: ___________________________________________________________________________

这是一些编译代码,是我正在尝试做的事情的简化副本:

public class Frame extends JFrame {

String[] list = {"Human", "Goat", "Dog", "Cat", "Duck"};
ArrayList<JButton> button = new ArrayList<JButton>();
JComboBox cBox = new JComboBox(list);
JPanel container = new JPanel();
JScrollPane scroller = new JScrollPane(container);

public Frame() {
    cBox.addActionListener(new Action());
    this.setLayout(new BorderLayout());
    this.setSize(200, 200);
    this.add(cBox, BorderLayout.SOUTH);
    this.add(scroller, BorderLayout.CENTER);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);      
}

public void createBtn(String s) {
    System.out.println("Button's label : " + s);
    button.add(new JButton(s));
    System.out.println("Button's index : " + (button.size() - 1));
    container.add(button.get(button.size() - 1));
}

public class Action implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Action made");
        JComboBox temp = (JComboBox) e.getSource();
        createBtn(temp.getSelectedItem().toString());
    }
}
}

所以我刚刚发现了一个刷新 JPanel 的非常简洁的函数:validate(). It was hiding almost right here in Whosebug here.

阅读了一些关于 validate() , revalidate() and invalidate()(That is not useful) here, I decided that validate() 之间的区别后,我需要的就是这个。

只需在 createBtn(String s) 中添加此功能,我就能让我所有的 JButton 出现。

新代码如下所示:

public class Frame extends JFrame {

    String[] list = {"Human", "Goat", "Dog", "Cat", "Duck"};
    ArrayList<JButton> button = new ArrayList<JButton>();
    JComboBox cBox = new JComboBox(list);
    JPanel container = new JPanel();
    JScrollPane scroller = new JScrollPane(container);

    public Frame() {
        cBox.addActionListener(new Action());
        this.setLayout(new BorderLayout());
        this.setSize(200, 200);
        this.add(cBox, BorderLayout.SOUTH);
        this.add(scroller, BorderLayout.CENTER);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);      
    }

    public void createBtn(String s) {
        System.out.println("Button's label : " + s);
        button.add(new JButton(s));
        System.out.println("Button's index : " + (button.size() - 1));
        container.add(button.get(button.size() - 1));

        //Note the new addition
        container.validate();
    }

    public class Action implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Action made");
            JComboBox temp = (JComboBox) e.getSource();
            createBtn(temp.getSelectedItem().toString());
        }
    }
}

据我了解,validate() 询问所有 child 他们的大小和类似的东西,并确认他们可以继续。唯一的问题是它会不会很耗时。换句话说,它可能会导致性能问题。

如果你有更好的答案,请纠正我。