如何在JavaSWT中显示复合子类

如何在JavaSWT中显示复合子类,java,swt,subclass,composite,Java,Swt,Subclass,Composite,我正在制作一个在一个shell中使用各种不同屏幕的游戏,我试图通过在SWT中使用复合子类来更改屏幕,但是当代码运行时,添加到子类的小部件都没有显示出来 调用组合的主代码- Button newGameButton = new Button(startComposite, SWT.PUSH); FormData fd_newGameButton = new FormData(); fd_newGameButton.left = new FormAttachment

我正在制作一个在一个shell中使用各种不同屏幕的游戏,我试图通过在SWT中使用复合子类来更改屏幕,但是当代码运行时,添加到子类的小部件都没有显示出来

调用组合的主代码-

         Button newGameButton = new Button(startComposite, SWT.PUSH);
    FormData fd_newGameButton = new FormData();
    fd_newGameButton.left = new FormAttachment(0, 296);
    newGameButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
    newGameButton.setText("New Game");
    newGameButton.addSelectionListener(new SelectionListener() {

        public void widgetSelected(SelectionEvent event) {
            if (fullScreen == true) {
                startComposite.dispose();
                shell.setFullScreen(true);
            } else {
                startComposite.dispose();
            }

            GameComposite gameComposite = new GameComposite(shell, SWT.NONE);
            gameComposite.setLayout(new GridLayout(1, false));
            gameComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        }

        public void widgetDefaultSelected(SelectionEvent event) {
            if (fullScreen == true) {
                startComposite.dispose();
                shell.setFullScreen(true);
            } else {
                startComposite.dispose();
            }

            GameComposite gameComposite = new GameComposite(shell, SWT.NONE);
            gameComposite.setLayout(new GridLayout(1, false));
            gameComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
这是子类代码-

     public class GameComposite extends Composite {
private Text text;

/**
 * Create the composite.
 * @param parent
 * @param style
 */
public GameComposite(Composite parent, int style) {
    super(parent, style);

    Group gamePanel = new Group(this, SWT.NONE);
    gamePanel.setText("GamePanel1");
    gamePanel.setBounds(518, 10, 196, 502);

    text = new Text(this, SWT.BORDER);
    text.setEditable(false);
    text.setBounds(10, 418, 285, 94);


}

@Override
protected void checkSubclass() {
    // Disable the check that prevents subclassing of SWT components
}
}

  • 设置布局后,需要调用
    layout()
    。这可能应该在构造器中完成,除非你真的想为你的
    GameComposite
    使用几种不同的布局

  • 您可以子类化
    Composite
    ,因此无需重写
    checkSubclass
    (这是一个坏习惯!)


  • 对不起,我对GUI编码一无所知,只是在大学开始了我的第一个学期!我怎么称呼布局?在子类中?
    this.setLayout(…)
    或只是
    setLayout(…)
    已经在这里完成了-GameComposite GameComposite=newgamecomposite(shell,SWT.NONE);setLayout(新的GridLayout(1,false));setLayoutData(新的GridData(SWT.FILL,SWT.FILL,true,true));是的,正如我所说,这应该在构造函数中完成,除非每次创建
    GameComposite
    后都要调用这些方法。但这只是关于如何改进代码的建议。重要的一点是,在设置布局之后,还应该调用游戏组合的
    layout()
    方法。而且,由于您更改了game composite的布局数据,因此还需要调用其父级的
    layout()
    方法。