2017-01-29 34 views
0

在我的应用程序中,我有一个BorderPane作为我的GUI的布局。在中间窗格中,我想添加一个可以绘制形状的画布。在我目前的代码中,我得到错误说“没有找到合适的方法添加(Canvas)。方法collection.add(节点)不适用。”我究竟做错了什么?如何在JavaFX中将画布添加到BorderPane?

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

} 

回答

0

您意外地导入了java.awt.Canvas而不是javaFX画布。

+0

呃......傻我。谢谢! – ShoeLace1291