组件不会从ArrayList中显示

时间:2014-08-06 09:36:19

标签: java swing

我正在创建一个创建项目的类。一个项目由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<Item>

public class minimumExample extends JFrame {

    private JButton addItem;

    private JComboBox itemBox;

    private String[] itemSelect = { "test1", "test2" };

    private JPanel addUpperPane;

    private JPanel addLowerPane;

    private ArrayList<Item> displayedItems = new ArrayList<Item>();

    public void createControlPane() {

        addUpperPane = new JPanel();
        addLowerPane = new JPanel(new GridLayout(0, 1));

        addItem = new JButton("Add item");

        itemBox = new JComboBox(itemSelect);

        addItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                getContentPane().setLayout(new GridLayout(0, 1));

                if(itemBox.getSelectedItem().toString().equals("test1")) {
                    displayedItems.add(new Item( new JButton("Test1"), "test1"));               
                    validate();
                    repaint();
                }

                if(itemBox.getSelectedItem().toString().equals("test2")) {
                    displayedItems.add(new Item( new JLabel("Test2"), "test2"));    
                    validate();
                    repaint();
                }

            }
        });

        for (int i = 0; i < displayedItems.size(); i++) {
            addLowerPane.add(displayedItems.get(i).getComponent());
            validate();
            repaint();
        }

        addUpperPane.add(itemBox, BorderLayout.EAST);
        addUpperPane.add(addItem, BorderLayout.WEST);
        addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));

        //put everything together

        add(addUpperPane, BorderLayout.NORTH);
        add(addLowerPane, BorderLayout.SOUTH);

        repaint();

    }

    private void makeLayout() {

        setTitle("Test App");
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(1000, 500));

        createControlPane();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);

    }


    /**
     * starts the GUI
     */
    public void start() {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                makeLayout();   
            }
        });
    }

    public static void main(String[] args) throws IOException {
        minimumExample ex = new minimumExample();
        ex.start();
    }

}

我的问题没有显示出来。任何建议为什么会这样?我非常感谢你的回答!

1 个答案:

答案 0 :(得分:2)

您刚刚在列表中添加了该组件,但未在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();
        }

    }
});

了解更多How does an ActionListener work?