【问题标题】:JavaFx : Set window title in fxml fileJavaFx:在 fxml 文件中设置窗口标题
【发布时间】:2013-08-19 08:54:45
【问题描述】:

我刚刚开始将 JavaFx 用于新应用程序。

我知道如何在 java 代码中设置窗口标题,但是如何在 fxml 文件中设置呢?

感谢您的帮助。

编辑: 这是我的代码

@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 中设置标题。

【问题讨论】:

    标签: javafx-2


    【解决方案1】:

    要在 FXML 中设置舞台的标题,您需要在 FXML 中构造舞台,如下所示:

    <?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 中的标题(没有代码后面)。

    【讨论】:

    猜你喜欢
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多