JavaFX并发事件

huangapple go评论104阅读模式
英文:

JavaFx Concurrency Event

问题

  1. ButtonType[] buttonTypes = new ButtonType[2];
  2. buttonTypes[0] = new ButtonType("立即更新", ButtonData.OK_DONE);
  3. buttonTypes[1] = new ButtonType("稍后更新", ButtonData.CANCEL_CLOSE);
  4. EventHandler[] eventHandlers = new EventHandler[2];
  5. eventHandlers[0] = new EventHandler<ActionEvent>() {
  6. @Override
  7. public void handle(ActionEvent event) {
  8. // 在这里处理立即更新的逻辑
  9. }
  10. };
  11. eventHandlers[1] = new EventHandler<ActionEvent>() {
  12. @Override
  13. public void handle(ActionEvent event) {
  14. // 在这里处理稍后更新的逻辑
  15. }
  16. };
  17. Dialog.AlertDialog(
  18. Alert.AlertType.CONFIRMATION,
  19. Main.productName,
  20. "更新确认",
  21. "有新的更新可用。您想要更新吗?",
  22. buttonTypes,
  23. Modality.APPLICATION_MODAL,
  24. StageStyle.DECORATED,
  25. stage,
  26. eventHandlers
  27. );

注意:上述代码为您提供了原始代码的翻译,但没有添加时间延迟的功能。如果您希望在点击"稍后更新"后30分钟再次显示对话框,您需要在适当的位置添加计时器和相应的逻辑来实现此功能。

英文:

I have a problem in JavaFx code.
I have one dialog box. Which has 2 buttons UPDATE NOW and UPDATE LATER.
My problem is : when I click on "UPDATE LATER" the dialog box should close and after 30 mins again it should pop up the same dialog box.
My main thread will be running only. I just need to remind it again after 30 mins whenever I click on UPDATE LATER.

Please help how can I do that in javaFx.

  1. ButtonType[] buttonTypes = new ButtonType[2];
  2. buttonTypes[0] = new ButtonType(&quot;Update Now&quot;, ButtonData.OK_DONE);
  3. buttonTypes[1] = new ButtonType(&quot;Update Later&quot;, ButtonData.CANCEL_CLOSE);
  4. EventHandler[] eventHandlers = new EventHandler[2];
  5. eventHandlers[0] = new EventHandler&lt;ActionEvent&gt;() {
  6. @Override
  7. public void handle(ActionEvent event) {
  8. ;
  9. }
  10. };
  11. eventHandlers[1] = new EventHandler&lt;ActionEvent&gt;() {
  12. @Override
  13. public void handle(ActionEvent event) {
  14. }
  15. };
  16. Dialog.AlertDialog(Alert.AlertType.CONFIRMATION, Main.productName, &quot;Update Confirmation&quot;, &quot;New Update is available. DO you want to update ?&quot;, buttonTypes, Modality.APPLICATION_MODAL, StageStyle.DECORATED, stage, eventHandlers);

答案1

得分: 3

为了在一段时间后执行某些操作,使用PauseTransition

在这种情况下,您可以这样做:

  1. public void handle(ActionEvent event) {
  2. PauseTransition delay = new PauseTransition(Duration.minutes(30));
  3. delay.setOnFinished(evt -> {
  4. // 30分钟后需要执行的操作....
  5. });
  6. delay.play();
  7. }
英文:

To do something after some period of time, use a PauseTransition.

In this case, you can do

  1. public void handle(ActionEvent event) {
  2. PauseTransition delay = new PauseTransition(Duration.minutes(30));
  3. delay.setOnFinished(evt -&gt; {
  4. // whatever you need to do in 30 minutes....
  5. });
  6. delay.play();
  7. }

huangapple
  • 本文由 发表于 2020年9月1日 00:47:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63674903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定