【问题标题】:Adding unknown number of JButton to a JScrollPane将未知数量的 JButton 添加到 JScrollPane
【发布时间】:2015-02-01 14:45:12
【问题描述】:

我已经(几乎)到处寻找(herehere 特别是)以找到我的问题的答案。我正在尝试将未知数量的JButton 添加到JScrollPane,具体取决于JComboBox 的选择。我所知道的是,您必须将元素添加到 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());
    }
}
}

【问题讨论】:

  • 您列出了this question,但您却莫名其妙地忽略了使用合适布局(例如GridLayout)的答案-为什么?为什么不将 containerPanel 的布局设置为 GridLayout 并在其中添加按钮?
  • Ehm 只是使用循环添加任意数量的按钮?
  • @HovercraftFullOfEels 我尝试使用BoxLayoutFlowLayout,但都没有。
  • @MuratK。问题是我不知道会有多少个按钮
  • Chax:除非我看到它,否则不要相信它——所以告诉我们。为了我的钱,我会使用 GridLayout,我会在连接到 JComboBox 的侦听器中的 for 循环中添加我的按钮。添加后,我会在按钮的容器上调用 revalidate 和 repaint,然后我就完成了。是的,你确实知道会有多少按钮。Comb 框会告诉你有多少项目已选中!

标签: java swing jbutton jscrollpane


【解决方案1】:

所以我刚刚破坏了一个非常简洁的函数,它刷新了JPanelvalidate()。它几乎就隐藏在 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() 询问所有孩子的大小和类似的东西,并确认他们可以继续。唯一的问题是它可能很耗时。换句话说,它可能会导致性能问题。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 2012-11-07
    相关资源
    最近更新 更多