我想知道这里对于 SWT 复合对象的正常/常见做法是什么。
我发现每当我添加一个复合体(使用任何 UI 示例:TextBox 或 Button)时,在复合体中创建的 UI 都不会与复合体的起始边缘对齐。 (您可以通过设置 Composite 的背景颜色来观察这一点)
在 TextBox UI 之前的 Composite 中有一些空间/填充。如果以前的 UI 不是在 Composite 中创建的,这会导致我正在创建的 GUI 表单出现错位。
我想知道使它们保持一致的常见做法是什么?通过设置一些负填充将 Composite 向后移动,使其中的 UI 看起来像是对齐的?
下面是示例代码!
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
GridLayout layout = new GridLayout();
layout.numColumns = 1;
layout.makeColumnsEqualWidth = false;
shell.setLayout(layout);
Text t1 = new Text(shell, SWT.SINGLE | SWT.BORDER);
t1.setText("Test box...");
Composite c = new Composite(shell, SWT.NONE);
// c.setBackground(new Color(shell.getDisplay(), 255,0,0));
layout = new GridLayout();
layout.numColumns = 2;
layout.makeColumnsEqualWidth = true;
c.setLayout(layout);
Text t2 = new Text(c, SWT.SINGLE | SWT.BORDER);
t2.setText("Test box within Composite... not aligned to the first textbox");
Button b = new Button(c, SWT.PUSH);
b.setText("Button 1");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
最佳答案
这将解决它:
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
Text t1 = new Text(shell, SWT.SINGLE | SWT.BORDER);
t1.setText("Test box...");
Composite c = new Composite(shell, SWT.NONE);
GridLayout layout = new GridLayout(2, true);
layout.marginWidth = 0; // <-- HERE
c.setLayout(layout);
Text t2 = new Text(c, SWT.SINGLE | SWT.BORDER);
t2.setText("Test box within Composite... not aligned to the first textbox");
Button b = new Button(c, SWT.PUSH);
b.setText("Button 1");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
如果 GridLayout
为 0
,只需设置 marginWidth
。
提示:您可以在 GridLayout
的构造函数中设置列数和等宽。
关于java - SWT 复合对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12926435/