【问题标题】:Components won't resize in SWT with a GridLayout组件不会在 SWT 中使用 GridLayout 调整大小
【发布时间】:2016-09-23 08:39:04
【问题描述】:

几天前我发现了 SWT,并决定将我的插件界面从 Swing 切换到 SWT。我可以随意放置组件,但是当我调整窗口大小时,组件根本不会调整大小。此外,当我用一个大字符串填充一个小文本(文本区域)时,我找不到调整它大小的方法...... 以下代码是定义布局和组件的代码,我想有人会发现我的错误在哪里。

P.S:我在网上看到一些教程在声明 shell 之前声明了一个 Display 对象。当我这样做时,我遇到了 InvalidThreadAccess 异常。

    Shell shell = new Shell();
    GridLayout gridLayout = new GridLayout(2, false);
    shell.setLayout(gridLayout);

    tree = new Tree(shell, SWT.CHECK | SWT.BORDER);

    Text tips = new Text(shell, SWT.READ_ONLY);
    tips.setText("Pick the files and nodes to refactor : ");
    oldFileViewer = new Text(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    oldFileViewer.setText("here is the old file viewer\t\t\t\t\t\t\t\t\t\n\n\n\n\n\n");
    oldFileViewer.setSize(400, 400);

    newFileViewer = new Text(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    newFileViewer.setText("and here is the new file viewer\t\t\t\t\t\t\t\t\t\n\n\n\n\n\n");
    newFileViewer.setSize(400, 400);
    Button ok = new Button(shell, SWT.PUSH);

感谢阅读。

【问题讨论】:

标签: java eclipse eclipse-plugin swt


【解决方案1】:

不要尝试将 Layouts 与 setSizesetBounds 混合使用,这是行不通的。

使用布局,您的代码可能如下所示:

GridLayout gridLayout = new GridLayout(2, false);
shell.setLayout(gridLayout);

tree = new Tree(shell, SWT.CHECK | SWT.BORDER);
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
tree.setLayoutData(data);

Text tips = new Text(shell, SWT.READ_ONLY);
tips.setText("Pick the files and nodes to refactor : ");
tips.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));

oldFileViewer = new Text(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
oldFileViewer.setText("here is the old file viewer\t\t\t\t\t\t\t\t\t\n\n\n\n\n\n");
data = new GridData(SWT.FILL, SWT.FILL, false, false);
data.heightHint = 400;
oldFileViewer.setLayoutData(data);

newFileViewer = new Text(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
newFileViewer.setText("and here is the new file viewer\t\t\t\t\t\t\t\t\t\n\n\n\n\n\n");
data = new GridData(SWT.FILL, SWT.FILL, false, false);
data.heightHint = 400;
newFileViewer.setLayoutData(data);

Button ok = new Button(shell, SWT.PUSH);
ok.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));

我在每个控件上调用了setLayoutData,提供了GridDataGridLayout 将使用它来决定如何布局控件。

注意:如果您正在编写一个 Eclipse 插件,您永远不会调用 new Display() - 这仅在编写独立 SWT 程序时使用。 Eclipse 已经创建了一个显示。

您可能想考虑使用 JFace Dialog 类,而不是仅仅创建一个新的Shell,它为您完成了许多基本的对话框处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 2012-10-07
    • 2011-11-11
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    相关资源
    最近更新 更多