我希望能够同时选择多个复选框?Java SWT
我已经创建了几个单选按钮,但由于某些原因,我只能选择一个,如果我选择另一个单选按钮,以前选择的单选按钮将突然取消选中 代码:我希望能够同时选择多个复选框?Java SWT,java,eclipse,swt,Java,Eclipse,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 {
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
等等。在SWT中,您应该在组合中创建按钮以形成一个组。所有6个按钮都是在同一个组合(shell)中创建的,因此它们都在同一个组中
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
Button btnRadioButton = new Button(shell, SWT.RADIO);
btnRadioButton.setBounds(0, 10, 90, 16);
btnRadioButton.setText("Radio Button");
Button btnRadioButton_1 = new Button(shell, SWT.RADIO);
btnRadioButton_1.setBounds(0, 30, 90, 16);
btnRadioButton_1.setText("Radio Button");
Button btnRadioButton_2 = new Button(shell, SWT.RADIO);
btnRadioButton_2.setBounds(0, 50, 90, 16);
btnRadioButton_2.setText("Radio Button");
Composite composite = new Composite(shell, SWT.NULL);
composite.setBounds(0, 70, 300, 200);
composite.setLayout(new RowLayout());
Button btnRadioButton_3 = new Button(composite, SWT.RADIO);
btnRadioButton_3.setBounds(0, 0, 90, 16);
btnRadioButton_3.setText("Radio Button");
Button button = new Button(composite, SWT.RADIO);
button.setText("Radio Button");
button.setBounds(0, 20, 90, 16);
Button button_1 = new Button(composite, SWT.RADIO);
button_1.setText("Radio Button");
button_1.setBounds(0, 40, 90, 16);
你必须使用一个
CheckboxGroup类用于将一组复选框组合在一起
按钮。复选框组中只能有一个复选框按钮位于
在任何给定时间的“开”状态。按下任何按钮都会将其状态设置为
“打开”并强制处于“打开”状态的任何其他按钮进入
“关闭”状态
下面的代码示例生成一个新的复选框组,其中包含三个
复选框:
awt示例:
CheckboxGroup cbg = new CheckboxGroup();
add(new Checkbox("one", cbg, true));
add(new Checkbox("two", cbg, false));
add(new Checkbox("three", cbg, false));
对于挥杆,你必须使用
最后,对于SWT,您可以使用
我如何按照此示例对上述单选按钮进行分组,例如btnRadioButton、btnRadioButton_1和btnRadioButton_2,前提是您使用了不同类型的按钮。我想使用SWT单选按钮,你知道如何实现吗?@MuratK。基于此问题标有
swt
的事实,您可能可以删除答案中有关AWT/Swing的部分。当我尝试上述操作时,如何按照此示例对上述单选按钮进行分组,例如btnRadioButton、btnRadioButton和btnRadioButton,最后三个单选按钮变为不可见这是因为当您更改按钮父级时,需要调整x和y边界。
//In initialization code:
//Create the radio buttons.
JRadioButton birdButton = new JRadioButton(birdString);
birdButton.setMnemonic(KeyEvent.VK_B);
birdButton.setActionCommand(birdString);
birdButton.setSelected(true);
JRadioButton catButton = new JRadioButton(catString);
catButton.setMnemonic(KeyEvent.VK_C);
catButton.setActionCommand(catString);
JRadioButton dogButton = new JRadioButton(dogString);
dogButton.setMnemonic(KeyEvent.VK_D);
dogButton.setActionCommand(dogString);
JRadioButton rabbitButton = new JRadioButton(rabbitString);
rabbitButton.setMnemonic(KeyEvent.VK_R);
rabbitButton.setActionCommand(rabbitString);
JRadioButton pigButton = new JRadioButton(pigString);
pigButton.setMnemonic(KeyEvent.VK_P);
pigButton.setActionCommand(pigString);
//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);
group.add(dogButton);
group.add(rabbitButton);
group.add(pigButton);
public class RadioButtonComposite {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Composite composite = new Composite(shell, SWT.NULL);
composite.setLayout(new RowLayout());
Button mrButton = new Button(composite, SWT.RADIO);
mrButton.setText("Mr.");
Button mrsButton = new Button(composite, SWT.RADIO);
mrsButton.setText("Mrs.");
Button msButton = new Button(composite, SWT.RADIO);
msButton.setText("Ms.");
Button drButton = new Button(composite, SWT.RADIO);
drButton.setText("Dr.");
shell.open();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
}