Javafx如何在控制器中访问应用程序对象?
我正在创建一个 简单 Javafx程序。在 helloapplication
我想做逻辑,在 hellocontroller
我想收听按钮点击,然后在 HelloApplcation
中执行方法。
helloapplication.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();
}
}
hellocontroller.java:
HelloApplication helloApp=new HelloApplication(); //this creates another instance...
@FXML
protected void onHelloButtonClick() {
helloApp.StartCounter("stop");
}
,但是我如何访问 helloapplication 在 abound> abound> abound> pluench()
上创建的对象?
loader.getApplication()
之类的东西将是完美的,而不是在控制器中创建新对象(无论如何都无法使用)。
现在,鼠标单击helloapp.startcounter()原因: java.lang.reflect.invocationTargetException
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如何访问控制器中的应用程序对象?
直接回答您的问题。
init
方法中的静态变量。这有点像单身模式,但是应用程序实例是由
启动
方法创建的。然后,可以使用以下类别从任何类访问该应用程序:
示例代码和方法
您不应创建另一个应用程序实例。
abougn() method的调用来创建。
启动()
方法只能为JVM进程调用一次。在应用程序类中绝不应该有
@fxml
注释。@fxml
仅在控制器中使用,应用程序类也不应为控制器。您的整体方法不正确:
您不应该这样做。
应用程序类负责应用程序生命周期。除非它是一个琐碎的独立hello世界风格应用程序,否则它不应该做其他事情。绝对不应为FXML控制器回调提供服务。
建议的方法
直接在控制器或专用服务类中执行应用程序逻辑。
我建议您不必尝试调试和修复您的当前代码,而是建议您改用建议的方法。
要更多地了解此类方法,请参见:
How can I access the Application object in Controller?
To directly answer your question.
init
method.This is kind of like a singleton pattern, but the application instance is created by the
launch
method.Then the application can be accessed from any class using:
Issues with your sample code and approach
You should not create another application instance.
The application lifecycle javadoc notes there should be only one application instance in the JVM and this will be created by the invocation of the
launch()
method.The
launch()
method can only be called once for the JVM process.There should never be an
@FXML
annotation in an Application class.@FXML
is only used in a controller and the application class should not also be a controller.Your overall approach is incorrect:
You should not do this.
The application class is responsible for the application lifecycle. Unless it is a trivial self-contained hello world style application, it should not be doing anything else. It definitely should not be servicing fxml controller callbacks.
Recommended Approach
Perform your application logic either directly in the controller or in a dedicated service class.
Rather than trying to debug and fix your current code, I recommend you work on implementing the suggested approach instead.
To understand such approaches more, see: