繁体   English   中英

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

[英]JavaFX: Load scene from FXML but add label from code

我在另一个场景中按下按钮时加载了以下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();
}

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

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

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

有什么建议吗? 谢谢

切勿初始化@FXML字段。 你应该有

@FXML Label ipopenstack ;
@FXML GridPane gridPane ;

代替

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

首先尝试从场景中获取窗格,然后进行更新

Parent p=scene.getRoot();
// Navigate to the appropriate pane object
//the try adding the label to the pane object
pane.add(ipL, 2, 2);

并确保所有这些操作都在JavaFx线程(工作线程)中完成

或者您可以做的另一件事是,当您需要更新消息时,立即保留标签的添加和隐藏,只需使用消息更新标签并关闭其隐藏即可

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM