我一直在玩JavaFX最近,我只是想出如何使用MouseEvent
s,我想做一個程序,當你點擊,它抓住你的X
和Y
並創建一個圈在那個地方。從內部類引用的局部變量
我試試這個時出現錯誤local variables referenced from an inner class
。我打算使用拉姆達,但我無法弄清楚。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Circle;
public class circle extends Application{
Scene scene;
VBox v;
public void start(Stage stage){
double x = 0;
double y = 0;
v = new VBox(10);
scene = new Scene(v, 300, 300);
scene.setOnMousePressed(new EventHandler<MouseEvent>(){
public void handle(MouseEvent e)
{
x = e.getX();
y = e.getY();
}
});
Circle circle = new Circle();
circle.setCenterX(x);
circle.setCenterY(y);
v.getChildren().add(circle);
stage.setScene(scene);
stage.show();
}
}