首页 > 解决方案 > How to make scene local JavaFX

问题描述

The scene at the start of lambda has an error that is not local. How to make it local. I tried adding up top but shows null. How to make scene local at the lambda because scene sets primaryStage. I need the other nodes for GridPane. I am trying to draw on the canvas. Please help fix this problem. Thank you in advanced!

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class TestFX extends Application {

    Node scene; // i tried adding this but throws null at the lambda

    @Override
    public void start(Stage primaryStage) throws Exception {

        primaryStage.setTitle("MyPaint");

        RadioButton radioButton1 = new RadioButton("Draw");
        RadioButton radioButton2 = new RadioButton("Erase");
        RadioButton radioButton3 = new RadioButton("Circle");

        ToggleGroup radioGroup = new ToggleGroup();

        radioButton1.setToggleGroup(radioGroup);
        radioButton2.setToggleGroup(radioGroup);
        radioButton3.setToggleGroup(radioGroup);

        Button b1 = new Button("Clear Canvas");

        TextField colorText = new TextField();
        colorText.setText("Colors");

        Canvas canvas = new Canvas(300, 250);
        GraphicsContext gc = canvas.getGraphicsContext2D();

           // scene cannot be resolved without initializing at top: Node scene;
            scene.setOnMousePressed(q -> {
                gc.setFill(Color.BLACK);
                gc.setLineWidth(1); 
                gc.beginPath();
                gc.lineTo(q.getSceneX(),
                q.getSceneY());
                gc.getStroke();
                });

        gc.strokeText("Hello Canvas", 150, 100);

        GridPane gridPane = new GridPane();

        GridPane.setConstraints(radioButton1, 0, 0);
        GridPane.setConstraints(radioButton2, 0, 1);
        GridPane.setConstraints(radioButton3, 0, 2);
        GridPane.setConstraints(colorText, 0, 3);
        GridPane.setConstraints(b1, 0, 4);
        GridPane.setConstraints(canvas, 1, 1);

        // adding spaces between radiobutton, button and radiobutton
        radioButton1.setPadding(new Insets(15));
        radioButton2.setPadding(new Insets(15));
        radioButton3.setPadding(new Insets(15));
        b1.setPadding(new Insets(15));
        colorText.setPadding(new Insets(15));

        gridPane.getChildren().addAll(radioButton1, radioButton2, radioButton3, colorText, b1, canvas);
        Scene scene = new Scene(gridPane, 800, 800);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

标签: javafxlocal-variablesscene

解决方案


Scene在这部分代码中定义:

gridPane.getChildren().addAll(radioButton1, radioButton2, radioButton3, colorText, b1, canvas);
Scene scene = new Scene(gridPane, 800, 800);
primaryStage.setScene(scene);
primaryStage.show()

但是您在定义之前调用此代码Scene

// scene cannot be resolved without initializing at top: Node scene;
scene.setOnMousePressed(q -> {
   gc.setFill(Color.BLACK);
   gc.setLineWidth(1); 
   gc.beginPath();
   gc.lineTo(q.getSceneX(),
   q.getSceneY());
   gc.getStroke();
});

您需要删除Node scene. 并将scene.setOnMousePressed()代码移动到您定义Scene.

完整代码:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class TestFX extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        primaryStage.setTitle("MyPaint");

        RadioButton radioButton1 = new RadioButton("Draw");
        RadioButton radioButton2 = new RadioButton("Erase");
        RadioButton radioButton3 = new RadioButton("Circle");

        ToggleGroup radioGroup = new ToggleGroup();

        radioButton1.setToggleGroup(radioGroup);
        radioButton2.setToggleGroup(radioGroup);
        radioButton3.setToggleGroup(radioGroup);

        Button b1 = new Button("Clear Canvas");

        TextField colorText = new TextField();
        colorText.setText("Colors");

        Canvas canvas = new Canvas(300, 250);
        GraphicsContext gc = canvas.getGraphicsContext2D();

        gc.strokeText("Hello Canvas", 150, 100);

        GridPane gridPane = new GridPane();

        GridPane.setConstraints(radioButton1, 0, 0);
        GridPane.setConstraints(radioButton2, 0, 1);
        GridPane.setConstraints(radioButton3, 0, 2);
        GridPane.setConstraints(colorText, 0, 3);
        GridPane.setConstraints(b1, 0, 4);
        GridPane.setConstraints(canvas, 1, 1);

        // adding spaces between radiobutton, button and radiobutton
        radioButton1.setPadding(new Insets(15));
        radioButton2.setPadding(new Insets(15));
        radioButton3.setPadding(new Insets(15));
        b1.setPadding(new Insets(15));
        colorText.setPadding(new Insets(15));

        gridPane.getChildren().addAll(radioButton1, radioButton2, radioButton3, colorText, b1, canvas);
        Scene scene = new Scene(gridPane, 800, 800);
        scene.setOnMousePressed(q -> {
                gc.setFill(Color.BLACK);
                gc.setLineWidth(1); 
                gc.beginPath();
                gc.lineTo(q.getSceneX(),
                q.getSceneY());
                gc.getStroke();
                });

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

推荐阅读