【问题标题】:JavaFX Canvas Returning NullJavaFX 画布返回 Null
【发布时间】:2018-05-21 02:07:47
【问题描述】:

对于任何有 JavaFX 经验的人,我已经在我的场景构建器中制作了一个画布,但是当我实际尝试访问所述画布时,它是空的,我不确定为什么我知道它是在我的场景构建器中制作并显示在我的 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))));
    }






}



}

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

【问题讨论】:

  • Canvas canvas = new Canvas(570,250); 除非此语句引发异常,否则 canvas 之后不能是 null。但是,如果您抱怨 Canvas 是空的……您需要在 GraphicsContext 上调用 stroke 才能实际绘制一些东西。此外,始终使用startX 作为x 坐标可能不是正确的做法......

标签: java javafx


【解决方案1】:

实际上是canvasBox 字段是null。但问题在于您的GrapherScene 课程。您正在手动创建 GrapherController 并在 that 实例上调用 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();
    }
}

注意:我在这里使用的是 instance FXMLLoader.load() 方法,而不是 static FXMLLoader.load(URL) 方法


更多信息:

【讨论】:

  • 谢谢,昨晚我推送了一个更新到 github,它按照你所说的进行了修复。
猜你喜欢
  • 2011-10-11
  • 2017-01-25
  • 1970-01-01
  • 1970-01-01
  • 2019-01-25
  • 2018-06-05
  • 1970-01-01
  • 1970-01-01
  • 2022-11-08
相关资源
最近更新 更多