5

私は 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");
4

1 に答える 1

7

問題は、テキストが変更されたときにレイアウトが無効にされないことだと思います-(を呼び出してgetShell().layout(true, true))再レイアウトを強制してみてください。

于 2010-08-17T09:00:53.520 に答える