gpt4 book ai didi

java - 如何使 SWT Ui 响应式

转载 作者:行者123 更新时间:2023-12-02 01:25:15 25 4
gpt4 key购买 nike

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

我尝试过使用网格布局。

最佳答案

看看这个 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);
}

}

关于java - 如何使 SWT Ui 响应式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57055536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com