我想用 JavaFx 创建一些形状,并且我希望每个形状都在其中定义自己的 TranslateTransition 对象,并且我希望在单击形状时播放 TranslateTransition 。我的代码:
形状类:
package com;
import javafx.animation.TranslateTransition;
import javafx.beans.property.DoubleProperty;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.util.Duration;
public class MShape extends Parent{
private TranslateTransition Transition;
Circle circle;
DoubleProperty tox;
DoubleProperty toy;
public MShape(int i, int j) {
circle = new Circle(20);
circle.setCenterX(i);
circle.setCenterY(j);
circle.setFill(Color.RED);
Transition= new TranslateTransition();
Transition.setToX(350);
Transition.setToX(375);
Transition.setDuration(Duration.seconds(2));
Transition.setNode(this.circle);
this.getChildren().add(circle);
circle.setOnMouseClicked(new EventHandler<Event>() {
@Override
public void handle(Event paramT) {
Transition.play();
System.out.println("hello");
}
});
}
}
主班
package com;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Test extends Application{
@Override
public void start(Stage stage) throws Exception {
VBox root = new VBox();
stage.setScene(new Scene(root,800,600));
MShape test =new MShape(100, 100);
root.getChildren().add(test);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
当我按下形状时它不会移动的问题