我创建了几个单选按钮但由于某种原因我只能选择一个,如果我选择另一个,之前选择的单选按钮将突然变为未选中状态。
代码:
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回答问题一
使用单选按钮4,5和6
回答问题二等
答案 0 :(得分:1)
在SWT中,您应该在Composite中创建按钮以形成一个组。所有6个按钮都在同一个复合(shell)中创建,因此它们都在同一个组中。
<!-- =====================================sessionFactory==================================== -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.mavenTest.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://localhost"/> <!-- jdbc:hsqldb:mem:. -->
<property name="username" value="sa"/>
<property name="password" value=""/>
<property name="maxActive" value="100000"/>
<property name="maxIdle" value="30"/>
<property name="maxWait" value="16000"/>
<property name="minIdle" value="0"/>
</bean>
<!-- =====================================Bean dependency==================================== -->
<bean id="empService" class="com.mavenTest.service.EmpService" autowire="byName"/>
<bean id="employeeDAO" class="com.mavenTest.dao.EmpDao" autowire="byName">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
答案 1 :(得分:0)
您必须使用CheckBoxGroup。
CheckboxGroup类用于将一组Checkbox组合在一起 纽扣。 CheckboxGroup中的一个复选框按钮可以在 在任何特定时间“开启”状态。按任意按钮将其状态设置为 “on”并强制处于“on”状态的任何其他按钮进入 “关闭”状态。
以下代码示例生成一个新的复选框组,其中包含三个 复选框:
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();
}
}