Java 在屏幕上查找复合位置
我正在用Java为SWT和AWT实现一个屏幕键盘。 一件重要的事情是将键盘移动到所选文本字段可以显示的位置,而不是位于屏幕键盘后面 对于AWT,我可以使用Java 在屏幕上查找复合位置,java,swing,plugins,swt,awt,Java,Swing,Plugins,Swt,Awt,我正在用Java为SWT和AWT实现一个屏幕键盘。 一件重要的事情是将键盘移动到所选文本字段可以显示的位置,而不是位于屏幕键盘后面 对于AWT,我可以使用 Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); if (owner == null) { return; } Point ownerLocation = owner.getLocationOnScreen(
Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
if (owner == null) {
return;
}
Point ownerLocation = owner.getLocationOnScreen();
Dimension ownerSize = owner.getSize();
如何在SWT中实现相同的逻辑?
我通过向SWT事件队列添加focuslistener来获取当前选定的小部件。但是当我打电话的时候
Point location = new Point(mTextWidget.getLocation().x, mTextWidget.getLocation().y);
Dimension dimension = new Dimension(mTextWidget.getSize().x, mTextWidget.getSize().y);
我将得到相对于父组合的位置
我怎样才能获得一个特殊小部件相对于整个屏幕的位置?我相信该方法应该能够将您的坐标转换为相对于屏幕的坐标
这可以说明您的目标:
package org.eclipse.jface.snippets;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
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 Bla {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
final Text t = new Text(shell,SWT.BORDER);
t.setBounds(new Rectangle(10,10,200,30));
System.err.println(t.toDisplay(1, 1));
Button b = new Button(shell,SWT.PUSH);
b.setText("Show size");
b.setBounds(new Rectangle(220,10,100,20));
b.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.err.println(t.toDisplay(1, 1));
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
谢谢你,伙计!你的回答结束了一个上午的痛苦。效果很好,谢谢你提起今天的节目()!它还将接受一个点,这在转换组件位置时非常方便。我在拖放过程中使用它来转换事件坐标:composite.toDisplay(composite.getLocation())@Gregor很高兴知道这个答案仍然很有用。。。五年多以后。@VonC:omg,我不知道;)不幸的是,SWT/RCP仍然通过RAP继续存在。我渴望JavaFX成为桌面/web客户端的交叉框架,但是…言语无法表达我现在有多爱你。