如何从FXML控制器访问在我的Main Class中初始化的公共String

时间:2018-04-18 05:10:05

标签: java javafx fxml scenebuilder

在我的主要课程中我有

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);

    }

}

1 个答案:

答案 0 :(得分:1)

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

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