简体   繁体   English

JavaFX:是否使LocalDateTime触发事件?

[英]JavaFX: Make LocalDateTime fire Event?

I want to automate an online game. 我想自动化一个在线游戏。 It worked while it was only terminal based. 它在仅基于终端的情况下有效。 But I wanted to add an JavaFX UI and now I don't know where to put Thread.waits until a certain LocalDateTime is reached. 但是我想添加一个JavaFX UI,现在我不知道在到达某个LocalDateTime之前将Thread.waits放在哪里。 Where does JavaFX wait for it's events (like button clicks, ...) and can I overwrite that method? JavaFX在哪里等待事件(例如按钮单击等),我可以覆盖该方法吗? Or is there a way to create custom LocalDateTime events and listeners for javaFX? 还是有办法为javaFX创建自定义LocalDateTime事件和侦听器?

If i add a Thread.wait in initialize() the whole scene doesn't appear until that certain LocalDateTime is reached. 如果我在initialize()中添加Thread.wait,直到达到特定的LocalDateTime才会出现整个场景。

If you want to have an event fire at some predetermined moment, but you want the user-interface of the app to remain responsive to the user's actions, then you need a background thread. 如果您希望在某个预定时刻触发事件,但希望应用程序的用户界面对用户的操作保持响应,则需要一个后台线程。 You should almost never sleep the GUI thread of your app, as that makes your app unresponsive and appear to be crashed. 您几乎应该永远不要休眠应用程序的GUI线程,因为这会使您的应用程序无响应,并且似乎已崩溃。 For fun, try it the wrong way: Drop a Thread.sleep call into your GUI code to see how it temporarily freezes your app. 为了娱乐,请尝试使用错误的方法: Thread.sleep调用放入GUI代码中,以查看其如何暂时冻结应用程序。

Learn about concurrency . 了解并发 Start with the Oracle Tutorial , and work your way up to eventually making an annual read of the Bible, Java Concurrency in Practice by Goetz et al. Oracle教程开始,逐步进行,直到最终每年阅读Goetz等人的《圣经, Java并发实践 》。

The Executors framework was built to make threading easier. Executors框架旨在简化线程。 Specifically, you will want the ScheduledExecutorService class. 具体来说,您将需要ScheduledExecutorService类。 This has been covered many many times already on Stack Overflow. 堆栈溢出已经对此进行了很多次讨论。 Search to learn more. 搜索以了解更多信息。

And learn about how to properly interact with the GUI thread from a background thread . 从后台线程了解如何与GUI线程正确交互

Never use LocalDateTime to track a moment, as explained in the class doc. 如类文档中所述,切勿使用LocalDateTime跟踪时刻。 For a moment, use Instant , OffsetDateTime , or ZonedDateTime . 暂时使用InstantOffsetDateTimeZonedDateTime For a span-of-time on the scale of hours-minutes-seconds, use Duration . 对于小时-分钟-秒的时间跨度,请使用Duration The Duration class offers methods such as toMinutes , toSeconds , the results of which you can feed as your delay to a ScheduledExecutorService . Duration类提供了诸如toMinutestoSeconds类的方法,您可以将其结果作为您的延迟提供给ScheduledExecutorService All of these classes have been covered many many times on Stack Overflow. 所有这些类在Stack Overflow上已经讨论了很多次。 Search to learn more. 搜索以了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM