1

コンポジットでcoolBarを使用しようとしたときに、このコンポジットをアプリケーションに埋め込むと問題が発生します。coolBarは単に表示されません。この問題は、toolBarや他のコンポジットなどの別のツールでは発生しません。何を間違えたり忘れたりすることができますか?

コードに従う前に、システムを参照します。

  • Win7
  • Eclipse:バージョン:Indigo Serviceリリース2ビルドID:20120216-1857
  • Google WindowBuilder 1.5.0 Google
  • プラグイン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);            
    }
}

助けてくれてありがとう。

4

2 に答える 2

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

リンク

于 2012-11-23T07:40:26.360 に答える
1

クールバーのレイアウト データを設定していないことが原因である可能性があります。レイアウトの仕組みを理解するには、この記事を参照してください。

于 2012-11-23T06:14:47.407 に答える