6

ScrolledComposite垂直スクロールのみを許可するものがあります。( heighthint = 400)。

この ScrolledComposite 内に、CompositeA他のすべてのウィジェットを格納する別のウィジェット (スクロールのために高さが 400 を超える場合があります) があります。

非常に長いラベルがあります (SWT.WRAP有効になっています)。ただし、折り返しではなく、常に 1 行で表示されます。このラベルを親の幅に合わせて折り返す ( CompositeA)

CompositeAこれが の 2 列GridLayoutであることを追加するのを忘れていましたmakeColumnsEqualWidth = true

これが私のコードです:

public void createPartControl(Composite parent) {
    // TODO Auto-generated method stub

    Display display = parent.getDisplay();

    toolkit = new FormToolkit(display);
    form = toolkit.createForm(parent);
    form.setText("ABC");

    Composite body = form.getBody();

    TableWrapLayout layout = new TableWrapLayout();
    layout.numColumns = 2;
    body.setLayout(layout);

    Label header1 = toolkit.createLabel(body, "ABC: ");
    Font font = new Font(display, "Arial", 11, SWT.BOLD);
    header1.setFont(font);

    Label header2 = toolkit.createLabel(body, "XYZ",
            SWT.WRAP);
    font = new Font(display, "Arial", 11, SWT.NONE);
    header2.setFont(font);

    TableWrapData wd = new TableWrapData(TableWrapData.FILL_GRAB);      
    header2.setLayoutData(wd);

    form.getBody().setBackground(
            form.getBody().getDisplay()
                    .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));

    // Scrolled composite
    ScrolledComposite sc = new ScrolledComposite(body, SWT.BORDER_SOLID
            | SWT.V_SCROLL);
    sc.setAlwaysShowScrollBars(true);
    sc.setBackground(new Color(display, 50,255,155));


    wd = new TableWrapData(TableWrapData.FILL); 
    wd.heightHint = 360;
    wd.colspan = 2;
    wd.grabHorizontal = false;
    sc.setLayoutData(wd);

    sc.setLayout(new TableWrapLayout());

    Composite innerComposite = toolkit.createComposite(sc);
    sc.setContent(innerComposite);

    innerComposite.setLayout(new TableWrapLayout());
    innerComposite.setBackground(new Color(display, 255,50,50));

    Section section = toolkit.createSection(innerComposite,
            Section.DESCRIPTION | Section.TITLE_BAR | Section.EXPANDED);
    wd = new TableWrapData(TableWrapData.FILL);
    wd.maxWidth = 600; // don't want to hardcode this value

    section.setLayoutData(wd);
    section.setText("Section");
    section.setDescription("A not so long description......................");

    // Composite for Section
    Composite sectionClient = toolkit.createComposite(section);
    layout = new TableWrapLayout();
    layout.numColumns = 2;
    layout.makeColumnsEqualWidth = true;
    sectionClient.setLayout(layout);

    toolkit.createButton(sectionClient, "Button 1", SWT.RADIO);

    Label rightDesc = toolkit
            .createLabel(
                    sectionClient,
                    "A very long long long long long long long long long long long long long long long long long long long long desc that needs wrapping",
                    SWT.WRAP);
    font = new Font(display, "Arial", 10, SWT.ITALIC);
    rightDesc.setFont(font);
    wd = new TableWrapData();
    wd.rowspan = 2;
    rightDesc.setLayoutData(wd);

    Combo comboDropDown = new Combo(sectionClient, SWT.DROP_DOWN
            | SWT.BORDER);
    comboDropDown.setText("DDL");
    comboDropDown.add("1");
    comboDropDown.add("2");
    comboDropDown.add("3");

    Label lineBreak = toolkit.createSeparator(sectionClient, SWT.SEPARATOR
            | SWT.HORIZONTAL);
    wd = new TableWrapData(TableWrapData.FILL);
    wd.colspan = 2;
    lineBreak.setLayoutData(wd);

    /***********************/

    toolkit.createButton(sectionClient, "Button 2", SWT.RADIO);

    Label rightDesc2 = toolkit
            .createLabel(
                    sectionClient,
                    "A long long long long long long long long long long long long long long long long long long long long desc that needs wrapping",
                    SWT.WRAP);
    font = new Font(display, "Arial", 10, SWT.ITALIC);
    rightDesc2.setFont(font);
    wd = new TableWrapData(TableWrapData.FILL);
    wd.rowspan = 3;
    rightDesc2.setLayoutData(wd);

    toolkit.createLabel(sectionClient, "Desc",
            SWT.WRAP);
    toolkit.createText(sectionClient, "hello world", SWT.NONE);

    section.setClient(sectionClient);

    innerComposite.pack();

}

実行すると、緑のスクロールしたコンポジットと赤のコンポジットが表示されます。ハードコーディングせずに、赤いコンポジットの幅を scrolledcomposite の幅に合わせて埋めたいですmaxWidth = 600

4

5 に答える 5

0

これは間違っていることを覚えているかもしれませんが、テキストが変更されたときにコンポジットを再レイアウトする必要があったことを思い出します。これでテキストが折り返されました。

于 2012-08-02T07:08:03.270 に答える
0

驚くべきことに、TableWrapLayoutマネージャーが水平方向にスクロールするコンポジットを信頼しないことを理解しました。ツールキットでそのレイアウトを維持するには、をForm使用する必要があります。上記のコードの例を作成しました。

public void createPartControl(Composite parent) {
    // TODO Auto-generated method stub

    Display display = parent.getDisplay();

    toolkit = new FormToolkit(display);
    form = toolkit.createForm(parent);
    form.setText("ABC");

    Composite body = form.getBody();

    TableWrapLayout layout = new TableWrapLayout();
    layout.numColumns = 2;
    body.setLayout(layout);

    Label header1 = toolkit.createLabel(body, "ABC: ");
    Font font = new Font(display, "Arial", 11, SWT.BOLD);
    header1.setFont(font);

    Label header2 = toolkit.createLabel(body, "XYZ", SWT.WRAP);
    font = new Font(display, "Arial", 11, SWT.NONE);
    header2.setFont(font);

    TableWrapData wd = new TableWrapData();      
    header2.setLayoutData(wd);

    body.setBackground(body.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));

    // Scrolled composite
    Composite sc = toolkit.createComposite(body, SWT.BORDER_SOLID | SWT.V_SCROLL);
    sc.setBackground(new Color(display, 50,255,155));
    layout = new TableWrapLayout();    
    sc.setLayout(layout);

    wd = new TableWrapData(TableWrapData.FILL_GRAB); 
    wd.heightHint = 360;
    wd.colspan = 2;
    sc.setLayoutData(wd);

    Composite innerComposite = toolkit.createComposite(sc);

    layout = new TableWrapLayout();
    innerComposite.setLayout(layout);
    innerComposite.setBackground(new Color(display, 255,50,50));

    wd = new TableWrapData(TableWrapData.FILL_GRAB); 
    innerComposite.setLayoutData(wd);

    Section section = toolkit.createSection(innerComposite,
            Section.DESCRIPTION | Section.TITLE_BAR | Section.TWISTIE | Section.EXPANDED);

    section.setText("Section");
    section.setDescription("A not so long description......................");

    wd = new TableWrapData(TableWrapData.FILL_GRAB);
    // wd.maxWidth = 600; // don't want to hardcode this value
    section.setLayoutData(wd);


    // Composite for Section
    Composite sectionClient = toolkit.createComposite(section);
    layout = new TableWrapLayout();
    layout.numColumns = 2;
    // layout.makeColumnsEqualWidth = true;
    sectionClient.setLayout(layout);
    section.setClient(sectionClient);

    wd = new TableWrapData(TableWrapData.FILL_GRAB); 
    sectionClient.setLayoutData(wd);

    Label rightDesc = toolkit.createLabel(sectionClient,
                    "A very long long long long long long long long long long long long long long long long long long long long desc that needs wrapping",
                    SWT.WRAP);
    font = new Font(display, "Arial", 10, SWT.ITALIC);
    rightDesc.setFont(font);
    wd = new TableWrapData();
    wd.colspan = 2;
    rightDesc.setLayoutData(wd);

    Combo comboDropDown = new Combo(sectionClient, SWT.DROP_DOWN | SWT.BORDER);
    comboDropDown.setText("DDL");
    comboDropDown.add("1");
    comboDropDown.add("2");
    comboDropDown.add("3");

    Label lineBreak = toolkit.createSeparator(sectionClient, SWT.SEPARATOR | SWT.HORIZONTAL);
    wd = new TableWrapData(TableWrapData.FILL_GRAB);
    lineBreak.setLayoutData(wd);
        //
        //    /***********************/
        //
    Button button1 = toolkit.createButton(sectionClient, "Button 1", SWT.RADIO);
      wd = new TableWrapData();
      wd.colspan = 2;
      button1.setLayoutData(wd);

    Button button2 = toolkit.createButton(sectionClient, "Button 2", SWT.RADIO);
      wd = new TableWrapData();
      wd.colspan = 2;
      button2.setLayoutData(wd);

    Label rightDesc2 = toolkit.createLabel(sectionClient,
                    "A long long long long long long long long long long long long long long long long long long long long desc that needs wrapping",
                    SWT.WRAP);
    font = new Font(display, "Arial", 10, SWT.ITALIC);
    rightDesc2.setFont(font);
    wd = new TableWrapData();
    wd.colspan = 2;
    rightDesc2.setLayoutData(wd);

    Label desc = toolkit.createLabel(sectionClient, "Desc:");
    Text hello = toolkit.createText(sectionClient, "hello world");
    wd = new TableWrapData(TableWrapData.FILL_GRAB);
    hello.setLayoutData(wd);
}
于 2012-08-24T09:00:35.500 に答える
0

長いテキスト ラベルを折り返したい場合は、SWT.WRAP では不十分です。レイアウトでwidthHintを指定しない場合、ラベルはデフォルトでテキストを折り返さない。これは私のために働く:

Label label = new Label(parent, SWT.WRAP);
label.setText("very very long text....");
GridData gd = new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1);
gd.widthHint = 200;
label.setLayoutData(gd);
于 2020-07-08T07:42:05.967 に答える