如何在下面的代码中对复选框进行分组?如何在java中获取它的复选框的选定值?
// CHECKBOXES
JCheckBox cb_CCP = new JCheckBox("Chinese Cultural Places");
JCheckBox cb_HandCP = new JCheckBox("Heritage & Culture Places");
JCheckBox cb_HCentres = new JCheckBox("Heritage Centres");
JCheckBox cb_HMuseums = new JCheckBox("Heritage Museums");
JCheckBox cb_ICP = new JCheckBox("Indian Cultural Places");
JCheckBox cb_MCP = new JCheckBox("Malay Cultural Places\r\n");
JCheckBox cb_HTrails = new JCheckBox("Heritage Trails");
JCheckBox cb_PCP = new JCheckBox("Peranakan Cultural Places");
答案 0 :(得分:1)
要进行分组,您需要ButtonGroup
ButtonGroup bg = new ButtonGroup();
bg.add(cb_CCP);
bg.add(cb_HandCP);
.. and so on
添加要分组的复选框。通过分组,您可以选择只有一个。
要检查选择了哪一个,这应该在你的听众中
if (cb_CCP.isSelected()){
String s = cb_CCP.getText();
// do something
} else if (cb_HandCP.isSelected()){
// do something
} else if (.....){
...
}