我正在创建一个简单的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)