我是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中嘗試相同時,我收到消息「屏幕尚未加載!!!」
感謝您提供的任何幫助。
謝謝!我不記得爲什麼我在字段中添加了「靜態」(沒有經驗的新用戶!),但刪除它解決了問題。屏幕沒有正確加載,這就是爲什麼我無法訪問容器。它現在加載。 – lynnj