我沒有分組我的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