简体   繁体   English

如何在JavaFX中将画布添加到BorderPane?

[英]How can I add a canvas to a BorderPane in JavaFX?

In my application, I have a BorderPane which serves as the layout for my GUI. 在我的应用程序中,我有一个BorderPane用作我的GUI的布局。 in the center pane, I want to add a canvas on which I can draw shapes. 在中央窗格中,我想添加一个可以在其上绘制形状的画布。 In my current code, I get the error saying "No suitable method found for add(Canvas). Method collection.add(Node) is not applicable." 在我当前的代码中,出现错误消息“找不到适用于add(Canvas)的合适方法。方法collection.add(Node)不适用。” What am I doing wrong? 我究竟做错了什么?

package MyGame;

import java.awt.Canvas;
import javafx.scene.paint.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
import javafx.stage.Stage;

public class MyGame extends Application {

    public Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    public double windowWidth = screenSize.getWidth() * .75;
    public double windowHeight = screenSize.getHeight() * .75;

    @Override
    public void start(Stage primaryStage) {

        BorderPane root = new BorderPane();

        Button regenerate = new Button("Regenerate Board");
        regenerate.setPrefWidth(windowWidth * .15 * .80);
        regenerate.getStyleClass().add("spButton");

        Button settings = new Button("Game Settings");
        settings.setPrefWidth(windowWidth * .15 * .80);
        settings.getStyleClass().add("spButton");

        Button start = new Button("Start Game");
        start.setPrefWidth(windowWidth * .15 * .80);
        start.getStyleClass().add("spButton");

        VBox bpLeft = new VBox(20);
        bpLeft.setPrefWidth(windowWidth * .15);
        //bpLeft.resize(((screenSize.getWidth() * .75) *.20), screenSize.getHeight() * .75);
        bpLeft.getStyleClass().add("bpLeft");
        bpLeft.getChildren().addAll(regenerate, settings, start);

        root.setLeft(bpLeft);

        double originX = (windowWidth * .15) + ((windowWidth * .85) / 2);
        double originY = (windowHeight / 2);

        Pane wrapperPane = new Pane();
        root.setCenter(wrapperPane);
        // Put canvas in the center of the window
        Canvas canvas = new Canvas();
        canvas.setBounds((int)originX, (int)originY, (int)Math.round(windowWidth * .85), (int)Math.round(windowHeight));
        wrapperPane.getChildren().add(canvas);

        //root.getChildren().add(btn);
        Scene scene = new Scene(root, windowWidth, windowHeight);

        scene.getStylesheets().add(HelloWorld.class.getResource("Catan.css").toExternalForm());
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
        }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

您不小心导入了java.awt.Canvas而不是javaFX canvas。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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