组件不会在带有GridLayout的SWT中调整大小 [英] Components won't resize in SWT with a GridLayout

查看:362
本文介绍了组件不会在带有GridLayout的SWT中调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I discovered SWT a few days ago and decided to switch my plugin interface from Swing to SWT. I can place the components as I want, but when I resize the window, the components are not resized at all. Furthermore, when I fill a little Text (text area) with a large string, I can't find a way to resize it... The following code is the one defining the layout and the components, I guess someone will find where my mistake(s) is(are).

PS:我看到一些在线教程,在声明外壳之前先声明一个Display对象。当我这样做时,我会遇到InvalidThreadAccess异常。

P.S : I see some tutorials online declaring a Display object before declaring the shell. When I do, I encounter an InvalidThreadAccess Exception.

    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);

感谢您的阅读。

推荐答案

不要尝试将布局与 setSize setBounds 混合使用。

Don't try and mix Layouts with setSize and setBounds it won't work.

使用布局,您的代码可能类似于:

Using layouts your code might look like:

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 GridData GridLayout 将用来决定如何布置控件。

I have called setLayoutData on each control providing a GridData that GridLayout will use to decide how to layout the controls.

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

Note: If you are writing an Eclipse plugin you never call new Display() - that is only used when writing a standalone SWT program. Eclipse has already created a display.

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

Rather than just creating a new Shell you may want to look at using the JFace Dialog class which does a lot of the basic dialog handling for you.

这篇关于组件不会在带有GridLayout的SWT中调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆