如何将SWT对象绑定到应用程序的中心window?

How to bind SWT objects to the center of the application window?

我正在使用 SWT 创建应用程序 GUI,我真的不需要调整组件的大小,但令我困扰的是当 window 最大化时,组件保持左对齐。有没有办法用 SWT 解决这个问题,还是我需要使用一组不同的 GUI 工具?

提前致谢。我为此应用程序使用 SWT 4.8。

编辑:图片

小:https://imgur.com/CPbAlaZ

最大化:https://imgur.com/4d6YXcl

提供的图像是使用以下代码的基本应用程序

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

public class TestWindow {

    protected Shell shlSwtApplicationExample;
    private Text text;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            TestWindow window = new TestWindow();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shlSwtApplicationExample.open();
        shlSwtApplicationExample.layout();
        while (!shlSwtApplicationExample.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shlSwtApplicationExample = new Shell();
        shlSwtApplicationExample.setSize(705, 529);
        shlSwtApplicationExample.setText("SWT Application Example");

        Composite composite = new Composite(shlSwtApplicationExample, SWT.NONE);
        composite.setBounds(10, 10, 669, 465);

        text = new Text(composite, SWT.BORDER);
        text.setBounds(22, 10, 334, 295);

        Button btnNewButton = new Button(composite, SWT.NONE);
        btnNewButton.setBounds(49, 384, 137, 26);
        btnNewButton.setText("New Button");

        Button button = new Button(composite, SWT.NONE);
        button.setText("New Button");
        button.setBounds(300, 384, 137, 26);

    }
}

由于您使用的是 setBounds,因此您需要向 shell 添加一个控件侦听器,以了解有关调整大小和移动事件的信息。然后您将不得不重新计算每个调整大小事件的位置。

shlSwtApplicationExample.addControlListener(
    new ControlListener() {
        @Override
        public void controlMoved(ControlEvent event) {
            // No action
        }

        @Override
        public void controlResized(ControlEvent event) {
           Rectangle rect = shlSwtApplicationExample.getClientArea();

           // TODO Call new `setBounds` on each control based on the 
           // client area size
        }
    });

现在可能是了解使用布局而不是 setBounds 的好时机(请参阅 here)。布局将自动处理调整大小。

我不建议使用 setBounds,因为它不会在您调整应用程序大小时调整组件的大小。使用布局,如下例所示,我对 ShellComposite 使用了 GridLayout,这将在调整大小时正确排列 UI。

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

public class TestWindow {

    protected Shell shlSwtApplicationExample;
    private Text text;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            TestWindow window = new TestWindow();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents(display);
        shlSwtApplicationExample.open();
        shlSwtApplicationExample.layout();
        shlSwtApplicationExample.setLayout(new GridLayout(1, false));
        while (!shlSwtApplicationExample.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     * @param display 
     */
    protected void createContents(Display display) {
        shlSwtApplicationExample = new Shell(display);
        shlSwtApplicationExample.setLayout(new GridLayout(1, false));

        Composite txtcomposite = new Composite(shlSwtApplicationExample, SWT.NONE);
        txtcomposite.setLayout(new GridLayout(1, false));
        txtcomposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        Composite btncomposite = new Composite(shlSwtApplicationExample, SWT.NONE);
        btncomposite.setLayout(new GridLayout(2, false));
        btncomposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

        text = new Text(txtcomposite, SWT.BORDER);
        text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        Button btnNewButton = new Button(btncomposite, SWT.NONE);
        btnNewButton.setText("New Button");

        Button button = new Button(btncomposite, SWT.NONE);
        button.setText("New Button");

        shlSwtApplicationExample.setText("SWT Application Example");
        //shlSwtApplicationExample.setSize(705, 529);

    }
}