提问人:Joao Soares 提问时间:5/31/2022 最后编辑:Joao Soares 更新时间:5/31/2022 访问量:205
JavaFX - 将 FXML 组件加载到 Tab 时出现 FXML 加载异常
JavaFX - FXML Load Exception on loading FXML component into Tab
问:
我想要一个带有选项卡的页面,在每个选项卡中,我将调用另一个FXML组件。我已经尝试了很多答案,但没有一个对我有用。我的 TabPane 所在的主页以及我想调用的选项卡之一。现在,我正在尝试这种方法:home.fxml
view.fxml
home.fxml 页面:
<VBox xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1"
prefHeight="${screen.visualBounds.height}"
fx:controller="pkg.magazineservice.home_controller"
fx:id="wrapper">
<fx:define>
<Screen fx:factory="getPrimary" fx:id="screen" />
</fx:define>
<VBox style="-fx-background-color: #003d80" alignment="CENTER" prefHeight="${wrapper.height * 0.10}" >
<Label BorderPane.alignment="CENTER" style="-fx-text-fill: white" text="Magazine Service" >
<font>
<Font size="32" />
</font>
</Label>
</VBox>
<TabPane style="-fx-close-tab-animation: none" fx:id="contentWrapper" prefHeight="${wrapper.height * 0.9}" prefWidth="${screen.visualBounds.width}">
<Tab fx:id="View" text="View">
<VBox>
<!--<fx:include source="view.fxml" />-->
</VBox>
</Tab>
<Tab fx:id="Create" text="Create">
<VBox>
<!--<fx:include source="view.fxml" />-->
</VBox>
</Tab>
<Tab fx:id="Edit" text="Edit">
<VBox>
<!--<fx:include source="view.fxml" />-->
</VBox>
</Tab>
</TabPane>
在它的控制器上,我正在尝试初始化一个页面:home_controller.java
@Override
public void initialize(URL location, ResourceBundle resources)
{
try
{
Tab tab = new Tab();
View.setContent((Node) FXMLLoader.load(this.getClass().getResource("view.fxml")));
}
catch(IOException iex)
{
System.out.println(iex.getCause().toString());
System.out.println("File not found");
}
}
但是,它不断抛出一个.JavaNullPointerException
这是:view.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.stage.Screen?>
<VBox xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1"
fx:id="View"
alignment="CENTER"
prefHeight="${screen.height * 0.9}"
prefWidth="${screen.visualBounds.width}"
fx:controller="pkg.magazineservice.view_controller"
>
<fx:define>
<Screen fx:factory="getPrimary" fx:id="screen" />
</fx:define>
<VBox fx:id="listsWrapper" alignment="CENTER" prefHeight="${View.height}" prefWidth="${View.width * 0.50}">
<VBox>
</VBox>
<Separator orientation="HORIZONTAL" prefWidth="${listsWrapper.width}" style="-fx-background-color: black" />
<VBox>
</VBox>
<Separator orientation="VERTICAL" prefHeight="${View.height}" style="-fx-background-color: black" />
<VBox fx:id="detailsWrapper" alignment="CENTER" prefHeight="${View.height}" prefWidth="${View.width * 0.50}">
</VBox>
</VBox>
我已经尝试使用过,但它一直抱怨未设置root,我不知道如何从家庭控制器实现root。任何帮助和建议都很棒!fx:root
编辑: 堆栈跟踪:
javafx.fxml.LoadException:
/MagazineService/target/classes/pkg/magazineservice/view.fxml:40
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at com.magazineservice.magazineservice/pkg.magazineservice.home_controller.initialize(home_controller.java:40)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at com.magazineservice.magazineservice/pkg.magazineservice.HelloApplication.start(HelloApplication.java:25)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.NullPointerException
at javafx.fxml/com.sun.javafx.fxml.expression.Expression.lambda$multiply$2(Expression.java:928)
at javafx.fxml/com.sun.javafx.fxml.expression.BinaryExpression.evaluate(BinaryExpression.java:55)
at javafx.fxml/com.sun.javafx.fxml.expression.ExpressionValue.getValue(ExpressionValue.java:192)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.addListener(ExpressionHelper.java:53)
at javafx.base/javafx.beans.value.ObservableValueBase.addListener(ObservableValueBase.java:55)
at javafx.fxml/com.sun.javafx.fxml.expression.ExpressionValue.addListener(ExpressionValue.java:201)
at javafx.base/javafx.beans.binding.DoubleBinding.bind(DoubleBinding.java:152)
at javafx.base/javafx.beans.property.DoublePropertyBase$ValueWrapper.<init>(DoublePropertyBase.java:277)
at javafx.base/javafx.beans.property.DoublePropertyBase$2.<init>(DoublePropertyBase.java:182)
at javafx.base/javafx.beans.property.DoublePropertyBase.bind(DoublePropertyBase.java:182)
at javafx.graphics/javafx.css.StyleableDoubleProperty.bind(StyleableDoubleProperty.java:75)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:326)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:242)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:775)
at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2838)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2557)
... 21 more
答:
0赞
Joao Soares
5/31/2022
#1
多亏了@James_D的评论,他指出了我,这就是问题所在。只需从中删除这些行即可解决我的问题。prefWidth
prefHeight
view.fxml
溶液:
<VBox xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1"
fx:id="View"
alignment="CENTER"
fx:controller="pkg.magazineservice.view_controller"
>
<!-- NO WIDTH AND HEIGHT-->
评论
prefHeight
prefWidth