如何使SWT窗口/外壳及其上的所有组件可调节?

问题描述 投票:1回答:1

所以我在应用程序中创建了带按钮的窗口/外壳,但是我希望在展开时调整大小,而不是留在一个角落。我已经使用了SWT和窗口构建器来实现这一点我使用了绝对布局,现在当我按全屏时它全部在一个角落我怎么能使这美观,所以所有的按钮和标签也扩展了?

user-interface resize swt windowbuilder
1个回答
0
投票

请查看SWT中的标准布局。请参阅How to position your widgetsUnderstanding Layouts

例如,下面是一个示例代码,我在网格布局中创建了2个标签和2个文本,在调整大小时将水平填充。您可以根据需要进行更改。

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SampleApplication
{

    protected Shell shell;
    private Text text;
    private Text text_1;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(final String[] args)
    {
        try
        {
            SampleApplication window = new SampleApplication();
            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.setSize(450, 224);
        shell.setText("SWT Application");
        shell.setLayout(new GridLayout(2, false));

        Label lblNewLabel = new Label(shell, SWT.NONE);
        lblNewLabel.setText("Name");
        lblNewLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));

        text = new Text(shell, SWT.BORDER);
        text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        Label lblNewLabel_1 = new Label(shell, SWT.NONE);
        lblNewLabel_1.setText("ID");
        lblNewLabel_1.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));

        text_1 = new Text(shell, SWT.BORDER);
        text_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.