Find composite location on screen

后端 未结 1 856
礼貌的吻别
礼貌的吻别 2021-02-06 06:47

I am implementing a on screen keyboard in Java for SWT and AWT. One important thing is to move the keyboard to a position where the selected text field can show and is not lying

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-06 07:36

    I believe the method Control.toDisplay() should be able to translate your coordinates into ones relative to the screen.

    This snippet may illustrate what you are after:

    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();
        }
    }
    

    0 讨论(0)
提交回复
热议问题