我需要知道是否有任何方法可以打开 shell 而不使其处于 Activity 状态,即使我单击其中的控件也是如此。解释我需要什么的最好方法是向您展示这个小例子。我需要保持第一个 shell 处于 Activity 状态,即使我单击第二个 shell 或其包含的任何小部件。
public class TestMeOut
{
public static void main(final String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
final Shell shell2 = new Shell(shell);
shell2.setLayout(new GridLayout());
final Button btn = new Button(shell, SWT.PUSH);
final Button btn2 = new Button(shell2, SWT.PUSH);
btn.setText("Test me");
btn2.setText("I steal focus");
btn.addSelectionListener(new SelectionAdapter()
{
@Override
public void widgetSelected(final SelectionEvent e)
{
shell2.setVisible(true);
}
});
btn2.addSelectionListener(new SelectionAdapter()
{
@Override
public void widgetSelected(final SelectionEvent e)
{
shell2.setVisible(false);
}
});
shell.pack();
shell.open();
shell.addShellListener(new ShellListener()
{
public void shellIconified(final ShellEvent e)
{
}
public void shellDeiconified(final ShellEvent e)
{
}
public void shellDeactivated(final ShellEvent e)
{
System.out.println("Deactivated! This isn't supposed to happen.");
}
public void shellClosed(final ShellEvent e)
{
}
public void shellActivated(final ShellEvent e)
{
System.out.println("Activated!");
}
});
shell2.pack();
shell2.open();
shell2.setVisible(false);
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
提前谢谢您!
最佳答案
您可以更改第二个 Shell
的样式并使用 SWT.APPLICATION_MODAL
,这将确保您无法与父 shell 交互,除非您关闭子 shell:
final Shell shell2 = new Shell(shell, SWT.SHELL_TRIM | SWT.APPLICATION_MODAL);
The modality of an instance may be specified using style bits. The modality style bits are used to determine whether input is blocked for other shells on the display. The
PRIMARY_MODAL
style allows an instance to block input to its parent. TheAPPLICATION_MODAL
style allows an instance to block input to every other shell in the display. TheSYSTEM_MODAL
style allows an instance to block input to all shells, including shells belonging to different applications.
在这种情况下,甚至 SWT.PRIMARY_MODAL
就足够了。
更新
另一方面,如果您不希望父级在单击子级时失去焦点,只需禁用子级即可:
shell2.setEnabled(false);
关于java - 如何在不激活 shell 的情况下打开它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26120040/