【问题标题】:How to wait for a shell to be closed from another shell in swt?如何等待一个shell从swt中的另一个shell关闭?
【发布时间】:2012-03-18 00:20:18
【问题描述】:

我正在用 SWT 创建一个程序。我有第一个外壳,上面有“添加用户”按钮。 当您单击该按钮时,会出现第二个外壳。 在这种情况下,第一个 shell 仍然是 cccable 和可聚焦的。我无法理解如何避免第一个 shell 可聚焦,直到第二个 shell 关闭。

此行为是对话框的默认行为,但我希望在 shell 中具有相同的行为。你知道我怎样才能得到它吗?

我用来打开第二个 shell 的代码是这样的:

Display display = Menu.this.getDisplay();
AddEditUser shell = new AddEditUser(display);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) {
        display.sleep();
    }
}

谢谢

我听从了你的建议,现在行为还可以,但是现在第二个 shell 没有如图所示的顶栏。

【问题讨论】:

    标签: java shell user-interface swt


    【解决方案1】:

    SWT.APPLICATION_MODAL样式打开第二个shell

    【讨论】:

      【解决方案2】:

      使用SWT.SYSTEM_MODALSWT.APPLICATION_MODAL 作为第二个shell 样式

      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.Shell;
      
      
      public class ShellTest {
      
          public static void main(String[] args) 
          {
              final Display display = new Display();
              final Shell shell = new Shell(display);
              shell.setLayout(new GridLayout());
      
              Button b = new Button(shell, SWT.PUSH);
              b.setText("Open Shell");
              b.addSelectionListener(new SelectionAdapter() {
                  public void widgetSelected(SelectionEvent e) {
                      openNewShell(shell);
                  }
              });
              shell.setSize(250, 150);
              shell.open();
              while (!shell.isDisposed()) {
                  if (!display.readAndDispatch())
                      display.sleep();
              }
              display.dispose();
          }
      
          protected static void openNewShell(final Shell shell) 
          {
              Shell child = new Shell(shell, SWT.TITLE|SWT.SYSTEM_MODAL| SWT.CLOSE | SWT.MAX);
              child.setSize(100, 100);
              child.setLayout(new GridLayout());
              child.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
      
              child.open();
          }
      }
      

      【讨论】:

      • 但现在我看不到顶部栏。看我上面添加的图片。
      • @hurtledown:查看更新后的答案。特别是子壳构造函数。另外,我建议您使用不同的 swt 样式位来感受它们。
      猜你喜欢
      • 2015-02-25
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多