我的 GUI 遇到了很大的问题,每当屏幕最大化时,小部件和合成似乎不会在屏幕上伸展或排列 - 我尝试使用布局,但它们没有给我正确的结果。我需要它——
(居中)
标识
按钮
按钮
复选框
import org.eclipse.swt.SWT;
public class StartWindow {
protected Shell shell;
public static boolean fullScreen = false;
/**
* Launch the application.
*
* @param args
*/
public static void main(String[] args) {
try {
StartWindow window = new StartWindow();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setMaximized(true);
shell.setText("Militia Manager");
shell.setLayout(new FillLayout(SWT.HORIZONTAL));
// this creates a composite canvas which houses the start window widgets
final Composite startComposite = new Composite(shell, SWT.NONE);
startComposite.setLayout(new FormLayout());
// logo label
Label logoLabel = new Label(startComposite, SWT.NONE);
FormData fd_logoLabel = new FormData();
fd_logoLabel.top = new FormAttachment(0, 12);
fd_logoLabel.left = new FormAttachment(0, 276);
logoLabel.setLayoutData(fd_logoLabel);
logoLabel.setAlignment(SWT.CENTER);
logoLabel.setText("OUR LOGO GOES HERE");
// Button for New Game
Button newGameButton = new Button(startComposite, SWT.PUSH);
FormData fd_newGameButton = new FormData();
fd_newGameButton.left = new FormAttachment(0, 296);
newGameButton.setLayoutData(fd_newGameButton);
newGameButton.setText("New Game");
newGameButton.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
if (fullScreen == true) {
startComposite.dispose();
shell.setFullScreen(true);
} else {
startComposite.dispose();
}
}
public void widgetDefaultSelected(SelectionEvent event) {
if (fullScreen == true) {
startComposite.dispose();
shell.setFullScreen(true);
} else {
startComposite.dispose();
}
}
});
// Quits
Button quitButton = new Button(startComposite, SWT.NONE);
fd_newGameButton.bottom = new FormAttachment(quitButton, -30);
FormData fd_quitButton = new FormData();
fd_quitButton.top = new FormAttachment(0, 323);
fd_quitButton.left = new FormAttachment(0, 313);
quitButton.setLayoutData(fd_quitButton);
quitButton.setText("Quit");
quitButton.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
shell.dispose();
}
public void widgetDefaultSelected(SelectionEvent event) {
shell.dispose();
}
});
// listens for escape key if in fullscreen
shell.addListener(SWT.Traverse, new Listener() {
public void handleEvent(Event event) {
switch (event.detail) {
case SWT.TRAVERSE_ESCAPE:
shell.setFullScreen(false);
event.detail = SWT.TRAVERSE_NONE;
event.doit = false;
break;
}
}
});
// When checked sets boolean
final Button fullScreenCheckBox = new Button(startComposite, SWT.CHECK);
FormData fd_fullScreenCheckBox = new FormData();
fd_fullScreenCheckBox.bottom = new FormAttachment(100, -10);
fd_fullScreenCheckBox.right = new FormAttachment(100, -10);
fullScreenCheckBox.setLayoutData(fd_fullScreenCheckBox);
fullScreenCheckBox.setText("Full Screen Mode");
fullScreenCheckBox.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
if (fullScreenCheckBox.getSelection()) {
fullScreen = true;
} else {
fullScreen = false;
}
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
if (fullScreenCheckBox.getSelection()) {
fullScreen = true;
} else {
fullScreen = false;
}
};
});
}
}