有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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


共 (2) 个答案

  1. # 1 楼答案

    在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);
    
  2. # 2 楼答案

    您必须使用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();
      }
    
    }