SWT CheckBox按钮获得选中/取消选中状态

sau*_*abh 11 java checkbox swt

我有一个复选框按钮,我希望将变量设置为truefalse.但我不知道如何处理这个事件.这是我的代码:

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.
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

ale*_*410 15

要验证getSource()事件的选择使用方法以获取object(Button)并检查它是否已选中:

    checkBox.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent event) {
            Button btn = (Button) event.getSource();
            System.out.println(btn.getSelection());
        }
    });
Run Code Online (Sandbox Code Playgroud)