2016-04-30 9 views
0

私は、図、三角形、六角形、円、画像と図形を含む多くの異なる図形を持つBorderPaneを持っています。次に、最初に三角形をクリックしてから円をクリックするようにダイアログが表示されます。そのBorderPaneの最後の2つの要素を取得する最善の方法は何ですか?JavaFXで最後にクリックされた要素を表示する

私はこのように、それをクリックするか、いないかどうかを確認するために、単一のオブジェクトのためのMouseEventを使用することができます知っている:

hexagon.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() { 
     @Override 
     public void handle(MouseEvent mouseEvent) { 
      System.out.println("hexagon clicked"); 
     } 
}); 

答えて

0

あなたはBorderPaneのためのIDを設定する(rootを想定)との各要素ができます(のための仮定長方形rectangle1,rectangle2)。次に、要素が押されたかどうかを確認できます。サンプルコードは次のとおりです。

root.setOnMousePressed(new EventHandler<MouseEvent>() 
    { 
     @Override 
     public void handle(MouseEvent mouseEvent) 
     { 
      if (hexagon.isPressed()) 
       System.out.println("hexagon"); 
      else if (rectangle1.isPressed()) 
       System.out.println("rectangle1"); 
      else if (rectangle2.isPressed()) 
       System.out.println("rectangle2"); 
      else if (imageView1.isPressed()) 
       System.out.println("imageView1"); 
      else if (imageView2.isPressed()) 
       System.out.println("imageView2"); 
      else 
       System.out.println("Others"); 

     } 
    });