有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSWT中的Resizeble对话框

我有一个复合(容器),它位于另一个复合(对话框区域)内。容器包含一些UI元素。如何使对话框的大小变大或可调整大小。这是我的密码

 protected Control createDialogArea(Composite parent) {
    setMessage("Enter user information and press OK");
    setTitle("User Information");
    Composite area = (Composite) super.createDialogArea(parent);
    Composite container = new Composite(area, SWT.NONE);
    container.setLayout(new GridLayout(2, false));
    container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    Label lblUserName = new Label(container, SWT.NONE);
    lblUserName.setText("User name");

    txtUsername = new Text(container, SWT.BORDER);
    txtUsername.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    txtUsername.setEditable(newUser);
    txtUsername.setText(name);

    return area;
}

共 (1) 个答案

  1. # 1 楼答案

    要使JFace对话框可调整大小,请为isResizable方法添加覆盖:

    @Override
    protected boolean isResizable() {
        return true;
    }
    

    要使对话框在打开时变大,可以在布局上设置宽度或高度提示。例如:

    GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
    data.widthHint = convertWidthInCharsToPixels(75);
    txtUsername.setLayoutData(data);
    

    或者您可以重写getInitialSize(),例如,此代码在水平方向(75个字符)和垂直方向(20行)上为更多字符留出空间:

    @Override
    protected Point getInitialSize() {
        final Point size = super.getInitialSize();
    
        size.x = convertWidthInCharsToPixels(75);
    
        size.y += convertHeightInCharsToPixels(20);
    
        return size;
    }