2016-09-21 44 views
-2

我有片段類。如何通過鼠標點擊事件來打破以下循環?如何打破鼠標點擊事件的循環

public class Sample { 

    private static Label label; 

    public static void main(String[] args) {   
     Display display = new Display(); 
     Shell shell = new Shell(display); 
     shell.setLayout(new FillLayout()); 
     shell.setBounds(100, 100, 200, 70); 
     Button button = new Button(shell, SWT.NONE); 
     button.setText("Go"); 
     button.addListener(SWT.Selection, new Listener(){ 
      @Override 
      public void handleEvent(Event arg0) { 
       while (true) { 
        int x = MouseInfo.getPointerInfo().getLocation().x; 
        int y = MouseInfo.getPointerInfo().getLocation().y; 
        label.setText(x + ":" + y); 
       } 
      }}); 
     label = new Label(shell, SWT.NONE); 
     shell.open(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) display.sleep(); 
     } 
     display.dispose(); 
    } 
} 
+1

它是什麼,你試圖實現在第一個地方? – Baz

+0

我想單擊位於外部應用程序上的指定文本框,然後循環被截斷。我想記住點擊文本框的位置(x,y)。 – ks099

+0

如何知道何時點擊了外部應用程序中的文本框? – Baz

回答

0

我有我想要的。 謝謝Baz的幫助。

public class Sample { 

private static Label label; 

public static void main(String[] args) {   
    Display display = new Display(); 
    Shell shell = new Shell(display); 
    shell.setLayout(new FillLayout()); 
    shell.setBounds(100, 100, 200, 70); 
    Button button = new Button(shell, SWT.NONE); 
    button.setText("Go"); 
    button.addListener(SWT.Selection, new Listener(){ 
     @Override 
     public void handleEvent(Event arg0) { 
      Shell screenShell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP); 
      screenShell.setMaximized(true); 
      screenShell.setBackground(display.getSystemColor(SWT.COLOR_GRAY)); 
      screenShell.setAlpha(120); 
      screenShell.addListener(SWT.MouseDown, new Listener() { 
       @Override 
       public void handleEvent(Event event) { 
        screenShell.close();       
       }}); 
      screenShell.open(); 
      Display.getCurrent().timerExec(10, new Runnable(){ 
       @Override 
       public void run() { 
        if (!screenShell.isDisposed()) { 
         int x = MouseInfo.getPointerInfo().getLocation().x; 
         int y = MouseInfo.getPointerInfo().getLocation().y; 
         label.setText(x + ":" + y); 
         Display.getCurrent().timerExec(10, this); 
        } 
       }});  
     }}); 

    label = new Label(shell, SWT.NONE); 
    shell.open(); 

    while (!shell.isDisposed()) { 
     if (!display.readAndDispatch()) display.sleep(); 
    } 
    display.dispose(); 
}