JavaFX:从 FXML 加载场景但从代码添加标签

标签 java javafx javafx-2 javafx-8

我有以下 FXML,可以在另一个场景中按下按钮时加载

<GridPane fx:id="gridPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" styleClass="root" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="VMProvOpenstack.Controller">
    <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="295.0" minWidth="10.0" prefWidth="162.0" />
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="295.0" minWidth="10.0" prefWidth="162.0" />
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="358.0" minWidth="0.0" prefWidth="358.0" />
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="450.0" minWidth="0.0" prefWidth="0.0" />
    </columnConstraints>
    <rowConstraints>
        <RowConstraints maxHeight="166.0" minHeight="10.0" prefHeight="96.0" vgrow="SOMETIMES" />
        <RowConstraints maxHeight="238.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints maxHeight="305.0" minHeight="10.0" prefHeight="257.0" vgrow="SOMETIMES" />
    </rowConstraints>
    <children>
        <Region prefHeight="36.0" prefWidth="538.0" style="-fx-background-color: brown;" GridPane.rowIndex="1">
            <effect>
                <Blend />
            </effect>
        </Region>
        <Region prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: brown;" GridPane.columnIndex="1" GridPane.rowIndex="1" />
        <Region layoutX="10.0" layoutY="112.0" prefHeight="36.0" prefWidth="538.0" style="-fx-background-color: brown;" GridPane.columnIndex="2" GridPane.rowIndex="1">
            <effect>
                <Blend />
            </effect>
        </Region>
        <Label fx:id="loggedinUser" contentDisplay="RIGHT" prefHeight="21.0" prefWidth="199.0" text="Loged in as:  " textFill="green" GridPane.columnIndex="2" GridPane.rowIndex="0" />
        <Label fx:id="ipopenstack" contentDisplay="RIGHT" prefHeight="21.0" prefWidth="199.0" text="IP : 192.168.131.132" textFill="#eb0f0f" translateX="-40.0" GridPane.columnIndex="2" GridPane.rowIndex="2" />
    </children>
</GridPane>

代码是:

@FXM Label ipopenstack=new Label();
@FXML GridPane gridPane=new GridPane();
//...
Stage stageTheEventSourceNodeBelongs = (Stage)((Node)event.getSource()).getScene().getWindow();
try {
    Scene scene= new Scene(FXMLLoader.load(getClass().getResource("my.fxml")),700,500);
    stageTheEventSourceNodeBelongs.setScene(scene);
} catch (IOException e) {
    e.printStackTrace();
}

如果我这样做,一切都会显示得很好。 但我想向这个场景添加一个带有动态消息的标签,并且仍然保留 fxml 中的所有内容。 如果我这样做,消息是一个字符串

Label ipL=new Label(message);                  
gridPane.add(ipL, 2, 2);
scene.setRoot(gridPane);

setScene之前,它会添加我的标签,但不会添加fxml中的元素。

请问有什么建议吗? 谢谢

最佳答案

永远不要初始化 @FXML 注入(inject)的字段。你应该有

@FXML Label ipopenstack ;
@FXML GridPane gridPane ;

而不是

@FXML Label ipopenstack=new Label();
@FXML GridPane gridPane=new GridPane();

关于JavaFX:从 FXML 加载场景但从代码添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30991954/

相关文章:

java - 如何使用 ArrayList 中的字符串填充 ViewPager?

java - 是否可以分发使用 C 生成的可执行文件的 java 桌面应用程序?

java - 根据 String.valueOf(Object obj) 的值创建一个新 Shape

java - 在新线程中调用方法的简单方法

javafx-2 - 绑定(bind)到可以切换的对象的 JavaFX 属性

java - MongoDB DBRef 列表在 Spring Boot 中返回 null

java - Spring 4.0 : Resolve Type of GenericDao

java - JavaFX 中的多个 boolean 值绑定(bind)

javafx-2 - javaFX2 miglayout 可移植性

javafx-2 - 在全屏 JavaFX 中更改场景