简体   繁体   中英

local variables referenced from an inner class

I've been playing around with JavaFX lately and I just figured out how to use MouseEvent s, I wanted to make a program where when you click, it grabs your X and Y and creates a circle in that place.

I get the error local variables referenced from an inner class when I try this. I was going to use a lambda, but I couldn't figure out how.

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();
    }
}

Even if your code compiled, it wouldn't update the position of the circle. You set the centerX and centerY of the circle just once, at a time when x=0 and y=0 . If you were allowed to access (non-final, non-effectively-final) local variables in the inner class, all you would be doing would be to update x and y , not the circle's centerX and centerY properties.

You just need

public void start(Stage stage){
    v = new VBox(10);
    scene = new Scene(v, 300, 300);
    Circle circle = new Circle();
    v.getChildren().add(circle);
    scene.setOnMousePressed(new EventHandler<MouseEvent>(){
        public void handle(MouseEvent e)
        {
            circle.setCenterX(e.getX());
            circle.setCenterY(e.getY());
        }
    });
    stage.setScene(scene);
    stage.show();
}

The lambda expression version looks like

public void start(Stage stage){
    v = new VBox(10);
    scene = new Scene(v, 300, 300);
    Circle circle = new Circle();
    v.getChildren().add(circle);
    scene.setOnMousePressed(e -> {
        circle.setCenterX(e.getX());
        circle.setCenterY(e.getY());
    });
    stage.setScene(scene);
    stage.show();
}

In versions of Java prior to Java8, you would need to declare circle as final, which you can readily do:

final Circle circle = new Circle();

The error message is telling you what the problem is: your local variables, x and y , are being referenced in an inner class -- your EventHandler . You need to declare x and y as members of your Circle class (which should be capitalized by convention, btw), not as local variables in the start function. Then you will be able to set them in your EventHandler .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM