0

我的 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;
            }
        };

    });

}

}

4

1 回答 1

4

您可以简单地使用GridLayoutand的组合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");
}

看起来像这样:

在此处输入图像描述

在此处输入图像描述

于 2013-11-14T11:43:17.340 回答