我对javaFX项目有疑问。
我有一个fxml,其中包括另一个:
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1200.0" prefWidth="1600.0" stylesheets="@../css/mainWindow.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.spaceopera.client.ihm.mainwindow.MainWindowController">
<children>
<BorderPane fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1200.0" prefWidth="1600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<top>
<ListView fx:id="gameMenu" onMouseClicked="#handleGameMenuClick" orientation="HORIZONTAL" prefHeight="100.0" prefWidth="1600.0" style="-fx-background-color: orange;" BorderPane.alignment="CENTER" />
</top>
<left>
<ListView fx:id="planetMenu" onMouseClicked="#handlePlanetMenuClick" minHeight="1100.0" minWidth="200.0" maxHeight="1100.0" maxWidth="200.0" prefHeight="1100.0" prefWidth="200.0" style="-fx-background-color: blue;" />
</left>
<right>
<ListView fx:id="empireMenu" onMouseClicked="#handleEmpireMenuClick" minHeight="1100.0" minWidth="200.0" maxHeight="1100.0" maxWidth="200.0" prefHeight="1100.0" prefWidth="200.0" style="-fx-background-color: red;" BorderPane.alignment="CENTER" />
</right>
<center>
<fx:include fx:id="mainElement" source="MainElementView.fxml"/>
</center>
</BorderPane>
</children>
</AnchorPane>
在控制器中,我使用
@FXML 私有MainElementViewController mainElement; 要访问包含的元素
这是MainElementView.fxml:
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="1200.0" stylesheets="@../css/specificElement.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.spaceopera.client.ihm.mainwindow.mainelement.MainElementViewController">
<children>
</children>
</AnchorPane>
MainElementViewController.java的颂歌:
package com.spaceopera.client.ihm.mainwindow.mainelement;
import com.spaceopera.client.ihm.mainwindow.mainelement.specificelement.SpecificElementViewController;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;
/**
*
* @author olivier
*/
public class MainElementViewController extends AnchorPane implements Initializable {
@FXML
SpecificElementViewController specificElement;
private List<String> elementTypesList;
public MainElementViewController() {
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
public List<String> getElementTypesList() {
return elementTypesList;
}
public void setElementTypesList(List<String> elementTypesList) {
System.out.println("plop");
this.elementTypesList = elementTypesList;
specificElement.setElementTypes(elementTypesList);
}
}
这是SpecificElementView.xml:
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.ListView?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="1200.0" stylesheets="@../css/specificElement.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.spaceopera.client.ihm.mainwindow.mainelement.specificelement.SpecificElementViewController">
<children>
<ListView fx:id="elementTypes" onMouseClicked="#handleSpecificElementClick" orientation="HORIZONTAL" prefHeight="100.0" prefWidth="1200.0" style="-fx-background-color: purple;" />
</children>
</AnchorPane>
还有java:
package com.spaceopera.client.ihm.mainwindow.mainelement.specificelement;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
/**
*
* @author olivier
*/
public class SpecificElementViewController extends AnchorPane implements Initializable {
private List<String> elementTypesList;
@FXML
private ListView elementTypes;
public SpecificElementViewController() {
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
@FXML
public void handleSpecificElementClick(MouseEvent arg) throws Exception {
throw new Exception("Not implemented yet!!!");
}
public List<String> getElementTypes() {
return elementTypesList;
}
public void setElementTypes(List<String> elementTypesList) {
this.elementTypes = elementTypes;
elementTypes.getItems().clear();
for (String s : elementTypesList) {
System.out.println("type : " + s);
elementTypes.getItems().add(s);
}
}
}
运行它时,我有一个例外:
avafx.fxml.LoadException: /home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainElementView.fxml:15 /home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainWindow.fxml:27
在javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) 在javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)在 javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)在 javafx.fxml.FXMLLoader.access $ 2700(FXMLLoader.java:103)在 javafx.fxml.FXMLLoader $ IncludeElement.constructValue(FXMLLoader.java:1143) 在 javafx.fxml.FXMLLoader $ ValueElement.processStartElement(FXMLLoader.java:746) 在javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707) 在javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)在 javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)在 javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)在 com.spaceopera.client.Client.startGame(Client.java:88)位于 com.spaceopera.client.Client.start(Client.java:62)位于 com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 161(LauncherImpl.java:863) 在 com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 174(PlatformImpl.java:326) 在 com.sun.javafx.application.PlatformImpl.lambda $ null $ 172(PlatformImpl.java:295) 在java.security.AccessController.doPrivileged(本机方法)在 com.sun.javafx.application.PlatformImpl.lambda $ runLater $ 173(PlatformImpl.java:294) 在 com.sun.glass.ui.InvokeLaterDispatcher $ Future.run(InvokeLaterDispatcher.java:95) 在com.sun.glass.ui.gtk.GtkApplication._runLoop(本机方法)处 com.sun.glass.ui.gtk.GtkApplication.lambda $ null $ 48(GtkApplication.java:139) 在java.lang.Thread.run(Thread.java:748)2018-11-11 20:10:11错误 LoggerTool:32-启动主窗口出错: /home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainElementView.fxml:15 /home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainWindow.fxml:27
由于:java.lang.IllegalArgumentException:无法设置 com.spaceopera.client.ihm.mainwindow.mainelement.specificelement.SpecificElementViewController 领域 com.spaceopera.client.ihm.mainwindow.mainelement.MainElementViewController.specificElement 到javafx.scene.layout.AnchorPane sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) 在 sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) 在 sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81) 在java.lang.reflect.Field.set(Field.java:764)在 javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163)在 javafx.fxml.FXMLLoader.access $ 1600(FXMLLoader.java:103)在 javafx.fxml.FXMLLoader $ ValueElement.processValue(FXMLLoader.java:857) 在 javafx.fxml.FXMLLoader $ ValueElement.processStartElement(FXMLLoader.java:751) 在javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707) 在javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)...还有19个
如果删除MainElementView.fxml中的include,则MainWindow.fxml中的include也有相同的例外(问题是将MainElement设置为anchorpane而不是specificElement)。
您能帮我解决这个问题吗?