eclipse我希望能够同时选择多个复选框?Java SWT
我已经创建了几个单选按钮,但由于某些原因,我只能选择一个,如果我选择另一个单选按钮,以前选择的单选按钮将突然取消选中
代码:
package demo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
public class example {
protected Shell shell;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
example window = new example();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
Button btnRadioButton = new Button(shell, SWT.RADIO);
btnRadioButton.setBounds(83, 10, 90, 16);
btnRadioButton.setText("Radio Button");
Button btnRadioButton_1 = new Button(shell, SWT.RADIO);
btnRadioButton_1.setBounds(55, 86, 90, 16);
btnRadioButton_1.setText("Radio Button");
Button btnRadioButton_2 = new Button(shell, SWT.RADIO);
btnRadioButton_2.setBounds(179, 158, 90, 16);
btnRadioButton_2.setText("Radio Button");
Button btnRadioButton_3 = new Button(shell, SWT.RADIO);
btnRadioButton_3.setBounds(293, 65, 90, 16);
btnRadioButton_3.setText("Radio Button");
Button button = new Button(shell, SWT.RADIO);
button.setText("Radio Button");
button.setBounds(303, 103, 90, 16);
Button button_1 = new Button(shell, SWT.RADIO);
button_1.setText("Radio Button");
button_1.setBounds(189, 196, 90, 16);
}
}
我想单选按钮1,2和3被链接,这样只有一个可以同时选择。但我希望4、5和6在一个单独的小组中,等等
我怎样才能解决这个问题,谢谢
使用示例:
使用单选按钮1、2和3回答问题1
使用单选按钮4、5和6回答问题2
等
# 1 楼答案
在SWT中,您应该在组合中创建按钮以形成一个组。所有6个按钮都是在同一个组合(shell)中创建的,因此它们都在同一个组中
# 2 楼答案
您必须使用CheckBoxGroup
awt示例:
对于Swing,必须使用ButtonGroup
最后,对于SWT,您可以使用Composite