小编Rek*_*eft的帖子

JavaFX 如何访问控制器中的应用程序对象?

我正在创建一个简单的javaFX 程序。我HelloApplication想要执行逻辑,并且HelloController我想要监听按钮单击,然后执行HelloApplcation.

你好应用程序.java:

Timeline timeline;
@FXML
HelloController HC;
FXMLLoader fxmlLoader;
private int counter=0;
public void start(Stage stage) throws IOException {
        fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));

        Scene scene = new Scene(fxmlLoader.load(), 640, 480);
        stage.setTitle("Program!");
        stage.setScene(scene);
        stage.show();
        StartCounter("start");
}
public void StartCounter(String status)
    {
        HC = fxmlLoader.getController();
        if(timeline==null) {
            timeline = new Timeline(new KeyFrame(Duration.seconds(1), ev -> {
                HC.getCmd().setText(String.valueOf(counter));
                counter++;
            }));
            timeline.setCycleCount(Animation.INDEFINITE);
        }else{
            if(status=="stop")
                timeline.stop();
            else if(status=="start")
                timeline.play();
        }
}
Run Code Online (Sandbox Code Playgroud)

你好控制器.java:

  HelloApplication helloApp=new HelloApplication(); //this creates …
Run Code Online (Sandbox Code Playgroud)

java javafx

0
推荐指数
1
解决办法
591
查看次数

标签 统计

java ×1

javafx ×1