JavaFx:在fxml文件中设置窗口标题 [英] JavaFx : Set window title in fxml file
问题描述
我刚刚开始将JavaFx用于新的应用程序.
I'm just starting to use JavaFx for a new application.
我知道如何在Java代码中设置窗口标题,但是如何在fxml文件中设置窗口标题?
I know how to set the window title in the java code but how to set it in a fxml file ?
感谢您的帮助.
这是我的代码
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
primaryStage.setTitle(applicationName);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
我只想在Main.fxml中设置标题.
I just want set the title in Main.fxml.
推荐答案
要在FXML中设置舞台的标题,您需要在FXML中构建舞台,如下所示:
to set the title of the stage in FXML, you need to construct the stage in FXML, like this:
<?xml version="1.0" encoding="utf-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.stage.Stage?>
<?import javafx.scene.Scene?>
<?import javafx.scene.control.Label?>
<Stage title="Some Stage">
<scene>
<Scene>
<VBox xmlns:fx="http://javafx.com/fxml">
<children>
<Label text="John Doe"/>
</children>
</VBox>
</Scene>
</scene>
</Stage>
如果仅通过FXML构造场景的根元素(在我的示例中为VBox),然后像您一样将其放入场景中(这是通常的方法),则无法设置直接在FXML中添加标题(没有后面的代码).
If you only construct the root element of the scene (in my example, the VBox) via FXML and then put it into a scene afterwards like you do it (which is the common way), then it is impossible to set the title in FXML directly (without code behind).
这篇关于JavaFx:在fxml文件中设置窗口标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!