javaFX无法访问Scene元素

时间:2015-07-20 13:21:30

标签: javafx

我是javafX的新手,经历过各种教程,并广泛搜索。我正在尝试编写一个多屏幕javaFX程序,其第二个屏幕有一个拆分窗格,其右窗格应显示一个网格和一个按钮。我用于多个屏幕的框架是从Angela Caiedo的MultipleScreens框架教程(https://github.com/acaicedo/JFX-MultiScreen/tree/master/ScreensFramework)中复制的。

什么有效:屏幕框架在我的多个屏幕之间翻转方面是成功的。

我的问题:我点击屏幕1上的按钮移动到屏幕2.屏幕2成功显示。在屏幕2上,我单击一个按钮来填充我的vBox中的数据。屏幕2的控制器类中的代码无法访问vbox(或此屏幕上任何其他已定义的容器)。

我的主要课程设置控制器屏幕。

public class FieldMapCreator extends Application {
    public static String mainID = "main";
    public static String mainFile = "MainMapFXMLDocument.fxml";
    public static String initialMapID = "initialMap";
    public static String initialMapFile = "FXMLMapPicture.fxml";
  @Override
  public void start(Stage stage) throws Exception {

    ScreensController mainContainer = new ScreensController();
    mainContainer.loadScreen(FieldMapCreator.mainID, FieldMapCreator.mainFile);
    mainContainer.loadScreen(FieldMapCreator.initialMapID, FieldMapCreator.initialMapFile);

    mainContainer.setScreen(FieldMapCreator.mainID);
    Group root = new Group();
    root.getChildren().addAll(mainContainer);
    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
  }
}

我点击屏幕上的按钮进入第二个屏幕1. Button1代码如下:

@FXML
private void initialMapButtonAction(ActionEvent event) {
    myController.setScreen(FieldMapCreator.initialMapID);
}

屏幕2有一个按钮,单击该按钮时会执行为vbox创建数据的方法。屏幕2的fxml是:

<AnchorPane id="AnchorPane" fx:id="mainAnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="FieldMapCreator.FXMLMapPictureController">
   <children>
      <SplitPane fx:id="mapSplitPane" dividerPositions="0.29797979797979796" layoutX="3.0" layoutY="7.0" prefHeight="400.0" prefWidth="600.0">
        <items>
          <AnchorPane fx:id="anchorPaneLeft" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
               <children>
                  <Button fx:id="backButton" layoutX="14.0" layoutY="348.0" mnemonicParsing="false" onAction="#goToMainScreen" text="Back" />
               </children>
            </AnchorPane>
          <AnchorPane fx:id="anchorPaneRight" minHeight="0.0" minWidth="0.0" prefHeight="364.0" prefWidth="401.0">
               <children>
                  <Button fx:id="vBoxShowMap" layoutX="24.0" layoutY="346.0" mnemonicParsing="false" onAction="#buildMapFromNurseries" prefHeight="26.0" prefWidth="91.0" text="show map" />
                  <VBox fx:id="mapVBox" layoutX="24.0" layoutY="24.0" onMouseClicked="#vBoxMouseClicked" prefHeight="200.0" prefWidth="309.0" />
               </children></AnchorPane>
        </items>
      </SplitPane>
   </children>
</AnchorPane>

失败的代码位于屏幕2的控制器类的“showInitialMap()”方法中。执行时,此方法的最后一行“mapVBox.getChildren()。add(root)”,结果为null指针异常。我希望通过fxml注入解决这个问题。我还尝试了注释掉的代码,使用场景查找来检索“mapVBox”,但它也会导致空指针异常。控制器代码如下:

public class FXMLMapPictureController  implements Initializable, ControlledScreen  {

  ScreensController myController;
  static MyNode[][] mainField ;

  @FXML
  private AnchorPane mainAnchorPane;
  @FXML
  private static SplitPane mapSplitPane;
  @FXML
  private AnchorPane anchorPaneLeft;
  @FXML
  private static AnchorPane anchorPaneRight;
  @FXML
  private static VBox mapVBox;
  @FXML
  private Button vBoxShowMap;
  /**
   * Initializes the controller class.
   */
  @Override
  public void initialize(URL url, ResourceBundle rb) {
     // Should something be in here ??
  }

  public void goToMainScreen() {
    myController.setScreen(FieldMapCreator.mainID);
  }

  public void showInitialMap() {
    int row = userFieldMap.getNumRows();
    int col = userFieldMap.getNumCols();

    double gridWidth = 309/col;
    double gridHeight = 200/row;
    Group root = new Group();
    // initialize the field
    for (int idx = 0; idx < col; idx++) {
        for (int jdx = 0; jdx < row; jdx++) {
            // create plot
            Plot plot = new Plot(idx, jdx, "Plot" + idx + "/" + jdx);
            // Create node to hold the Plot
            MyNode node = new MyNode(idx*gridWidth, jdx*gridHeight, gridWidth, gridHeight, plot);
            // add plot to group
            root.getChildren().add(node);
        }
    }
    //Scene myScene = myController.getScene();
    //VBox mapVBox = (VBox)myScene.lookup("#mapVBox");
    mapVBox.getChildren().add(root);
  }

  @Override
  public void setScreenParent(ScreensController screenParent) {
    myController = screenParent;
  }

initialize()方法中是否缺少某些内容?当我换屏幕时,我想知道我是不是正确设置了一些东西。我可以从所有屏幕1方法(包括初始化类)中无问题地访问初始屏幕的容器。当我在屏幕2中尝试相同时,我收到消息“屏幕尚未加载!!!”

感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:1)

您正在使用static字段作为FXML注入目标,这些字段在Java 8中不起作用,并且不应该在任何其他版本中实现,因为静态UI元素不会使感。这里有一些更详细answer。大部分时间都使用static字段意味着需要重新考虑应用程序的软件设计。

答案 1 :(得分:0)

只需安装Oracle JDK 8。 ubuntu installation

相关问题