JavaFX Canvas返回Null

时间:2018-05-21 02:07:47

标签: java javafx

对于有JavaFX经验的人,我在我的场景构建器中制作了一个画布,但是当我实际尝试访问所述画布时它是null,我不知道为什么我知道它在我的场景构建器中制作并显示在我的FXML文件 我已经将代码推送到github,如果有人想看看我是否做错了,可以在这里找到它: https://github.com/ProSavage/JavaFXCalculator/tree/master/src/application/grapher 随意提供任何其他建议!

以下是相关代码

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
 <?import javafx.scene.layout.HBox?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" 
minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"  
xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="application.grapher.GrapherController">
<children>
  <HBox alignment="CENTER" layoutX="4.0" layoutY="280.0" prefHeight="20.0" 
prefWidth="593.0">
     <children>
        <TextArea prefHeight="37.0" prefWidth="569.0" />
     </children>
  </HBox>
  <Button layoutX="238.0" layoutY="336.0" mnemonicParsing="false" 
  prefHeight="50.0" prefWidth="125.0" text="Graph" />
     <HBox fx:id="canvasBox" layoutX="14.0" layoutY="20.0" 
prefHeight="250.0" prefWidth="570.0" />
 </children>
   </AnchorPane>

班级

public class GrapherController {

@FXML
private HBox canvasBox;


public void test() {
    Canvas canvas = new Canvas(570,250);
    canvasBox.getChildren().add(canvas);
    double startX = canvas.getWidth()/2 * -1;
    double endX = startX * -1;
    double startY = canvas.getHeight()/2 * -1;
    double endY = startY * -1;

    GraphicsContext grapher = canvas.getGraphicsContext2D();
    grapher.beginPath();

    String equation = "x+1";
    grapher.moveTo(startX,Evaluator.eval(equation.replace("x",startX + "")));
    for (double i = startX; i < endX; i++) {
        grapher.lineTo(startX,Evaluator.eval(equation.replace("x",String.valueOf(i))));
    }






}



}

基本上在运行测试方法时,如果我创建一个画布并将其添加到框中,则画布为null,如果我尝试将其放入xml文件本身,则它仍为null。我觉得这是我自己在画布上遗漏的一些重要步骤。

1 个答案:

答案 0 :(得分:0)

实际上您的canvasBox字段为null。但问题在于你的GrapherScene课程。您正在手动创建GrapherController并在 实例上调用test()。在该情况下,canvasBox 不可能成为null

  • 你永远不会自己设定
  • 该实例未由FXMLLoader创建,这意味着不会发生FXML字段的依赖注入

更改:

public GrapherScene() {
    try {
        Stage grapherStage = new Stage();
        Parent root = FXMLLoader.load(getClass().getResource("grapher.fxml"));
        Scene scene = new Scene(root, 600, 400);
    //scene.getStylesheets().add(getClass().getResource("app.css").toExternalForm());
        grapherStage.setScene(scene);
        grapherStage.setTitle("Grapher");
        grapherStage.show();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    GrapherController grapher = new GrapherController();
    grapher.test();
}

要:

public GrapherScene() {
    try {

        Stage grapherStage = new Stage();
        FXMLLoader loader = new FXMLoader(getClass().getResource("grapher.fxml"));
        grapherStage.setScene(new Scene(loader.load());
        grapherStage.setTitle("Grapher");
        grapherStage.show();

        // Must be called AFTER loader.load()
        // Method has generic return type so explicit casting is not necessary in this case
        GrapherController grapher = loader.getController();
        grapher.test();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

注意:我在这里使用实例 FXMLLoader.load()方法,而不是静态 FXMLLoader.load(URL)方法

了解更多信息: