下面附加的程序创建了一个 TitleAreaDialog,其中包含四个组。每个组都包含控件,最后一个是“动态”,即在组合框中选择条目时,会生成一定数量的控件。
现在,幸运的是,对话框会自动调整大小——但我希望对调整大小有更多的控制。就像现在一样,对话大小在组之间“分布”均匀。我想要完成的是只放大(或缩小)最后一组,即使用户用鼠标调整对话框的大小。三个静态组的大小(高度)应保持固定。
有没有办法做到这一点?蒂亚!
public class DialogExample extends ApplicationWindow {
class MyDialog extends TitleAreaDialog {
MyDialog(Shell shell) {
super(shell);
setShellStyle(getShellStyle() | SWT.MAX | SWT.RESIZE);
}
private Composite createComposite(Composite parent) {
Composite c = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout(1, false);
layout.horizontalSpacing = 10;
c.setLayout(layout);
c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
return c;
}
private Group createGroup(Composite parent, String title) {
Group newGroup = new Group(parent, SWT.NONE);
newGroup.setText(title);
newGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
GridLayout layout = new GridLayout();
newGroup.setLayout(layout);
return newGroup;
}
private Group createGroup(Composite parent, int controlCount) {
Group newGroup = createGroup(parent, "Group " + controlCount);
for (int i = 0; i < controlCount; i++) {
Label l = new Label(newGroup, SWT.NONE);
l.setText("Label " + (i + 1));
}
return newGroup;
}
private Group createControlGroup(final Composite parent) {
final Group newGroup = createGroup(parent, "Control Group");
final Combo combo = new Combo(newGroup, SWT.READ_ONLY);
combo.add("1");
combo.add("2");
combo.add("3");
combo.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent event) {
for (Control c: newGroup.getChildren()) {
if (c != combo) {
c.dispose();
}
}
for (int i = 0; i <combo.getSelectionIndex() + 1; i++) {
Label l = new Label(newGroup, SWT.None);
l.setText("Dynamic Label " + (i + 1));
}
parent.getShell().pack();
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
}
});
return newGroup;
}
public void create() {
super.create();
setTitle("Title");
setMessage("Message");
}
protected Control createDialogArea(Composite parent) {
Composite main = createComposite(parent);
createGroup(main, 1);
createGroup(main, 2);
createGroup(main, 3);
createControlGroup(main);
return main;
}
}
public DialogExample()
{
super(null);
}
public void run() {
setBlockOnOpen(true);
open();
Display.getCurrent().dispose();
}
protected Control createContents(Composite parent) {
Label label = new Label(parent, SWT.CENTER);
label.setText("Dialog example");
TitleAreaDialog dialog = new MyDialog(parent.getShell());
dialog.open();
return label;
}
public static void main(String... args) {
new DialogExample().run();
}
}