我试图创建一个扩展TitleAreaDialog的非常基本的对话框,它创建了这个令人沮丧的additoinal垂直空间,我似乎无法找到创建此空间的内容并阻止它发生。下面是截图和代码。
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class ImageDialog extends TitleAreaDialog {
public ImageDialog(Shell arg0) {
super(arg0);
}
@Override
protected void configureShell(Shell shell) {
super.configureShell(shell);
Display display = Display.getCurrent();
Rectangle bounds = display.getPrimaryMonitor().getBounds();
Rectangle rect = shell.getBounds();
int x = bounds.x + (bounds.width - rect.width) / 2;
int y = bounds.y + (bounds.height - rect.height) / 2;
shell.setLocation(x,y);
}
@Override
protected void setShellStyle(int arg0) {
super.setShellStyle(SWT.CLOSE| SWT.MODELESS | SWT.BORDER | SWT.TITLE);
setBlockOnOpen(false);
}
private Text txtLink;
private String link;
@Override
public void create() {
super.create();
setTitle("Enter an image url");
setMessage("Valid formats are .gif, .jp(e)g, .bmp, .png", IMessageProvider.INFORMATION);
}
@Override
protected Control createDialogArea(Composite parent) {
//Composite area = (Composite) super.createDialogArea(parent);
Composite container = new Composite(parent, SWT.NONE);
container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
GridLayout layout = new GridLayout(2, false);
//container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
container.setLayout(layout);
createLinkText(container);
return parent;
}
private void createLinkText(Composite container) {
Label lbtLink = new Label(container, SWT.NONE);
lbtLink.setText("Link: ");
GridData dataLink = new GridData();
dataLink.grabExcessHorizontalSpace = true;
dataLink.horizontalAlignment = GridData.FILL;
txtLink = new Text(container, SWT.BORDER);
txtLink.setLayoutData(dataLink);
}
@Override
protected void okPressed() {
System.out.println(getInitialSize());
this.link = txtLink.getText();
super.okPressed();
}
public String getLink() {
return link;
}
}
在swt中创建布局对我来说总是一场战斗,非常感谢任何帮助。
答案 0 :(得分:1)
“罪犯”可以在TitleAreaDialog.getInitialSize()
中找到。它使用一些硬编码常量来实现最小尺寸。
根据您希望对话框的外观,有几种解决方案:
getInitialSize()
并指定您想要的任何尺寸initializeBounds()
并设置一些您自己的尺寸,或致电this.getShell().pack()
。在这种情况下,不要忘记先调用super.initializeBounds()
因为从我从代码中看到的内容,它似乎不仅仅是初始化边界。答案 1 :(得分:0)
您需要使用Composite
返回的super.createDialogArea
:
@Override
protected Control createDialogArea(Composite parent) {
Composite area = (Composite) super.createDialogArea(parent);
Composite container = new Composite(area, SWT.NONE);
container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
GridLayout layout = new GridLayout(2, false);
container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
container.setLayout(layout);
createLinkText(container);
return parent;
}