动态地将新的jcombobox和jbutton添加到层次结构中

时间:2013-02-01 19:22:45

标签: java swing panel jcombobox dynamic-programming

所以我有一个按钮,假设在我的swing gui中添加一个(子)竞赛选项来创建混合字符,实际更新类和stats值的代码在控制台中工作,但GUI方面现在证明是困难的代码确实动态地创建了新代码但是分层内置了修改父面板本身不需要的添加按钮。想法?

if (e.getActionCommand().equals("AddRaceBox")){
        JButton cb = (JButton)e.getSource();
        String[] cRace = new String[75];
        JComboBox[] raceBox = new JComboBox[2];
        raceBox[1] = new JComboBox();
        for (int i=0; i<75; i++){
            cRace[i] = nala.getRaceName(i);
            raceBox[1].addItem(cRace[i]);
            }
        cb.add(raceBox[1]);

        ImageIcon deleteRaceIcon = createImageIcon("1359702329_delete_user.png", "add race icon");
        JButton addRaceButton1 = new JButton(deleteRaceIcon);
        addRaceButton1.addActionListener(this);
        addRaceButton1.setActionCommand("AddRaceBox");
        addRaceButton1.setSize(new Dimension(32, 32));          
        cb.add(addRaceButton1);
        cb.revalidate();
        cb.repaint();
        System.out.println("Add Race Clicked");
    }

1 个答案:

答案 0 :(得分:2)

1)为什么您从另一个JButton

派生了JButton来新增JComboBoxActionListener
JButton cb = (JButton)e.getSource();

.
.

cb.add(raceBox[1]);

.

cb.add(addRaceButton1);
cb.revalidate();
cb.repaint();

2)JButton错过任何LayoutManager,然后新的JButtonJComboBox(添加到派生的JButton)有Dimmension[0, 0]

3)更快地发布SSCCE,简短,可运行,可编辑的