【发布时间】:2020-07-21 17:34:11
【问题描述】:
以下小示例在 Windows 和 Ubuntu 上的行为不同:
-
mainShell是一个Shell,它包含一个Text输入和一个Button - 按下按钮时,另一个外壳 @987654325@ 是
opened(具有特定样式) - shell打开后,焦点应该回到文本输入
在 Windows 上,焦点确实回到了文本输入。然而,在 Ubuntu 上,smallShell 仍然是焦点,即使我们调用 setActive 或 setFocus。我希望它表现得像第一种情况并产生焦点。
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class ShellWithoutTitleBar {
public static void main(String[] args) {
final Display display = new Display();
final Shell mainShell = new Shell(display, SWT.SHELL_TRIM);
mainShell.setSize(300, 200);
mainShell.setLayout(new GridLayout());
final Shell smallShell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
smallShell.setSize(150, 150);
Text text = new Text(mainShell, SWT.BORDER);
Button button = new Button(mainShell, SWT.PUSH);
button.setText("foo");
mainShell.open();
button.addMouseListener(new MouseListener() {
@Override
public void mouseDoubleClick(MouseEvent e) { }
@Override
public void mouseDown(MouseEvent e) {
smallShell.open();
mainShell.setActive();
text.setFocus();
}
@Override
public void mouseUp(MouseEvent e) { }
});
// Set up the event loop.
while (!smallShell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
}
当我省略 SWT.ON_TOP 标志时,一切正常,但是对于我的用例来说,新的 shell 始终处于顶层是至关重要的。
我尝试了随机方式将焦点强制返回到文本输入或至少主 shell,但似乎没有任何效果。
有人知道解决方法吗?提前非常感谢。
【问题讨论】:
标签: java eclipse-plugin gtk swt