gpt4 book ai didi

java - 在 Swing 中等待几秒钟

转载 作者:行者123 更新时间:2023-11-29 09:53:41 25 4
gpt4 key购买 nike

我需要在使用 Swing 接口(interface)的程序中调用两个不同的方法之间等待几秒钟。这些方法与 GUI 无关。

firstMethod();
//The interface is changed by other methods
...
//I want to Wait five seconds
secondMethod();

我试过使用 Swing Timer但它不起作用。显然,Timer启动但是是一个非阻塞操作,所以 secondMethod()立即执行。我可以使用 sleep() ,但这会使 GUI 卡住那五秒钟,因此界面直到在那之后才更新,我希望避免这种情况。

我在这里找到了一些使用建议 Future<V>我已经阅读了 Javadoc但我从未使用过ExecutorService之前,我担心我可能会为如此简单的事情编写一段过于复杂的代码。

知道怎么做吗?

最佳答案

Sounds like your code is something like:

firstMethod();

startTimer();

secondMethod();

I have tried using a Timer but it does not work

你不能只启动一个定时器什么都不做。当计时器触发时,您需要在计时器的 actionPerformed 中调用 secondMethod(...)

关于java - 在 Swing 中等待几秒钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24343002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com