java - 将未知数量的 JButton 添加到 JScrollPane

标签 java swing jbutton jscrollpane

我(几乎)到处寻找(特别是herehere)来寻找我的问题的答案。我正在尝试根据 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() 。它几乎就藏在 StackOverflow here 中。 .

在阅读了 validate() 之间的区别之后、revalidate()invalidate()(这没用)here ,我决定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 的尺寸和类似信息,并确认他们可以继续。唯一的问题是这会很耗时。换句话说,它可能会导致性能问题。

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

关于java - 将未知数量的 JButton 添加到 JScrollPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28263935/

相关文章:

java - 什么是 Java 中的参数多态性(附示例)?

java - 如何为带圆角的 JDialog 设置 3D 边框?

java - hibernate 异常: transaction roll back failed

java - 平台碰撞和跳跃的奇怪行为

java - 使按钮不可调整大小

java - 初级 Java - 类编译器错误

java - 如何创建一个 Observable 来在完成后对 Observable 流进行 Margin 处理?

java - 在程序中删除抽象类的对象

java - 如何通过单击另一个按钮java来执行按钮?

java - 如何正确格式化 ActionEvent 以便 JButton 正常工作