JavaFX 8:更改主要阶段的标题

Han*_*nes 4 java javafx javafx-8

当窗口已经显示时,如何更改主要阶段的标题?

Stage#setTitle(String) 显然不是诀窍.

Jam*_*s_D 6

setTitle(...) 似乎在这里工作正常:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class UpdateStageTitle extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextField titleField = new TextField();
        titleField.textProperty().addListener((obs, oldText, newText) -> 
            primaryStage.setTitle(newText));
        HBox root = new HBox(5, new Label("Window title: "), titleField);
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 350, 75);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Run Code Online (Sandbox Code Playgroud)

更新以下评论.

对于更复杂的例子,如果你需要设置在控制器的初始化方法,你需要安排之前,控制器必须对舞台的引用FXMLLoaderload()方法被调用.您可以通过调用setController加载程序或通过调用来执行此操作setControllerFactory.我通常更喜欢设置控制器工厂,因为它允许fx:controller在FXML中使用属性(设置控制器直接禁止这个).

所以:

PrimaryStageAware.java:

package application;

import javafx.stage.Stage;

public interface PrimaryStageAware {
    public void setPrimaryStage(Stage primaryStage);
}
Run Code Online (Sandbox Code Playgroud)

Main.java:

package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.fxml.FXMLLoader;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("TitleSetter.fxml"));
            loader.setControllerFactory((Class<?> type) -> {
                try {
                    Object controller = type.newInstance();
                    if (controller instanceof PrimaryStageAware) {
                        ((PrimaryStageAware) controller).setPrimaryStage(primaryStage);
                    }
                    return controller ;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
            HBox root = loader.load();
            Scene scene = new Scene(root,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Run Code Online (Sandbox Code Playgroud)

TitleSetter.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>

<HBox alignment="CENTER" xmlns:fx="http://javafx.com/fxml/1" 
    fx:controller="application.TitleSettingController">
    <Label text="Update title: "/>
    <TextField  fx:id="titleField" />
</HBox>
Run Code Online (Sandbox Code Playgroud)

TitleSettingController.java:

package application;

import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class TitleSettingController implements PrimaryStageAware {
    private Stage stage ;

    @FXML
    private TextField titleField ;

    @Override
    public void setPrimaryStage(Stage primaryStage) {
        this.stage = primaryStage ;
    }

    public void initialize()  {
        updateTitle("Initial title");
        titleField.textProperty().addListener((obs, oldTitle, newTitle) -> updateTitle(newTitle));
    }

    private void updateTitle(String title) {
        if (stage != null) {
            stage.setTitle(title);
        } else {
            System.out.println("Warning: null stage");
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

(将FXML与Java文件放在同一个包中).