我正在用 Java 为 SWT 和 AWT 实现屏幕键盘。一件重要的事情是将键盘移动到所选文本字段可以显示并且不位于屏幕键盘后面的位置。
对于 AWT,我可以通过以下方式检测当前选定组件的位置
Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
if (owner == null) {
return;
}
Point ownerLocation = owner.getLocationOnScreen();
Dimension ownerSize = owner.getSize();
如何在 SWT 中实现相同的逻辑?我通过向 SWT 事件队列添加焦点侦听器来获取当前选定的小部件。但是当我打电话时
Point location = new Point(mTextWidget.getLocation().x, mTextWidget.getLocation().y);
Dimension dimension = new Dimension(mTextWidget.getSize().x, mTextWidget.getSize().y);
我将获得相对于父复合材料的位置。
我怎样才能得到一个特殊的小部件相对于整个屏幕的位置?