我的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;
}
};
});
}
}
答案 0 :(得分:4)
您只需使用GridLayout
和GridData
的组合:
protected Shell shell;
public static boolean fullScreen = false;
public static void main(String[] args)
{
try
{
CreateMultipleLineText window = new CreateMultipleLineText();
window.open();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void open()
{
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
}
protected void createContents()
{
shell = new Shell();
shell.setMaximized(true);
shell.setText("Militia Manager");
shell.setLayout(new GridLayout(1, false));
// this creates a composite canvas which houses the start window widgets
final Composite startComposite = new Composite(shell, SWT.NONE);
startComposite.setLayout(new GridLayout(1, false));
startComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
// logo label
Label logoLabel = new Label(startComposite, SWT.NONE);
logoLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
logoLabel.setText("OUR LOGO GOES HERE");
// Button for New Game
Button newGameButton = new Button(startComposite, SWT.PUSH);
newGameButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
newGameButton.setText("New Game");
// Quits
Button quitButton = new Button(startComposite, SWT.NONE);
quitButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
quitButton.setText("Quit");
// When checked sets boolean
final Button fullScreenCheckBox = new Button(startComposite, SWT.CHECK);
fullScreenCheckBox.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
fullScreenCheckBox.setText("Full Screen Mode");
}
看起来像这样: