簡體   English   中英

如何在SWT中實現父子關系復選框?

[英]How to implement parent child relationship checkbox in SWT?

我是SWT的新手,我的要求是我需要在對話框中添加兩個復選框。 我想要,如果我選擇了第一個復選框,則還應該選擇第二個。 但是,如果我禁用第二個復選框,則第一個仍處於選中狀態。 我不允許選擇第二個復選框,僅當選擇第一個復選框時才選擇第二個復選框。 我相信必須使用其父子關系和CheckboxTreeViewer(仍然不確定)。 有人可以跨要求發送代碼片段嗎?

查看以下代碼:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class CheckBoxExample {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(3, true));

        Button parentButton = new Button(shell, SWT.CHECK);
        parentButton.setText("Parent");
        Button childButton = new Button(shell, SWT.CHECK);
        childButton.setText("Child");
        childButton.setEnabled(false);

        parentButton.addListener(SWT.Selection, event -> {
            if (!parentButton.getSelection()) {
                childButton.setEnabled(false);
                childButton.setSelection(false);
                return;
            }
            childButton.setEnabled(true);
        });

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}

以上代碼的輸出

第1步 第2步 第三步 第四步 步驟5

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM