[SWT] ScrolledComposite のコンテンツを動的変更後もスクロール可能にする

はじめに:
ユーザー インターフェイスのデザインでは、限られたスペースに大量のコンテンツを表示する必要がある場合があります。コンテンツが表示可能領域のサイズを超える場合、スクロール機能を使用すると、ユーザーはスクロールしてすべてのコンテンツを確認できます。この記事では、Eclipse SWT ライブラリの ScrolledComposite コントロールを使用して、スクロール領域にスクロール可能なラベルを表示する方法を紹介します。

問題の説明

ボタンとスクロール領域を含むインターフェイスがあるとします。ユーザーがボタンをクリックすると、スクロール領域に新しいラベルが追加されることが期待されます。ラベルの数がスクロール領域の表示領域のサイズを超える場合、ユーザーはスクロールしてすべてのラベルを表示できます。スクロールバー。

解決

Eclipse SWT では、ScrolledComposite は、他のコントロールを含めてスクロール機能を提供できるスクロール可能なコンテナ コントロールです。

サンプルコードは次のとおりです。


import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class DynamicContentExample {
    
    

    public static void main(String[] args) {
    
    
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setMinimumSize(300, 360);	
        shell.setLayout(new GridLayout());

        Button addButton = new Button(shell, SWT.PUSH);
        addButton.setText("Add Content");
        
        // draw ScrolledComposite
        ScrolledComposite scrolledComposite = new ScrolledComposite(shell, SWT.V_SCROLL);
        scrolledComposite.setExpandVertical(true);
        scrolledComposite.setLayoutData(new GridData(GridData.FILL_BOTH));

        Composite contentComposite = new Composite(scrolledComposite, SWT.NONE);
        contentComposite.setLayout(new GridLayout(1, false));
        contentComposite.setLayoutData(new GridData(GridData.FILL_BOTH));

        scrolledComposite.setContent(contentComposite);
        scrolledComposite.setMinSize(contentComposite.computeSize(scrolledComposite.getClientArea().width, SWT.DEFAULT));

        // add event
        addButton.addListener(SWT.Selection, event -> {
    
    
            // 添加新内容
            Label label = new Label(contentComposite, SWT.NONE);
            label.setText("New Label");

            contentComposite.layout();
            contentComposite.setSize(contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

            scrolledComposite.setMinSize(contentComposite.computeSize(scrolledComposite.getClientArea().width, SWT.DEFAULT));
        });
        
        shell.pack();
        shell.open();

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

        display.dispose();
    }
}

サンプル コードでは、Eclipse SWT ライブラリの ScrolledComposite コントロールを使用してスクロール領域を実装します。ユーザーが「コンテンツの追加」ボタンをクリックすると、新しいラベル コントロールが作成され、コンテンツ コンポジットに追加されます。次に、サイズを再計算し、すべてのラベルをスクロールできるように最小サイズを設定して、ScrolledComposite を更新します。

addButton.addListener重要なコードに注意してください


// 内容容器重新布局
contentComposite.layout();
// 内容容器重新计算大小
contentComposite.setSize(contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));


// 给滚动容器重新指定最小值
scrolledComposite.setMinSize(contentComposite.computeSize(scrolledComposite.getClientArea().width, SWT.DEFAULT));

効果

初期:
ここに画像の説明を挿入します
コンテンツを追加した後:

ここに画像の説明を挿入します

追加されたコンテンツが表示領域のサイズを超えると、右側にスクロール バーが表示されます。

ここに画像の説明を挿入します

結論は

Eclipse SWT の ScrolledComposite コントロールを使用すると、スクロール領域にスクロール可能なコンテンツを表示できます。この手法は、限られたスペースに大量のコンテンツを表示する必要があるユーザー インターフェイスに役立ちます。サンプル コードは、必要に応じて拡張し、特定の設計要件を満たすようにカスタマイズできます。

おすすめ

転載: blog.csdn.net/m0_47406832/article/details/132735248