Java 组件不会从ArrayList中显示

Java 组件不会从ArrayList中显示,java,swing,Java,Swing,我有一个类可以创建一个项目。项目由一个JComponent和一个功能组成 public class Item { private JComponent component; private String functionality; /** * constructor */ public Item() { super(); } /** * @param component * @para

我有一个类可以创建一个项目。项目由一个JComponent和一个功能组成

public class Item {

    private JComponent component;
    private String functionality;

    /**
     * constructor
     */
    public Item() {
        super();
    }

    /**
     * @param component
     * @param functionality
     */
    public Item(JComponent component, String functionality) {
        super();
        this.component = component;
        this.functionality = functionality;
    }

    /**
     * @return the component
     */
    public JComponent getComponent() {
        return component;
    }
    /**
     * @param component the component to set
     */
    public void setComponent(JComponent component) {
        this.component = component;
    }
    /**
     * @return the functionality
     */
    public String getFunctionality() {
        return functionality;
    }
    /**
     * @param functionality the functionality to set
     */
    public void setFunctionality(String functionality) {
        this.functionality = functionality;
    }

}
在我的gui中,我只是将
JComponents
添加到
ArrayList

public类minimumExample扩展了JFrame{
私人按钮附加项;
私人JComboBox项目箱;
私有字符串[]itemSelect={“test1”、“test2”};
私人JPanel-addUpperPane;
私人JPanel addLowerPane;
private ArrayList displayedItems=new ArrayList();
public void createControlPane(){
addUpperPane=new JPanel();
addLowerPane=新的JPanel(新的网格布局(0,1));
addItem=新的JButton(“添加项”);
itemBox=新的JComboBox(itemSelect);
addItem.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
getContentPane().setLayout(新的GridLayout(0,1));
如果(itemBox.getSelectedItem().toString().equals(“test1”)){
add(新项(新JButton(“Test1”),“Test1”);
验证();
重新油漆();
}
if(itemBox.getSelectedItem().toString().equals(“test2”)){
添加(新项(新JLabel(“Test2”),“Test2”);
验证();
重新油漆();
}
}
});
对于(int i=0;i

我的问题是什么也看不出来。有什么建议吗?为什么会这样?我真的很感激你的回答

您刚刚将组件添加到列表中,但未添加到
addLowerPane
中。在动作侦听器中移动循环以在布局中添加项

示例代码:

addItem.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        ...
        // add item in the list.
        ...
        for (int i = 0; i < displayedItems.size(); i++) {
            addLowerPane.add(displayedItems.get(i).getComponent());
            validate();
            repaint();
        }

    }
});
addItem.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
...
//在列表中添加项目。
...
对于(int i=0;i

请阅读更多关于您答案的信息

Thx!只是为了理解?为什么我必须将其添加到actionListener中的下窗格中?我还真的不明白吗?单击按钮时,侦听器会调用,此时会调用方法
actionPerformed()
addItem.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        ...
        // add item in the list.
        ...
        for (int i = 0; i < displayedItems.size(); i++) {
            addLowerPane.add(displayedItems.get(i).getComponent());
            validate();
            repaint();
        }

    }
});