当前位置: 首页 > 知识库问答 >
问题:

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

孟健
2023-03-14

我有下面的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);

在< code>setScene之前,它将添加我的标签,但不添加来自fxml的元素。

有什么建议吗?谢谢

共有2个答案

苏培
2023-03-14

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

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 线程(工作线程)中完成的

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

谷隐水
2023-03-14

永远不要初始化@FXML-注入字段。你应该

@FXML Label ipopenstack ;
@FXML GridPane gridPane ;

代替

@FXML Label ipopenstack=new Label();
@FXML GridPane gridPane=new GridPane();
 类似资料:
  • 我想为我的JavaFX应用程序设置一个控制器,它自动将FXML文件“view.FXML”加载到成员“父根”中,并接受构造函数参数(在本例中为“字符串消息”)。 我让它工作得很好,但后来我试图使用Spring实例化一个DemoController实例,我收到了一个“NullPointerException:Root不能为null”。这让我很恼火,因为使用Spring的bean实例化似乎工作得很好,但

  • 我正在尝试创建javafx applet,使用IntelliJ idea。构建之后,我得到了三个文件:.jar、.jnlp和.html。如果我启动jar所有工作都很好,但是如果我尝试使用jnlp或html运行app,它会抛出异常: 为什么会这样?Jar正好包含所需的位于指定路径的fxml。 Java: FXML: 我做错了什么?请帮帮忙。

  • 我的应用程序有选项卡式窗格,所以为了保持fxml文件的可管理性,我有一个包含选项卡的主fxml文件,并为每个其他选项卡单独设置一个fxml。这很好,但出于某种原因,应用程序已经停止加载第二个标签。我试着在主应用程序中单独加载它,效果很好。我尝试创建一个新的fxml文件作为测试,并加载它,这也是有效的。但是,它不会将其加载到第二个选项卡中。此外,控制台没有输出。 第二个: 很抱歉代码太长,并提前感谢

  • 我是JavaFX新手。我想与我的场景互动 例如:当我单击一个按钮时,我添加了一个新按钮,Textfield。。在里面。然而,我搜索了一下,却没有找到满意的答案! 我的fxml文件: 我的控制器: }

  • 问题内容: 我有2个fxml文件: 布局(标题,菜单栏和内容) Anchorpane(应该放在另一个fxml文件的内容中) 我想知道如何从“主”场景将第二个文件加载到内容空间内。在javaFX中工作是一件好事,还是加载一个新场景更好? 我正在尝试做这样的事情,但是不起作用: 谢谢您的帮助。 问题答案: 为什么您的代码不起作用 加载程序会创建一个新的AnchorPane,但是您绝不会将新窗格添加到场

  • null 我正试着做这样的事情,但它不起作用: 谢谢你的帮助。