java - 让 FillLayout 表现正确

标签 java user-interface layout swt grid-layout

我正在尝试创建一个包含两棵树的 View ,它们占据整个显示屏。截至目前,这两棵树只占据了 Canvas 的一半(左半部分)。我不明白为什么,因为我尝试了发送到每个设置方法的许多不同参数,例如 setLayout、SashForm 等。这是我的代码,我附加了一张图像来显示我现在得到的简化 View 是。

    super(parent);
    setDisplayName("Viewer");
    setLayout(new FillLayout());
    ((FillLayout) getLayout()).marginHeight = ((FillLayout) getLayout()).marginWidth = 20;

    fullForm = new SashForm(this, SWT.VERTICAL);
    fullForm.setLayout(new FillLayout());

    adForm = new SashForm(fullForm, SWT.VERTICAL);
    adForm.setLayout(new FillLayout());

    countLabel = GUIToolkit.newLabel(this, SWT.CENTER, "", new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
    countLabel.setFont(boldFont);

    ad1Tree = createTree(fullForm, "name1", "col1", "col2");
    ad2Tree = createTree(fullForm, "name2", "col1", "col2");

创建树

    private static Tree createTree(final Composite parent, final String text, final String... columns) {
    final Composite group = GUIToolkit.newGroup(parent, SWT.NONE, text, null);
    group.setFont(FontManager.NORMAL_BOLD);
    group.setLayout(new FillLayout());
    final Tree tree = new Tree(group, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
    tree.setHeaderVisible(true);
    GUIToolkit.createColumns(tree, columns);
    GUIToolkit.addColumnSort(tree, DATA);
    GUIToolkit.removeDoubleClickExpand(tree);

This is what the view looks like in a very simplified manner

最佳答案

我认为你的问题是你没有分配 GridDataGroup包含TreeTree本身。

尝试像这样更新您的代码:

private static Tree createTree(final Composite parent, final String text, final String... columns) {
    final Composite group = GUIToolkit.newGroup(parent, SWT.NONE, text, new GridData(SWT.FILL, SWT.FILL, true, true)); // <-- THIS
    group.setFont(FontManager.NORMAL_BOLD);
    group.setLayout(new FillLayout());
    final Tree tree = new Tree(group, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
    tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); // <-- THIS
    tree.setHeaderVisible(true);
    GUIToolkit.createColumns(tree, columns);
    GUIToolkit.addColumnSort(tree, DATA);
    GUIToolkit.removeDoubleClickExpand(tree);
}

不确定到底是什么 GUIToolkit是,但我假设它只会应用 LayoutData您提供。

<小时/>

更新:

好吧,我就是这样做的:

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(1, false));

    Group top = new Group(shell, SWT.NONE);
    top.setLayout(new GridLayout(1, false));
    top.setText("Top");
    top.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Group bottom = new Group(shell, SWT.NONE);
    bottom.setLayout(new GridLayout(1, false));
    bottom.setText("Bottom");
    bottom.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Tree topTree = new Tree(top, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
    createColumns(topTree);
    Tree bottomTree = new Tree(bottom, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
    createColumns(bottomTree);

    shell.pack();
    shell.open();

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

private static void createColumns(Tree tree)
{
    tree.setHeaderVisible(true);

    for(int i = 0; i < 3; i++)
        new TreeColumn(tree, SWT.NONE).setText("Col " + i);

    for(int i = 0; i < 10; i++)
    {
        TreeItem item = new TreeItem(tree, SWT.NONE);

        for(int j = 0; j < tree.getColumnCount(); j++)
        {
            item.setText(j, "Item " + i + " " + j);
        }
    }

    for(int i = 0; i < tree.getColumnCount(); i++)
        tree.getColumn(i).pack();
}

关于java - 让 FillLayout 表现正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24186246/

相关文章:

java.sql.SQLNonTransientException : [Amazon][JDBC](10900) Not all parameters have been populated

html - Bootstrap响应式布局设计

ruby-on-rails - 我可以在没有 Controller 的情况下直接从 routes.rb 渲染布局吗?

java - 使用 c++ java native 代码的 GraphicsMagick 性能问题

java - hibernate 。一对一映射,无需交叉引用

java - 为什么引入FileWriter会删除文件中的所有内容?

安卓。 App关闭后如何保存用户名和密码?

java - JavaFx 中的 UI 没有响应

java - 如果没有可用的硬件键盘,如何强制显示和隐藏虚拟键盘?

html - 将 flexbox 用于布局和菜单/其他子组件的良好做法?