Java GUI,需要暂停一个方法而不冻结GUI [英] Java GUI, need to pause a method without freezing GUI aswell

查看:126
本文介绍了Java GUI,需要暂停一个方法而不冻结GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题是由主线程上的sleep或wait调用引起的,如何解决这个问题的答案是将方法放入一个单独的线程然后让该线程进入休眠状态。但代码是一团糟,并没有真正的时间来解决它并将其拆分为单独的线程,并想知道是否有任何其他方式这样做?即使它不是使用GUI的最干净或最常见的做法。我只需要从方法中暂停几秒钟。

I know that this problem is caused by the sleep or wait calling on the main thread and that the answer on how to solve this will be to put the method into a seperate thread and then make that thread sleep. But the code is a mess and don't really have the time to sort it out and split it up into separate threads and was wondering if there is any other way of doing this? Even if it is not the cleanest or most common practice for working with GUIs. I only need about a seconds pause from the method.

推荐答案

如果不创建单独的线程,则无法执行此操作。用Java创建线程很容易。唯一需要注意的是,您只能通过主线程触摸UI。出于这个原因,你需要使用像 SwingUtilities.invokeLater()

You can't do it without creating a separate thread. Creating a thread in Java is easy. The only thing to pay attention to is that you can only touch the UI through the main thread. For this reason you need to use something like SwingUtilities.invokeLater().

这篇关于Java GUI,需要暂停一个方法而不冻结GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆