我正在处理JavaFX,我正在尝试将图形合成器从FXML页面添加到java类中,但是它总是返回null值
Page.fxml
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.BorderPane?>
<ScrollPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<content>
<BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" fx:id="Mycontent">
<top>
<fx:include source="TopMenu.fxml" />
</top>
<center>
<fx:include source="Operation.fxml" />
</center>
<left>
<fx:include source="SideBar_Inhumer.fxml" />
</left>
<bottom>
<fx:include source="Footer.fxml" />
</bottom>
</BorderPane>
</content>
</ScrollPane>
Controller.java
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.layout.BorderPane;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
public BorderPane Mycontent;
@FXML
public void goToDemandeur(){
System.out.println(Mycontent);
//Mycontent.setCenter(FXMLLoader.load(getClass().getResource("Demandeur.fxml")));
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
}
我的代码始终显示“空”
例如,如果我尝试使用Mycontent.getCenter()
,则会出现此错误
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
(这是正常的,因为Mycontent为null,但是为什么它为null?)
PS:单击goToDemandeur()
后,我的方法SideBar_Inhumer.fxml
被调用到另一个fxml页面onMouseClicked="#goToDemandeur"
SideBar_Inhumer.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox layoutX="77.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Pane >
<children>
<Label alignment="CENTER" text="Inhumer" >
<font>
<Font size="24.0" />
</font>
<padding>
<Insets bottom="20.0" left="25.0" right="25.0" top="30.0" />
</padding>
</Label>
</children>
</Pane>
<Pane style="-fx-background-color: #F08080;" onMouseClicked="#goToDemandeur">
<children>
<Label text="Demandeur" >
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
<VBox.margin>
<Insets />
</VBox.margin>
</Pane>
<Pane style="-fx-background-color: #C6E2B9;">
<children>
<Label text="Defunt">
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
</Pane>
<Pane style="-fx-background-color: #FBF6A5;">
<children>
<Label text="Emplacement">
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
</Pane>
<Pane style="-fx-background-color: #FFCC99;">
<children>
<Label text="Prestataire">
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
</Pane>
<Pane style="-fx-background-color: #D8B46D;">
<children>
<Label text="Opération">
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
</Pane>
</children>
</VBox>
Main.java
package View;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("../View/Page.fxml"));
primaryStage.setTitle("Finalys");
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
Scene scene =new Scene(root, 600, 475);
primaryStage.setX(bounds.getMinX());
primaryStage.setY(bounds.getMinY());
primaryStage.setWidth(bounds.getWidth());
primaryStage.setHeight(bounds.getHeight());
primaryStage.setScene(scene);
primaryStage.show();
scene.getStylesheets().add(Main.class.getResource("bootstrap2.css").toExternalForm());
}
public static void main(String[] args) {
launch(args);
}
}