当我尝试在复合材料中使用coolBar然后将这个复合材料嵌入到应用程序中时,我遇到了问题。 coolBar根本没有出现。其他工具(如toolBar和其他复合材料)不会出现此问题。我能做错什么或忘记什么?
在关注代码之前,我会参考我的系统:
复合代码:
package xx.xxx.xx.pcommJavaGUI.composites;
import org.eclipse.swt.widgets.Composite;
public class TestComposite extends Composite {
public TestComposite(Composite parent, int style) {
super(parent, style);
setLayout(new GridLayout(1, false));
CoolBar coolBar = new CoolBar(this, SWT.FLAT);
CoolItem coolItem = new CoolItem(coolBar, SWT.NONE);
Button btnTest = new Button(coolBar, SWT.NONE);
coolItem.setControl(btnTest);
btnTest.setText("Test");
Tree tree = new Tree(this, SWT.BORDER);
tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}
应用程序窗口代码:
package xx.xxx.xx.pcommJavaGUI.composites;
import org.eclipse.swt.SWT;
public class TestApplication {
protected Shell shell;
public static void main(String[] args) {
try {
TestApplication window = new TestApplication();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
shell.setLayout(new GridLayout(1, false));
TestComposite tc = new TestComposite(shell, SWT.NONE);
GridData gd_tc = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
tc.setLayoutData(gd_tc);
}
}
感谢您的帮助。
答案 0 :(得分:1)
可能只是因为您没有为酷吧设置布局数据。请参阅this article以了解布局的工作原理。
答案 1 :(得分:1)
您必须手动设置CoolItem
的大小。
pack();
将Button
设置为默认大小。CoolItem
的大小设置为Button
的大小。 Button
:
Button btnTest = new Button(coolBar, SWT.NONE);
coolItem.setControl(btnTest);
btnTest.setText("Test");
// If you do not call this, btnTest.getSize() will give you x=0,y=0.
btnTest.pack();
设置CoolItem
:
Point size = btnTest.getSize();
coolItem.setControl(btnTest);
coolItem.setSize(coolItem.computeSize(size.x, size.y));
<强>链接强>: