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