2015-07-20 67 views
0

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

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

我的问题:我点击屏幕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> 

失败的代码是在“showInitialMap()”为屏幕2,控制器类的方法执行时,这个方法“mapVBox.getChildren的最后一行( ).add(root)“,导致空指针异常。我期望通过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中尝试相同时,我收到消息“屏幕尚未加载!!!”

感谢您提供的任何帮助。

回答

0

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

+0

谢谢!我不记得为什么我在字段中添加了“静态”(没有经验的新用户!),但删除它解决了问题。屏幕没有正确加载,这就是为什么我无法访问容器。它现在加载。 – lynnj