我一直在玩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();
}
}