SWT:以编程方式设置单选按钮

| 当我创建几个单选按钮(
new Button(parent, SWT.RADIO)
)并使用
radioButton5.setSelection(true)
编程设置选择时,先前选择的单选按钮也保持选中状态。我是否需要遍历同一组中的所有其他单选按钮以取消选择它们,还是有一个更简单的选择?提前致谢。     
已邀请:
不幸的是,您必须遍历所有选项。第一次出现UI时,将触发
BN_CLICKED
事件。如果未使用
SWT.NO_RADIO_GROUP
选项创建
Shell
Group
或任何单选按钮容器,则将调用以下方法:
void selectRadio () 
{
    Control [] children = parent._getChildren ();
    for (int i=0; i<children.length; i++) {
        Control child = children [i];
        if (this != child) child.setRadioSelection (false);
    }
    setSelection (true);
}
因此,实际上,日食本身取决于遍历所有单选按钮并切换其状态。 每次您手动选择单选按钮时,都会触发
BN_CLICKED
事件,并因此自动切换。 当您使用
button.setSelection(boolean)
时,不会触发
BN_CLICKED
事件。因此,不会自动切换单选按钮。 有关更多详细信息,请查看“ 10”类。     
同一组合中的单选按钮将作为一个组。一次只能选择一个单选按钮。这是一个工作示例:
    Composite composite = new Composite(parent, SWT.NONE);

    Button btnCopy = new Button(composite, SWT.RADIO);
    btnCopy.setText(\"Copy Element\");
    btnCopy.setSelection(false);

    Button btnMove = new Button(composite, SWT.RADIO);
    btnMove.setText(\"Move Element\");
    
这应该自动发生。您如何创建按钮?他们是同一个父母吗?父母是否使用NO_RADIO_GROUP样式?     

要回复问题请先登录注册