我有一个主要的课程,如下所示:

public class Window extends Application {


    @Override
    public void start(Stage foablak) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Foablak.fxml"));
        Scene scene = new Scene(root);

        foablak.setScene(scene);
        foablak.setWidth(900);
        foablak.setHeight(700);
        foablak.setResizable(false);
        foablak.setTitle("Window");

        foablak.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}


如何在不关闭窗口并打开新窗口的情况下从另一个.java类更新Title的标题?

最佳答案

仅出于公开的目的,使用static公开属性可能被认为是错误的设计。您有不同的方法来实现相同目的,例如,从设置阶段标题的Window类中公开一个方法。

public class Window extends Application {

    private Stage stage;

    @Override
    public void start(Stage foablak) throws Exception {
        stage = foablak;
        Parent root = FXMLLoader.load(getClass().getResource("Foablak.fxml"));
        Scene scene = new Scene(root);

        foablak.setScene(scene);
        foablak.setWidth(900);
        foablak.setHeight(700);
        foablak.setResizable(false);
        foablak.setTitle("Window");

        foablak.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    public void setStageTitle(String newTitle) {
        stage.setTitle(newTitle);
    }
}

08-03 16:55