gpt4 book ai didi

java - 如何在不激活 shell 的情况下打开它?

转载 作者:行者123 更新时间:2023-11-30 09:02:06 26 4
gpt4 key购买 nike

我需要知道是否有任何方法可以打开 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. The APPLICATION_MODAL style allows an instance to block input to every other shell in the display. The SYSTEM_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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com