2017-05-19 40 views
0

我开始学习JavaFX。我制作了一个创建形状的简单程序。坐标位置的光标和颜色变化与一定条件 - JavaFX

我想要做驱动的执行方法基本事件,比如:

  1. 打印如果形状
  2. 内其改变颜色每次光标进入的形状,然后改变它的光标的坐标回到原来的一旦它出了形状

以下是我所做

import javafx.application.Application; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.scene.Group; 
import javafx.scene.shape.Rectangle; 
import javafx.scene.shape.Polygon; 


public class ColouredShapes extends Application { 

    @Override 
    public void start(Stage stage) { 
     int radius = 40; 
     int strokeWidth = 3; 
     Color strokeColor = Color.BROWN; 

     Circle circle1 = new Circle(250, 125, radius, Color.RED); 
     circle1.setStroke(strokeColor); 
     circle1.setStrokeWidth(strokeWidth); 

     Rectangle square = new Rectangle(375, 85, 80, 80); 
     square.setFill(Color.BLUE); 
     square.setStroke(strokeColor); 
     square.setStrokeWidth(strokeWidth); 

     Polygon polygon = new Polygon(); 
     polygon.getPoints().addAll(new Double[]{ 
       50.0, 85.0, 
       110.0, 60.0, 125.0, 175.0}); 

     polygon.setFill(Color.YELLOWGREEN); 

     Group root = new Group(); 
     root.getChildren().addAll(circle1, square, polygon); 
     Scene scene = new Scene(root, 500, 250, Color.LIGHTYELLOW); 
     stage.setScene(scene); 
     stage.show(); 

    } 
    public static void maint(String[] args){ 
     launch(args); 
    } 
} 
+0

当你创建你的形状,创造的OnEnter和的OnExit处理。 – Sedrick

+0

http://stackoverflow.com/questions/13359382/creating-a-mouselistner-to-javafx-rectangle。而不是onMouseClicked使用onMouseEntered和另一个OnMouseExited。 – Sedrick

回答

1

添加事件鼠标进入,退出和移动处理器的形状

square.setOnMouseMoved(new EventHandler<MouseEvent>() { 

    @Override 
    public void handle(final MouseEvent event) { 

     System.out.println(event.getScreenX()); 
     System.out.println(event.getScreenY()); 
    } 
}); 
square.setOnMouseEntered(new EventHandler<MouseEvent>() { 

    @Override 
    public void handle(final MouseEvent event) { 
    square.setFill(Color.GREEN); 
    } 
}); 
square.setOnMouseExited(new EventHandler<MouseEvent>() { 

    @Override 
    public void handle(final MouseEvent event) { 
    square.setFill(null); 

    } 
}); 
+0

嗨,你能帮我这个请https://stackoverflow.com/questions/44153922/trying-to-add-menubar-as-node-to-a-scene-javafx – Kutam