在调整外壳的大小时,我在RowLayout中遇到了一些问题。我创建了这个组合,并将该组合的布局设置为RowLayout。在复合体中添加了三个按钮。现在当我调整外壳的大小时,如果没有足够的空间放置第三个按钮,它应该会掉下来。但它不会降下来。有人能说出代码中哪里出了问题吗?请看我下面的代码。
package com.rcp.mytraining.layout.composite;
import org.eclipse.swt.widgets.Composite;
public class ChatComposite extends Composite {
/**
* Create the composite.
*
* @param parent
* @param style
*/
public ChatComposite(Composite parent, int style) {
super(parent, style);
Composite comp = new Composite(parent, SWT.NONE);
// Color s = new Color(display, new RGB(30,30,30));
Color s = Display.getDefault().getSystemColor(1);
comp.setBackground(s);
RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
rowLayout.wrap = true;
rowLayout.pack = false;
comp.setLayout(rowLayout);
Button btnNewButton = new Button(comp, SWT.NONE);
btnNewButton.setText("New Button");
Button btnNewButton_1 = new Button(comp, SWT.NONE);
btnNewButton_1.setText("ss");
Button btnNewButton_2 = new Button(comp, SWT.NONE);
btnNewButton_2.setText("New Button");
comp.pack();
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
// shell.setSize(200, 200);
// shell.setLayout(new FillLayout());
ChatComposite chatComposite = new ChatComposite(shell, SWT.NONE);
shell.pack();
shell.open();
// chatComposite.pack();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
// display.dispose();
}
}请看下面代码中的区别。我也想以同样的方式工作。上面的代码应该和下面的代码一样工作。
package com.rcp.mytraining.layout.composite;
import org.eclipse.swt.widgets.Composite;
public class ChatComposite extends Composite {
/**
* Create the composite.
*
* @param parent
* @param style
*/
public ChatComposite(Composite parent, int style) {
super(parent, style);
/*
* RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
*
* // rowLayout.wrap = true; //rowLayout.pack = true;
* setLayout(rowLayout);
*
* Button btnNewButton = new Button(this, SWT.NONE);
* btnNewButton.setText("New Button");
*
* Button btnNewButton_1 = new Button(this, SWT.NONE);
* btnNewButton_1.setText("New Button");
*
* Button btnNewButton_2 = new Button(this, SWT.NONE);
* btnNewButton_2.setText("New Button");
*/
pack();
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
// shell.setSize(200, 200);
shell.setLayout(new FillLayout());
Composite comp = new Composite(shell, SWT.NONE);
// Color s = new Color(display, new RGB(30,30,30));
Color s = Display.getDefault().getSystemColor(1);
comp.setBackground(s);
RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
rowLayout.wrap = true;
// rowLayout.pack = true;
comp.setLayout(rowLayout);
Button btnNewButton = new Button(comp, SWT.NONE);
btnNewButton.setText("New Button");
Button btnNewButton_1 = new Button(comp, SWT.NONE);
btnNewButton_1.setText("ss");
Button btnNewButton_2 = new Button(comp, SWT.NONE);
btnNewButton_2.setText("New Button");
comp.pack();
// ChatComposite chatComposite = new ChatComposite(shell, SWT.NONE);
shell.pack();
shell.open();
// chatComposite.pack();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
// display.dispose();
}
}发布于 2013-01-22 18:30:23
您的组合不会随壳调整大小,因为您没有指定它这样做。
将GridData对象添加到其布局数据中,如下所示:
comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));注意:出于调试目的,您可以将comp的背景设置为红色(或其他一些鲜艳的颜色),并查看它们之间的差异。
https://stackoverflow.com/questions/14456278
复制相似问题