GridLayout 和 Dialog:如何正确对齐项目
GridLayout and Dialog: How to align items correctly
我想在 SWT 中创建一个对话框。我的代码如下所示:
Shell dialog = new Shell(dialogShell, SWT.DIALOG_TRIM
| SWT.APPLICATION_MODAL);
dialog.setSize(800, 500);
dialog.setBounds(10, 10, 200, 240);
dialog.setText("Dummy dialog for Whosebug");
GridLayout gridLayout = new GridLayout(4, false);
dialog.setLayout(gridLayout);
Button btnSave = new Button(dialog, SWT.PUSH | SWT.CENTER);
btnSave.setText("Save");
btnSave.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true, 1, 1));
Button btnCancel = new Button(dialog, SWT.PUSH | SWT.CENTER);
btnCancel.setText("Cancel");
btnCancel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true, 1,
1));
Tree tree = new Tree(dialog, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
GridData gridData = new GridData(GridData.FILL);
gridData.horizontalSpan = 4;
gridData.verticalSpan = 10;
tree.setLayoutData(gridData);
TreeItem item = new TreeItem(tree, 0);
item.setText("Item 1");
item = new TreeItem(item, 0);
item.setText("Item 2");
item = new TreeItem(item, 0);
item.setText("Item 3");
Label lblOne = new Label(dialog, SWT.NONE);
lblOne.setText("Label One: ");
Text textOne = new Text(dialog, SWT.SINGLE | SWT.BORDER);
gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalSpan = 3;
textOne.setLayoutData(gridData);
Label lblTwo = new Label(dialog, SWT.NONE);
lblTwo.setText("Label Two: ");
gridData = new GridData();
gridData.verticalAlignment = SWT.TOP;
lblTwo.setLayoutData(gridData);
Text textTwo = new Text(dialog, SWT.SINGLE | SWT.BORDER);
gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalSpan = 3;
textTwo.setLayoutData(gridData);
dialog.pack();
dialog.open();
结果如下所示:
我无法达到以下 "goals":
- 扩大对话框的宽度,以便 header 文本完全可读
- 放大树,使其高度至少有八行
- 为 Text 控件设置一个最小宽度,使其在开始时没有数据填充时不会收缩
感谢您的帮助!
请尽量不要使用 dialog.pack()
,而是在开始时为您的元素指定任意大小。然后你可以在一些事件处理程序中 pack()
它,当你认为它合适时。另外,我有一段时间没有在 SWT 中做任何事情,但是您是否尝试过 minimumWidth
属性?
使用 GridData
heightHint
和 widthHint
值指定树和文本控件的建议高度和宽度。
例如:
gridData.widthHeight = 200;
我想在 SWT 中创建一个对话框。我的代码如下所示:
Shell dialog = new Shell(dialogShell, SWT.DIALOG_TRIM
| SWT.APPLICATION_MODAL);
dialog.setSize(800, 500);
dialog.setBounds(10, 10, 200, 240);
dialog.setText("Dummy dialog for Whosebug");
GridLayout gridLayout = new GridLayout(4, false);
dialog.setLayout(gridLayout);
Button btnSave = new Button(dialog, SWT.PUSH | SWT.CENTER);
btnSave.setText("Save");
btnSave.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true, 1, 1));
Button btnCancel = new Button(dialog, SWT.PUSH | SWT.CENTER);
btnCancel.setText("Cancel");
btnCancel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true, 1,
1));
Tree tree = new Tree(dialog, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
GridData gridData = new GridData(GridData.FILL);
gridData.horizontalSpan = 4;
gridData.verticalSpan = 10;
tree.setLayoutData(gridData);
TreeItem item = new TreeItem(tree, 0);
item.setText("Item 1");
item = new TreeItem(item, 0);
item.setText("Item 2");
item = new TreeItem(item, 0);
item.setText("Item 3");
Label lblOne = new Label(dialog, SWT.NONE);
lblOne.setText("Label One: ");
Text textOne = new Text(dialog, SWT.SINGLE | SWT.BORDER);
gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalSpan = 3;
textOne.setLayoutData(gridData);
Label lblTwo = new Label(dialog, SWT.NONE);
lblTwo.setText("Label Two: ");
gridData = new GridData();
gridData.verticalAlignment = SWT.TOP;
lblTwo.setLayoutData(gridData);
Text textTwo = new Text(dialog, SWT.SINGLE | SWT.BORDER);
gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalSpan = 3;
textTwo.setLayoutData(gridData);
dialog.pack();
dialog.open();
结果如下所示:
我无法达到以下 "goals":
- 扩大对话框的宽度,以便 header 文本完全可读
- 放大树,使其高度至少有八行
- 为 Text 控件设置一个最小宽度,使其在开始时没有数据填充时不会收缩
感谢您的帮助!
请尽量不要使用 dialog.pack()
,而是在开始时为您的元素指定任意大小。然后你可以在一些事件处理程序中 pack()
它,当你认为它合适时。另外,我有一段时间没有在 SWT 中做任何事情,但是您是否尝试过 minimumWidth
属性?
使用 GridData
heightHint
和 widthHint
值指定树和文本控件的建议高度和宽度。
例如:
gridData.widthHeight = 200;