Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 布局问题:自动增长标签(SWT)_Java_Layout_Swt_Autogrow - Fatal编程技术网

Java 布局问题:自动增长标签(SWT)

Java 布局问题:自动增长标签(SWT),java,layout,swt,autogrow,Java,Layout,Swt,Autogrow,我正在使用GridLayout,试图在不隐藏任何内容的情况下使标签自动增长。这里有一个简单的测试代码:每次按下按钮,标签文本都会变大,但只有在水平调整窗口大小后,我才能获得正确的布局。 有没有办法在不调整窗口大小的情况下解决这个问题? 我想我已经试过了所有的房子,但我还是不能让它工作,它快把我逼疯了 这就是我得到的 应该是这样的 import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import

我正在使用GridLayout,试图在不隐藏任何内容的情况下使标签自动增长。这里有一个简单的测试代码:每次按下按钮,标签文本都会变大,但只有在水平调整窗口大小后,我才能获得正确的布局。 有没有办法在不调整窗口大小的情况下解决这个问题? 我想我已经试过了所有的房子,但我还是不能让它工作,它快把我逼疯了

这就是我得到的

应该是这样的

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

public class UItest {

    protected Shell shell;
    private Label   label;

    public static void main(String[] args) {
        try {
            UItest window = new UItest();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    protected void createContents() {
        shell = new Shell();
        shell.setSize(450, 300);
        shell.setText("SWT Application");
        shell.setLayout(new GridLayout(1, false));

        Button button = new Button(shell, SWT.NONE);
        button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent arg0) {
                label.setText(label.getText() + " " + label.getText());
            }
        });
        button.setText("New Button");

        label = new Label(shell, SWT.WRAP);
        label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
        label.setText("New Label");

        List list = new List(shell, SWT.BORDER);
        list.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    }

    protected Label getLabel() {
        return label;
    }
}

谢谢您抽出时间。


通过这些更改解决:

Button button = new Button(shell, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent arg0) {
        label.setText(label.getText() + " " + label.getText());
        shell.layout(); // ADDED THIS
    }
});
button.setText("New Button");

label = new Label(shell, SWT.WRAP);
// SET HORIZONTAL GRAB ON LABEL (FIRST TRUE IN GridData CONSTRUCTOR)
label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
label.setText("New Label");

我认为,问题在于,当文本更改时,布局不会无效-尝试强制重新显示(通过调用
getShell().layout(true,true)