Java BorderPane始终返回空值
我在处理JavaFX,我试图将我的图形组件从FXML页面放入java类中,但它总是返回null值 Page.fxml 我的代码总是打印“null” 例如,如果我尝试Java BorderPane始终返回空值,java,javafx,fxml,Java,Javafx,Fxml,我在处理JavaFX,我试图将我的图形组件从FXML页面放入java类中,但它总是返回null值 Page.fxml 我的代码总是打印“null” 例如,如果我尝试Mycontent.getCenter()它会给出这个错误 Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException (这是正常的,因为Myconte
Mycontent.getCenter()
它会给出这个错误
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
(这是正常的,因为Mycontent是空的,但为什么它是空的?)
PS:mouseClicked=“#gotodemandur”
侧栏_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>
请您展示一下如何加载Page.fxml?可能的重复我对JavaFX不太熟悉,但我假设它使用基于基本Java代码约定的JavaBeans约定,该约定规定变量、字段等应使用以小写字符开头的名称,然后使用camelcase。特别是第一个要求是为了减少类名和字段名之间的混淆,即
Mycontent
看起来像类名,但实际上不是。尝试mycontent
或更好的mycontent
来代替。是的,问题确实是不同fxml使用相同的控制器:这没有用,因为每次加载都会创建一个新的控制器实例,其中只注入fxml知道的字段,这就是为什么边栏不能注入myContent,而myContent只是保持为空。对不同的fxml使用不同的控制器,所有这些都可以正常工作:)可能的重复请您演示如何加载Page.fxml?可能的重复我对JavaFX不太熟悉,但我假设它使用基于基本Java代码约定的Java Beans约定,并声明变量,字段等应使用以小写字符开头的名称,然后使用大小写。特别是第一个要求是为了减少类名和字段名之间的混淆,即Mycontent
看起来像类名,但实际上不是。尝试mycontent
或更好的mycontent
来代替。是的,问题确实是不同fxml使用相同的控制器:这没有用,因为每次加载都会创建一个新的控制器实例,其中只注入fxml知道的字段,这就是为什么边栏不能注入myContent,而myContent只是保持为空。对不同的FXML使用不同的控制器,所有这些控制器都可以正常工作:)可能重复
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
<?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>
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);
}
}