所以我有一个按钮,假设在我的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");
}
答案 0 :(得分:2)
1)为什么您从另一个JButton
JButton
来新增JComboBox
和ActionListener
JButton cb = (JButton)e.getSource();
.
.
cb.add(raceBox[1]);
.
cb.add(addRaceButton1);
cb.revalidate();
cb.repaint();
2)JButton
错过任何LayoutManager
,然后新的JButton
和JComboBox
(添加到派生的JButton
)有Dimmension[0, 0]
3)更快地发布SSCCE,简短,可运行,可编辑的