javafx画布不会在vbox中显示

krcsximq  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(245)

我正在尝试用javafx编程一个模拟游戏,目前正在尝试让ui正常工作,但是出于某种原因我的 Canvas 不管我用它做什么,它总是保持空白 GraphicsContext .
有人知道我做错了什么吗?

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Test extends Application {
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Conway's Game Of Life");

        // Game Scene
        VBox main = new VBox();
        Scene gameScene = new Scene(main, 800, 600);
        HBox header = new HBox();
        Label generationLabel = new Label("Generation: 0");
        Canvas gameCanvas = new Canvas(200, 200);
        HBox controls = new HBox();
        Button stepBtn = new Button("Step");

        // Put something on the canvas
        gameCanvas.getGraphicsContext2D().setFill(Color.BLACK);
        gameCanvas.getGraphicsContext2D().rect(0, 0, 100, 100);

        header.getChildren().addAll(generationLabel);
        controls.getChildren().add(stepBtn);

        // Put UI elements (including canvas) on the main scene
        main.getChildren().addAll(header, gameCanvas, controls);

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

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

使用JDK1.8。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题