java SWT CheckBox 按钮获得选中/未选中状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26656416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:20:23 来源:igfitidea点击:
SWT CheckBox Button get checked/unchecked state
提问by saurabh
I have a checkbox button based on which I want to set a variable as true
or false
. But I don't know how to handle the event. Here's my code:
我有一个复选框按钮,我想根据该按钮将变量设置为true
or false
。但我不知道如何处理事件。这是我的代码:
Boolean check = false;
Button checkBox = new Button(composite,SWT.CHECK);
checkBox.setText("CheckBox");
checkBox.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
if (event.detail == SWT.CHECK) {
// Now what should I do here to get
// Whether it is a checked event or unchecked event.
}
}
});
回答by alex2410
To validate selection use getSource()
method of event to get object(Button
) and check is it selected:
要验证选择使用getSource()
事件的方法来获取 object( Button
) 并检查它是否被选中:
checkBox.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
Button btn = (Button) event.getSource();
System.out.println(btn.getSelection());
}
});