所以我有一个按钮,它应该在我的摇摆 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");
}
1)为什么你添加了一个新的JButton
,JComboBox
到另一个,从ActionListener
派生JButton
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后,简短,可运行,可编译