1

所以我有一个按钮,假设向我的摇摆 gui 添加一个(子)比赛选项以创建混合字符实际更新类和统计值的代码在控制台中工作,但 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");
    }
4

1 回答 1

2

1)为什么你添加一个新的JButtonJComboBox另一个,JButton源自ActionListener

JButton cb = (JButton)e.getSource();

.
.

cb.add(raceBox[1]);

.

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

2)JButton错过任何LayoutManager,然后一个新的JButtonand JComboBox(添加到 derived JButton)有Dimmension[0, 0]

3)为了获得更好的帮助,请尽快发布SSCCE,简短,可运行,可编译

于 2013-02-01T19:43:34.810 回答