2012-11-22 63 views
1

我有一個問題,當我嘗試在複合中使用coolBar,然後我將這個複合體嵌入到應用程序中。 coolBar根本不會出現。這個問題不適用於其他工具,如toolBar和其他複合材料。我能做什麼錯或遺忘?CoolBar(java SWT WindowBuilder)Composite沒有出現在應用程序中

下面的代碼之前,我是指我的系統:

  • Win7的
  • 的Eclipse:版本:靛藍服務發佈2版本ID:20120216-1857
  • 谷歌的WindowBuilder 1.5.0谷歌
  • 插件3.1.0
  • SWT Designer 1.5.0
  • Google Web Toolkit 2.4.0

複合代碼:

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);    
    } 
} 

感謝您的幫助。

回答

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)); 

鏈接

1

這可能只是因爲你沒有設置coolbar的佈局數據。瞭解佈局如何工作,請參閱this article

相關問題