我需要一个布局,其中2个复合网站可以坐在一起垂直。第一个组合的高度应该比第二个组合的高度大。我不能使用null布局。为此,我必须使用一些布局管理器。每当我调整shell的大小时,组合也应该调整大小。哪种布局可以做到这一点?请看附件中的截图。这就是我需要的输出。
我不知道为什么屏幕截图没有出现。我已经添加了下面的代码。请试一试。
package views;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;
public class NewComposite extends org.eclipse.swt.widgets.Composite {
private Composite composite1;
private Composite composite2;
/**
* Auto-generated main method to display this
* org.eclipse.swt.widgets.Composite inside a new Shell.
*/
public static void main(String[] args) {
showGUI();
}
/**
* Overriding checkSubclass allows this class to extend org.eclipse.swt.widgets.Composite
*/
protected void checkSubclass() {
}
/**
* Auto-generated method to display this
* org.eclipse.swt.widgets.Composite inside a new Shell.
*/
public static void showGUI() {
Display display = Display.getDefault();
Shell shell = new Shell(display);
NewComposite inst = new NewComposite(shell, SWT.NULL);
Point size = inst.getSize();
shell.setLayout(new FillLayout());
shell.layout();
if(size.x == 0 && size.y == 0) {
inst.pack();
shell.pack();
} else {
Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
shell.setSize(shellBounds.width, shellBounds.height);
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
public NewComposite(org.eclipse.swt.widgets.Composite parent, int style) {
super(parent, style);
initGUI();
}
private void initGUI() {
try {
this.setLayout(null);
this.setSize(492, 304);
{
composite1 = new Composite(this, SWT.BORDER_SOLID);
GridLayout composite1Layout = new GridLayout();
composite1Layout.makeColumnsEqualWidth = true;
composite1.setLayout(composite1Layout);
composite1.setBounds(0, 0, 492, 232);
composite1.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_CYAN));
}
{
composite2 = new Composite(this, SWT.BORDER_SOLID);
GridLayout composite2Layout = new GridLayout();
composite2Layout.makeColumnsEqualWidth = true;
composite2.setLayout(composite2Layout);
composite2.setBounds(0, 232, 492, 72);
composite2.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
}
this.layout();
} catch (Exception e) {
e.printStackTrace();
}
}
}发布于 2012-05-16 23:54:00
您错误地使用了布局管理器--将GridLayout设置为每个子组合(composite1、composite2),但将null布局设置为其父组合(this)。它是管理其子组件的大小和位置的父组件。因此,将GridLayout设置为this,并将每个孩子的layoutData设置为适当配置的GridData。如何配置它们,请参阅Javadoc。它涉及到为verticalAlignment设置grabExcessVerticalSpace和使用SWT.FILL。
https://stackoverflow.com/questions/10617563
复制相似问题