各位大哥大姐,我用的是swt designer工具,在可视化设计的时候能看到组件,为什么在程序运行的时候就没有了呢 ?代码如下 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.custom.ViewForm; 
import org.eclipse.swt.graphics.Point; 
import org.eclipse.swt.layout.FillLayout; 
import org.eclipse.swt.widgets.CoolBar; 
import org.eclipse.swt.widgets.CoolItem; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.widgets.ToolBar; 
import org.eclipse.swt.widgets.ToolItem; 
public class CoolBarTest extends Shell { /** 
* Launch the application 
* @param args 
*/ 
public static void main(String args[]) { 
try { 
Display display = Display.getDefault(); 
CoolBarTest shell = new CoolBarTest(display, SWT.SHELL_TRIM); 
shell.open(); 
shell.layout(); 
while (!shell.isDisposed()) { 
if (!display.readAndDispatch()) 
display.sleep(); 

} catch (Exception e) { 
e.printStackTrace(); 

} /** 
* Create the shell 
* @param display 
* @param style 
*/ 
public CoolBarTest(Display display, int style) { 
super(display, style); 
createContents(); 
setLayout(new FillLayout()); 
} /** 
* Create contents of the window 
*/ 
protected void createContents() { 
setText("SWT Application"); 
setSize(378, 241); final ViewForm viewForm = new ViewForm(this, SWT.NONE); final CoolBar coolBar = new CoolBar(viewForm, SWT.NONE); 
viewForm.setTopLeft(coolBar); final CoolItem newItemCoolItem = new CoolItem(coolBar, SWT.NONE); 
newItemCoolItem.setText("New item"); final ToolBar toolBar = new ToolBar(coolBar, SWT.NONE); 
newItemCoolItem.setControl(toolBar); final ToolItem newItemToolItem = new ToolItem(toolBar, SWT.PUSH); 
newItemToolItem.setText("设置"); coolBar.layout(); 
viewForm.layout(); // 
} protected void checkSubclass() { 
// Disable the check that prevents subclassing of SWT components 
} } 
各位大哥,大姐,组件为什么显示不出来呢?谢谢

解决方案 »

  1.   

    显示不出来应该是layout的问题,查看一下layout的使用。
      

  2.   

    要给coolitem 设置大小
    用setSize()方法,
    才能显示
    这是版本差异造成d
      

  3.   

    package widowmulti;import org.eclipse.swt.SWT;
    import org.eclipse.swt.custom.ViewForm;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.CoolBar;
    import org.eclipse.swt.widgets.CoolItem;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.ToolBar;
    import org.eclipse.swt.widgets.ToolItem;public class demo extends Shell { /**
     * Launch the application
     * 
     * @param args
     */
    public static void main(String args[]) {
    try {
    Display display = Display.getDefault();
    demo shell = new demo(display, SWT.SHELL_TRIM);
    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
    display.sleep();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    } /**
     * Create the shell
     * 
     * @param display
     * @param style
     */
    public demo(Display display, int style) {
    super(display, style);
    createContents();
    setLayout(new FillLayout());
    } /**
     * Create contents of the window
     */
    protected void createContents() {
    setText("SWT Application");
    setSize(378, 241); final ViewForm viewForm = new ViewForm(this, SWT.NONE); final CoolBar coolBar = new CoolBar(viewForm, SWT.NONE);
    viewForm.setTopLeft(coolBar);
    final CoolItem newItemCoolItem = new CoolItem(coolBar, SWT.NONE);

    final ToolBar toolBar = new ToolBar(coolBar, SWT.NONE);
    newItemCoolItem.setControl(toolBar);

    newItemCoolItem.setText("New item");
    final ToolItem newItemToolItem = new ToolItem(toolBar, SWT.PUSH);
    newItemToolItem.setText("设置");
    //添加setSize() toolBar.pack();
    Point point = new Point(toolBar.getSize().x, toolBar.getSize().y);
    newItemCoolItem.setSize(point);
    coolBar.layout();
    viewForm.layout();        
    // 
    } protected void checkSubclass() {
    // Disable the check that prevents subclassing of SWT components

    }}
      

  4.   

    newItemToolItem.setText("设置"); 
    //添加setSize()
    toolBar.pack(); 
    Point point = new Point(toolBar.getSize().x, toolBar.getSize().y); 
    newItemCoolItem.setSize(point); 
    coolBar.layout(); 
    viewForm.layout();