如何制作场景本地JavaFX

How to make scene local JavaFX

lambda开头的场景出现非本地错误。如何使其成为本地。我尝试添加顶部但显示为空。由于场景设置了 primaryStage,因此如何在 lambda 处使场景成为本地场景。我需要 GridPane 的其他节点。我正在尝试利用 canvas。请帮助解决这个问题。先谢谢你了!

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

}

您在这部分代码中定义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 之后的位置。

Full Code:

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

}