1

我试图制作一个扩展 TitleAreaDialog 的非常基本的对话框,它创建了这个令人沮丧的附加垂直空间,我似乎无法找到创建这个空间的原因并阻止它发生。下面是截图和代码。

http://imgur.com/tEGPbeA

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 中创建布局对我来说总是一场战斗,非常感谢任何帮助。

4

2 回答 2

1

“罪犯”可以在 中找到TitleAreaDialog.getInitialSize()。它使用一些硬编码常量来实现最小尺寸。

根据您希望对话框的外观,有几种解决方案:

  • 覆盖getInitialSize()并指定您想要的任何大小
  • 覆盖initializeBounds()并设置您自己的一些大小,或调用this.getShell().pack(). 在这种情况下,不要忘记首先调用super.initializeBounds(),因为从我从代码中看到的,它似乎不仅仅是初始化边界。
于 2013-11-06T06:45:23.410 回答
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;
}
于 2013-11-05T21:42:02.453 回答