2017-12-11 2 views
2

。 マウスがシーンの内側にあるかデスクトップにあるかに関係なく、デスクトップ上にマウスのマウス座標が表示されます。私はこれをまとめることができた:JavaFXのショーマウスcoordsの

import javafx.geometry.Insets; 
import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.text.*; 
import javafx.stage.Stage; 
import javafx.animation.AnimationTimer; 
import java.awt.MouseInfo; 
import javafx.scene.layout.HBox; 

public class Apka extends Application { 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     // TODO Auto-generated method stub 

     new AnimationTimer() { 
      public void handle(long currentNanoTime) { 

       Text textX = new Text(MouseInfo.getPointerInfo().getLocation().getX() + ""); 
       Text textY = new Text(MouseInfo.getPointerInfo().getLocation().getY() + ""); 
       textX.setFont(new Font(20)); 
       textY.setFont(new Font(20)); 

       HBox root = new HBox(5); 
       root.setPadding(new Insets(1)); 
       root.getChildren().add(textX); 
       root.getChildren().add(textY); 

       Scene scene = new Scene(root, 300, 30); 

       primaryStage.setTitle("Mouse Coordinates"); 
       primaryStage.setScene(scene); 
      } 
     }.start(); 

     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

それは働く。
明白な問題は、Javaプログラムが常に上にあることです。Alt-Tabウィンドウのショートカットを使用してプログラムを切り替えることはできません。テキストオブジェクトと特にマウスコードの値を更新するだけです。このようにすることができますか、それとも私はそれをすべて取り除いて元に戻るべきですか?

"最適化" の後に解決
+0

の「外」に置かれたのですか?何が起きているのか、起こったのはもう少し説明できますか? – n247s

+0

Windows Alt-Tabを使用してプログラムを切り替えることはできません。Javaプログラムウィンドウは常に上に表示されます。私はフレームごとにすべてを再描画するので、そうだと思います。テキスト文字列を再描画したいだけです –

+0

なぜ新しいテキストフィールド/シーンを毎回作成する必要があるのですか?既存のテキストフィールドを更新するのはなぜですか? – MadProgrammer

答えて

0

:私はちょうどテキストはのsetText()メソッドでフィールドオブジェクト更新に必要な

new AnimationTimer() { 
     public void handle(long currentNanoTime) { 
      textX.setText(MouseInfo.getPointerInfo().getLocation().getX() + ""); 
      textY.setText(MouseInfo.getPointerInfo().getLocation().getY() + ""); 

     } 
    }.start(); 

    primaryStage.show(); 

。他のすべては、あなたが、「Alt + Tabキーの描画」を意味するかどうAnimationTimer