hum*_*nsg 1 java swt margin composite
我想知道SWT Composite对象的常规/常用做法是什么.
我发现每当我添加一个Composite(带有任何UI示例:TextBox或Button)时,在Composite中创建的UI不会与Composite的起始边缘对齐.(您可以通过设置Composite的背景颜色来观察)
在TextBox UI之前,Composite内部有一些空格/填充.如果未在Composite中创建先前的UI,则会导致我正在创建的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();
}
Run Code Online (Sandbox Code Playgroud)
这将解决它:
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();
}
Run Code Online (Sandbox Code Playgroud)
只需设置marginWidthif GridLayoutto 0.

只是一个提示:您可以在构造函数中设置列数和等宽度GridLayout.
| 归档时间: |
|
| 查看次数: |
6325 次 |
| 最近记录: |