java - 将 abel 包裹在复合 Material 中

标签 java eclipse swt jface

我有 ScrolledComposite,它只允许垂直滚动。 (heighthint = 400)。

在这个 ScrolledComposite 中,我有另一个 CompositeA(滚动高度可能超过 400)来存储所有其他小部件。

我有一个很长的标签(启用了 SWT.WRAP)。但它不是换行,而是始终显示在一行中。我希望此标签根据其父级 (CompositeA)

的宽度换行

我忘了补充一点,这个 CompositeA 是一个 2 列的 GridLayoutmakeColumnsEqualWidth = 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();

}

如果你运行它,你可以看到一个绿色的滚动复合体和一个红色的复合体。我希望红色复合宽度相对于滚动复合宽度填充,而无需硬编码 maxWidth = 600

最佳答案

我的布局中有同样的问题,发现 this有用的答案。

因为“这不是错误,它是一个功能”请尝试评论 #19 中的这个答案 here .

我将以下代码行用于我的标签。

Label tip = new Label(shell, SWT.WRAP | SWT.BORDER | SWT.LEFT);
final GridData data = new GridData(SWT.HORIZONTAL, SWT.TOP, true, false, 1, 1);
tip.setLayoutData(data);
tip.setText("Here stands my long, long and much more longer text...");

关于java - 将 abel 包裹在复合 Material 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11772305/

相关文章:

java - 将 GUI 置于单独的进程中

swt - 向 JFace 表中插入一个按钮

java - 每次将 Json 响应解析为不同的 Java 对象

java - WSDL 到 java 转换失败

java - 如何使 BufferedImage 与该图像一起使用? (java)

android - 在Eclipse中还原Gradle构建:

java - Eclipse PDT - 如何用于 Java 开发

java - 仅当在 cmd 中运行导出可执行 jar 时出现 ClassNotFoundException log4j

java - 如何在类编译后运行java程序

java - 在 SWT UI 中删除窗口边框会禁用重新定位