我没有分组我的jradiobuttons,以便用户可以选择多个选择,我可以存储在节点阵列..但它只能读取一次。代码有什么问题?请赐教为什么我的jradiobutton只能使用一次?
private String[] showGUIForNodeDeletion() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(map.size(), 1));
ButtonGroup btnGrp = new ButtonGroup();
final String nodes[] = new String[10];
Set<String> keySet = map.keySet();
for (String name : keySet) {
btnRadio = new JRadioButton(name);
btnRadio.setActionCommand(map.get(name).x + "," + map.get(name).y + "," + name);
//btnGrp.add(btnRadio);
panel.add(btnRadio);
}
btnRadio.addActionListener(new ActionListener() {
int x = 0;
public void actionPerformed(ActionEvent e) {
nodes[x] = ((JRadioButton) e.getSource()).getActionCommand();
System.out.println("Node counting " + x);
x++;
}
});
if (keySet.isEmpty()) {
JOptionPane.showMessageDialog(AnotherGuiSample.this, "Work Space is empty", "Error", JOptionPane.ERROR_MESSAGE);
} else {
JOptionPane.showMessageDialog(AnotherGuiSample.this, panel, "Select node to remove", JOptionPane.INFORMATION_MESSAGE);
}
for(int x = 0; x < nodes.length; x++)
System.out.println("node is " + nodes[x]);
return nodes;
}
如果您的目标是允许用户选择多个项目,请使用JCheckBox而不是JRadioButton。视觉线索很重要。大多数人会认为只能选择一个单选按钮。你不想让用户猜测事物是如何工作的,就像你不会把推杆放在向内开放的门上一样。 – VGR 2013-03-24 12:48:39