我几天前发现了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);
感谢阅读。
答案 0 :(得分:1)
请勿尝试将布局与setSize
及setBounds
混合使用。
使用布局代码可能如下所示:
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
将用于决定如何布局控件。
注意:如果您正在编写Eclipse插件,则永远不会调用new Display()
- 仅在编写独立的SWT程序时使用。 Eclipse已经创建了一个显示器。
您可能希望查看使用JFace Shell
类,而不仅仅是创建一个新的Dialog
,它为您执行了许多基本对话框处理。