【问题标题】:How to make SWT Ui responsive如何使 SWT Ui 响应式
【发布时间】:2019-07-16 10:47:40
【问题描述】:

我正在使用 SWT 构建 GUI,我正在使用网格布局。但是,当我从任何大小拖动窗口并调整大小时,它会留下大量空白空间。所以

我尝试过使用网格布局。

【问题讨论】:

  • 一个设计合理的 GridLayout 应该是响应式的。特别要查看 GridData 的参数,尤其是 SWT.FILL 和抓取空间。
  • 我更希望让它具有响应性,因为当我调整它的大小时,它只会在 ui 上留下大量空白
  • 使用正确的参数调整大小应该可以正常工作。您确实需要向我们展示示例。

标签: java eclipse swt windowbuilder


【解决方案1】:

看看这个post。以下代码来自GithubGist

public class ResponsiveDemo {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display, SWT.SHELL_TRIM);
        shell.setLayout(new FillLayout());
        shell.setBounds(20, 20, 1200, 600);

        Composite parent = new Composite(shell, SWT.NONE);
        parent.setLayout(new ScaleLayout(new ScaleProvider(parent)));
        parent.setBackground(getSystemColor(SWT.COLOR_LIST_BACKGROUND));
        configure(parent);

        shell.open();
        while (!shell.isDisposed()) {
            if (!shell.getDisplay().readAndDispatch()) {
                shell.getDisplay().sleep();
            }
        }
    }

    private static void configure(Composite parent) {
        ScaleData child1 = createChild(parent, "control1", SWT.COLOR_RED);
        ScaleData child2 = createChild(parent, "control2", SWT.COLOR_GREEN);
        ScaleData child3 = createChild(parent, "control3", SWT.COLOR_BLUE);
        ScaleData child4 = createChild(parent, "control4", SWT.COLOR_CYAN);
        ScaleData child5 = createChild(parent, "control5", SWT.COLOR_YELLOW);

        child1.on(Scale.SUPER_WIDE).setMargin(3, 3, 3, 3);
        child2.on(Scale.SUPER_WIDE).setMargin(3, 3, 3, 3);
        child3.on(Scale.SUPER_WIDE).setWidth(300).setMargin(3, 3, 3, 3);
        child4.on(Scale.SUPER_WIDE).setMargin(3, 3, 3, 3);
        child5.on(Scale.SUPER_WIDE).setMargin(3, 3, 3, 3);

        child1.on(Scale.WIDE).setMargin(3, 3, 3, 3);
        child2.on(Scale.WIDE).setMargin(3, 3, 3, 3);
        child3.on(Scale.WIDE).setWidth(300).setMargin(3, 3, 3, 3);
        child4.on(Scale.WIDE).tie(child5).setHeight(200).setMargin(3, 3, 3, 3);
        child5.on(Scale.WIDE).setMargin(3, 3, 3, 3);

        child1.on(Scale.STANDARD).setMargin(3, 3, 3, 3);
        child2.on(Scale.STANDARD).tie(child3).setMargin(3, 3, 3, 3);
        child3.on(Scale.STANDARD).setWidth(300).setMargin(3, 3, 3, 3);
        child4.on(Scale.STANDARD).tie(child5).setMargin(3, 3, 3, 3);
        child5.on(Scale.STANDARD).setMargin(3, 3, 3, 3);

        child1.on(Scale.COMPACT).setMargin(3, 3, 3, 3);
        child2.on(Scale.COMPACT).tie(child3).setMargin(3, 3, 3, 3);
        child3.on(Scale.COMPACT).tie(child4).setSize(300, 60).setMargin(3, 3, 3, 3);
        child4.on(Scale.COMPACT).tie(child5).setMargin(3, 3, 3, 3);
        child5.on(Scale.COMPACT).setMargin(3, 3, 3, 3);

        child1.on(Scale.SUPER_COMPACT).tie(child2).setMargin(3, 3, 3, 3);
        child2.on(Scale.SUPER_COMPACT).tie(child3).setMargin(3, 3, 3, 3);
        child3.on(Scale.SUPER_COMPACT).tie(child4).setHeight(60).setMargin(3, 3, 3, 3);
        child4.on(Scale.SUPER_COMPACT).tie(child5).setMargin(3, 3, 3, 3);
        child5.on(Scale.SUPER_COMPACT).setMargin(3, 3, 3, 3);
    }

    static ScaleData createChild(Composite parent, String label, int color) {
        return new ScaleData(createChildControl(parent, label, color));
    }

    private static Control createChildControl(Composite parent, String label, int color) {
        Label result = new Label(parent, SWT.WRAP);
        result.setText(label);
        result.setBackground(getSystemColor(color));
        return result;
    }

    private static Color getSystemColor(int colorIndex) {
        return Display.getCurrent().getSystemColor(colorIndex);
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多