java - 小部件和复合 Material 不会根据外壳大小调整大小

标签 java layout resize swt composite

我的 GUI 遇到了很大的麻烦,每当屏幕最大化时,小部件和复合 Material 似乎都没有在屏幕上延伸或排列 - 我尝试过使用布局,但它们没有给我正确的选择结果。我需要它 -

(居中)

标志

按钮

按钮

复选框

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

    });

}

}

最佳答案

您可以简单地使用 GridLayoutGridData 的组合:

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");
}

看起来像这样:

enter image description here

enter image description here

关于java - 小部件和复合 Material 不会根据外壳大小调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19974105/

相关文章:

java - JVMTI 代理崩溃

javascript - 透明 GIF 在 IE6 中显示在内容后面

jquery - 拖动调整 Bootstrap 列大小 jQuery UI

java - 如何使用 Java 调整图像大小?

c# - 在不使用 WinForms 的情况下更改 DataVisualization.Chart 大小

html - 如何防止浏览器窗口调整大小时 block 偏移发生 1-2px 的变化

java - 显示 Java 文件而不是 XML 文件中的文本 (Android Studio)

java - 比较不同正则表达式的性能,需要澄清

java - Maven 3 : Failed to execute goal org. apache.maven.plugins :maven-archetype-plugin:2. 2:生成

java子图显示