我希望能够同时 select 多个复选框? Java SWT

I want to be able to select multiple check-boxes simultaneously? Java SWT

我已经创建了几个单选按钮,但由于某些原因我只能 select 一个,如果我 select 另一个,之前 selected 的单选按钮将突然变得未选中。

代码:

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,这样只能同时 select 编辑其中一个。但我希望 4,5 和 6 在一个单独的组中等

我该如何解决这个问题,谢谢?

使用示例:

使用单选按钮 1、2 和 3 回答问题 1

使用单选按钮 4、5 和 6 回答问题二

在 SWT 中,您应该在 Composite 中创建按钮以形成一个组。所有 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

The CheckboxGroup class is used to group together a set of Checkbox buttons. Exactly one check box button in a CheckboxGroup can be in the "on" state at any given time. Pushing any button sets its state to "on" and forces any other button that is in the "on" state into the "off" state.

The following code example produces a new check box group, with three check boxes:

awt 示例:

CheckboxGroup cbg = new CheckboxGroup();
add(new Checkbox("one", cbg, true));
add(new Checkbox("two", cbg, false));
add(new Checkbox("three", cbg, false));

对于 Swing,您必须使用 ButtonGroup

//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);

最后,对于 SWT,您可以使用 Composite

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();
  }

}