0

我正在制作一个在一个 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
}

}

4

1 回答 1

0
  1. 您需要layout()在设置布局后调用。这可能应该在构造函数中完成,除非你真的想为你的GameComposite.

  2. 您可以继承子类Composite,因此无需覆盖checkSubclass(这是一个坏习惯!)

于 2013-11-15T06:11:09.887 回答