2013-11-14 19 views
0

我正在製作一個遊戲,在一個shell中使用各種不同的屏幕,我試圖通過在SWT中使用複合子類來更改屏幕,但是當代碼運行時,沒有添加到子類的小部件正在顯示。如何獲得複合子類在Java中顯示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 
} 

}

回答

0
  1. 您需要設置佈局之後調用layout()。這應該可以在構造函數中完成,除非您實際上想爲GameComposite使用幾種不同的佈局。

  2. 你被允許繼承Composite,所以沒有必要重寫checkSubclass(這是一個壞習慣!)

+0

對不起,我全新的GUI編碼,纔開始我的第一學期在大學!我將如何調用佈局?在子類中? –

+0

'this.setLayout(...)'或者'setLayout(...)'。 –

+0

它已經在這裏完成 - GameComposite gameComposite = new GameComposite(shell,SWT.NONE); gameComposite.setLayout(new GridLayout(1,false)); gameComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true)); –