我试图了解控制器之间的连接是如何工作的。我写了一个基本的程序,但是,不要正确补充。我发现它是初始化函数 - 如果我同时评论
missionController.initialize(this);
loggerController.initialize(this);
错误消失
package controllers;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
public class MainController {
@FXML private MissionsTabController missionController;
@FXML private LoggerTabController loggerController;
public TextArea getLog()
{
return missionController.getTxtArea();
}
public void initialize()
{
System.out.println("System started");
missionController.initialize(this);
loggerController.initialize(this);
}
}
以下是来自其他控件的两个初始化函数:
LoggerTabController:
public void initialize(MainController mainController) {
this.mainController = mainController;
}
MissionsTabController:
public void initialize(MainController mainController) {
ListView<String> list = new ListView<String>();
ObservableList<String> items =FXCollections.observableArrayList (
"Apollo", "DDAY", "Tomorrow", "Yesterday");
list.setItems(items);
this.mainController=mainController;
}
你知道它为什么不起作用吗?(?
以下是发生错误的部分:
javafx.fxml.LoadException:
/E:/WorkSpace/NASA/bin/view/MainFXML.fxml
at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
以下是您询问的Main.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.MainController">
<children>
<TabPane layoutX="14.0" layoutY="27.0" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab text="NASA Missions">
<content>
<fx:include fx:id="missionsTab" source="MissionsTab.fxml" />
</content></Tab>
<Tab text="Logger">
<content>
<fx:include fx:id="loggerTab" source="LoggerTab.fxml" />
</content></Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>