如何从 FXML 控制器访问在我的 Main Class 中初始化的 public 字符串

How do I access a public String that's initialized in my Main Class from an FXML Controller

在我的主要 class 我有

public class Main extends Application {

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

    public String versionNumber = "v2.1";

    @Override
    public void start(Stage primaryStage) throws Exception{

        // SETTING UP THE STAGE
        Stage window;
        window = primaryStage;
        window.setTitle("Support Tool " + versionNumber);


        // SETTING UP THE SCENES
        Parent parentNewCallDetails = FXMLLoader.load(getClass().getResource("newCallDetails.fxml"));
        Scene scnNewCallDetails = new Scene (parentNewCallDetails, 800, 600);

        // CHOOSING THE SCENE AND SHOWING THE STAGE
        window.setScene(scnNewCallDetails);
        window.show();

    }

}

我基本上希望能够从我的 FXML 控制器中的以下代码中访问 String versionNumber,我在其中设置了我要启动的下一个场景的标题

public class newCallController {

    // ACTION COMPLETED WHEN CALL BUTTON IS PRESSED
    public void btnCall(MouseEvent event) throws IOException {

        // TODO LAUNCH THE NEXT CALL WINDOW
        Parent fxmlMainCallWindow = FXMLLoader.load(getClass().getResource("mainCallWindow.fxml"));
        Scene scnMainCallWindow = new Scene(fxmlMainCallWindow, 1000, 800);

        Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
        // THIS IS WHERE I WANT TO ACCESS THE VERSIONNUMBER STRING
        window.setTitle("Support Tool " + versionNumber);
        window.setScene(scnMainCallWindow);

    }

}

您可以使用静态字符串参数定义常量 class。您可以在任何地方使用它。喜欢:Constants.VERSION

public class Constants {
    public static final String VERSION = "v2.1";
}