我们如何自动调整SWT中组件的大小?
在我的SWT应用程序中,我在SWT shell中有一些组件。
现在,我如何根据显示窗口的大小自动重新调整此组件的大小。
Display display = new Display(); Shell shell = new Shell(display); Group outerGroup,lowerGroup; Text text; public test1() { GridLayout gridLayout = new GridLayout(); gridLayout.numColumns=1; shell.setLayout(gridLayout); outerGroup = new Group(shell, SWT.NONE); GridData data = new GridData(1000,400); data.verticalSpan = 2; outerGroup.setLayoutData(data); gridLayout = new GridLayout(); gridLayout.numColumns=2; gridLayout.makeColumnsEqualWidth=true; outerGroup.setLayout(gridLayout); ... }
即,当我减小窗口的大小时,它内部的组件应该根据它出现。
这听起来很可疑,就像你没有使用布局一样。
布局的整个概念让人担心不必要的大小调整。 布局将考虑其所有组件的大小。
我建议阅读关于布局的Eclipse文章
您的代码很容易纠正。 不要设置单个组件的大小,布局将决定它们的大小。 如果您希望窗口具有预定义的大小,请设置shell的大小:
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout(1, false)); Group outerGroup = new Group(shell, SWT.NONE); // Tell the group to stretch in all directions outerGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); outerGroup.setLayout(new GridLayout(2, true)); outerGroup.setText("Group"); Button left = new Button(outerGroup, SWT.PUSH); left.setText("Left"); // Tell the button to stretch in all directions left.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); Button right = new Button(outerGroup, SWT.PUSH); right.setText("Right"); // Tell the button to stretch in all directions right.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); shell.setSize(1000,400); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
在resize之前:
resize后:
- 如何从Iterator 中创建无限的Stream ?
- 使用NIO直接缓冲区时设置-XX:+ DisableExplicitGC的影响
- 如何在Spring启动rest应用程序中使用Swagger ui使用密码流配置oAuth2
- 错误:客户端不支持服务器请求的身份validation协议; 考虑升级MySQL客户端
- 如何避免使用collection fetch指定的警告“firstResult / maxResults; 在内存中应用!“在使用Hibernate时?
- 在JRE 8中使用JavaFX,“访问限制”错误
- 将varargs传递给Spring spEL导致“在com.sun.proxy上找不到方法”
- 如何以异步方式发出Web请求
- 四元数平滑旋转