簡體   English   中英

如何在JavaFX中將畫布添加到BorderPane?

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

在我的應用程序中,我有一個BorderPane用作我的GUI的布局。 在中央窗格中,我想添加一個可以在其上繪制形狀的畫布。 在我當前的代碼中,出現錯誤消息“找不到適用於add(Canvas)的合適方法。方法collection.add(Node)不適用。” 我究竟做錯了什么?

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