我试图制作一个扩展 TitleAreaDialog 的非常基本的对话框,它创建了这个令人沮丧的附加垂直空间,我似乎无法找到创建这个空间的原因并阻止它发生。下面是截图和代码。
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 中创建布局对我来说总是一场战斗,非常感谢任何帮助。