SWT - 如何动态更改文本框的大小

时间:2018-01-26 18:04:20

标签: swt eclipse-rcp rcp

我正在尝试创建一个包含组合,文本框和浏览按钮的简单UI。组合将包含两个值:执行时间使用文件执行。 选择执行时间选项后,应显示后跟文本框的组合框。 enter image description here

enter image description here

选择执行文件选项时,应显示组合框,文本框和浏览按钮。

enter image description here

当我在这些选项之间切换时,小部件未正确对齐。请参阅下图。文本框大小未扩展到可用空间。 Image 4

public class TestUI {

    public static void main(String[] args)
    {
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setText("StackOverflow");
        shell.setLayout(new GridLayout(1, true));

        Composite composite = new Composite(shell, SWT.NONE);
        composite.setLayout(new GridLayout(3, false));
        composite.setLayoutData(new GridData(GridData.FILL_BOTH));

        Combo combo = new Combo(composite, SWT.READ_ONLY);
        String[] input = { "Execution Times", "Execute with File" };
        combo.setItems(input);

        Text loopText = new Text(composite, SWT.SINGLE | SWT.BORDER);
        GridData gridData = new GridData(SWT.BEGINNING | GridData.FILL_HORIZONTAL);
        gridData.horizontalSpan = 2;
        loopText.setLayoutData(gridData);
        loopText.setEnabled(false);

        Button browseButton = new Button(composite, SWT.PUSH);
        browseButton.setText("Browse...");
        browseButton.setVisible(false);

        combo.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                String text2 = combo.getText();
                System.out.println(text2);

                if (text2.equals("Execution Times")) {
                    loopText.setEnabled(true);
                    loopText.setText("1");//$NON-NLS-1$
                    GridData gridData1 = new GridData(SWT.BEGINNING, SWT.TOP, false, false);
                    gridData1.grabExcessHorizontalSpace = true;
                    gridData1.horizontalSpan = 2;
                    loopText.setLayoutData(gridData1);
                    browseButton.setVisible(false);
                    loopText.getParent().layout();
                }
                if (text2.equals("Execute with File")) {
                    GridData gridData1 = new GridData(SWT.BEGINNING, SWT.TOP, false, false);
                    gridData1.grabExcessHorizontalSpace = true;
                    loopText.setLayoutData(gridData1);
                    gridData.exclude= false;
                    browseButton.setVisible(true);
                    browseButton.setFocus();
                    loopText.setText("");
                    loopText.setEnabled(false);
                    loopText.getParent().layout();
                }
            }

        });

        shell.pack();
        shell.open();
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}

任何人都可以帮我吗?

2 个答案:

答案 0 :(得分:3)

据我所知,根据组合选择,文本字段和文本字段加按钮有不同的用途:

  • 选择执行时间时,输入次数
  • 否则使用文件执行需要输入或浏览文件名

因此,我会使用组合小部件旁边的Composite来保存文本字段以输入数字(甚至是Spinner)或文本字段和按钮来输入/选择一个文件名。

Composite composite = new Composite( parent, SWT.NONE );
Text executionTimesText = new Text( composite, SWT.BORDER );
composite.setLayout( new StackLayout() );
Composite executionFileComposite = new Composite( composite, SWT.NONE );
// use a GridLayout to position the file name text field and button within the executionFileComposite
combo.addListener( SWT.Selection, event -> {
  StackLayout layout = ( StackLayout )composite.getLayout();
  if( combo.getSelectionIndex() == 0 ) {
    layout.topControl = executionTimesText;
  } else if( combo.getSelectionIndex() == 1 ) {
    layout.topControl = executionFileComposite;
  }
  composite.layout();
}

StackLayout允许您堆叠不同的输入字段并根据需要切换它们(即根据组合的选择)。

答案 1 :(得分:0)

对于初学者,您不需要每次都为GridData小部件重新创建Text。相反,只需通过gridData.horizontalSpan修改原始文件,或者如果在实践中您无法访问GridData实例,则可以通过((GridData) gridData.getLayoutData()).horizontalSpan等获取该文件。

您在Shell底部看到空白区域的原因是您创建了一个包含3列的布局,然后添加了以下内容:

  1. Combo
  2. TexthorizontalSpan设为2,因此使用2列)
  3. Button
  4. ComboText会占用所有3列,因此会为Button添加新行。然后调用pack(),计算首选大小,该行将为2行,第一行仅为2个小部件调整大小。

    我们可以通过pack()Shell上设置尺寸,而不是调用Shell并将Shell.setSize(...)的尺寸缩小到首选尺寸。一般情况下,您不希望混合使用setSize(...)和布局,但是您已使用“RCP”标记了帖子,因此您的Shell已经有了一个大小,并且您不会手动调用{ {1}}和pack()

    完整示例:

    open()

    或者,如果您实际上是在创建并打开新的public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); shell.setSize(300, 80); shell.setText("StackOverflow"); shell.setLayout(new GridLayout(1, true)); Composite composite = new Composite(shell, SWT.NONE); composite.setLayout(new GridLayout(3, false)); composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); final Combo combo = new Combo(composite, SWT.READ_ONLY); String[] input = {"Execution Times", "Execute with File"}; combo.setItems(input); final Text loopText = new Text(composite, SWT.SINGLE | SWT.BORDER); final GridData textGridData = new GridData(SWT.FILL, SWT.FILL, true, false); textGridData.horizontalSpan = 2; loopText.setLayoutData(textGridData); loopText.setEnabled(false); final Button browseButton = new Button(composite, SWT.PUSH); browseButton.setText("Browse..."); browseButton.setVisible(false); combo.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { String text2 = combo.getText(); System.out.println(text2); if (text2.equals("Execution Times")) { loopText.setEnabled(true); loopText.setText("1"); // Can also do ((GridData) textGridData.getLayoutData())... textGridData.grabExcessHorizontalSpace = true; textGridData.horizontalSpan = 2; browseButton.setVisible(false); loopText.getParent().layout(); } if (text2.equals("Execute with File")) { loopText.setEnabled(false); loopText.setText(""); textGridData.grabExcessHorizontalSpace = true; textGridData.horizontalSpan = 1; browseButton.setVisible(true); browseButton.setFocus(); loopText.getParent().layout(); } } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } ,那么在使Shell小部件占用两列之前调用pack()(以获取首选大小):< / p>

    Text

    我们所做的是添加所有3个小部件而不调整shell.pack(); // Move these two lines down to the end textGridData.horizontalSpan = 2; browseButton.setVisible(false); shell.layout(true, true); shell.open(); 。然后,调用horizontalSpan设置pack()的大小,假设所有3个小部件都出现在一行中。致电Shell后,将pack()设置为2,然后隐藏horizontalSpan。打开Button后,您会看到:

    enter image description here