暂停循环,但仍然能够看到文本

发布于 2024-12-25 17:24:44 字数 111 浏览 1 评论 0原文

我有一个问题。 我使用循环绘制一些东西,并使用睡眠来暂停执行。 与此同时,我希望能够看到文本区域中的内容,该区域有很多行,我必须向下滚动才能看到它们。但我不能。使用睡眠这是不可能的。 有什么建议吗? 谢谢。

I have a problem.
I draw something using a loop and I use Sleep to pause the execution.
In the meantime, I want to be able to see what's in a textArea, which has a lot of lines and I have to scroll down to see them.But I can't.Using Sleep it's not possible.
Any suggestions?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾城花音 2025-01-01 17:24:45

不幸的是,您没有发布任何代码片段,也没有清楚地解释无法向下滚动文本区域意味着什么。

但我可以假设您的 UI 被冻结,因为您在 UI 线程中调用 sleep() 。可能是从重载的 paint()repaint() 方法调用的?如果我的猜测是正确的,请尝试执行循环并从单独的线程睡眠。您或许可以使用 SwingWorker

为了给您提供更具体的建议,我需要更多信息。

Unfortunately you have not posted any code snippet and have not explained clearly what does it mean that you cannot scroll down your text area.

But I can suppose that your UI is frozen because you are invoking sleep() in UI thread. Probably it is called from your overloaded paint() or repaint() method? If my guess is correct try to do your loop and sleep from separate thread. You can probably use SwingWorker.

To give you more concrete recommendations I need more information.

乙白 2025-01-01 17:24:44

不要在事件线程上使用睡眠。我假设您的应用程序是一个 Swing 应用程序(请告诉我们这是否正确),如果是这样,在事件线程上调用 Thread.sleep(...) 将使您的整个 GUI 进入睡眠状态使其没有反应。相反,如果这是 Swing 应用程序,请使用 Swing 计时器。

教程:如何使用 Swing 计时器

编辑 1< /strong>
注意:这里有很多使用 SwingWorker 的建议,这些建议很有用,但我会推迟使用它们,除非需要在时间间隔之间完成的代码非常占用 CPU 资源并且需要一些时间才能完成,例如读取中型到大型文件。如果您所做的只是画一个圆,然后暂停,然后画另一个圆,那么 SwingWorker 就显得大材小用了,而 Swing Timer 则是更容易实现的方法。

与所有建议一样,答案将取决于您问题的具体情况,您可能希望告诉我们更多信息。

Don't use sleep on the event thread. I assume that yours is a Swing application (please tell us if this is correct or not), and if so, calling Thread.sleep(...) on the event thread will put your whole GUI asleep making it unresponsive. Instead, if this is a Swing application, use a Swing Timer.

Tutorial: How to Use Swing Timers

Edit 1
Note: there are many recommendations here to use a SwingWorker, and these can be useful, but I would hold off using them unless the code that needs to be done between the time gaps is very CPU intensive and takes a bit of time to complete, such as reading in a medium to large file. If all you're doing is drawing a circle, then pausing, then drawing another circle, a SwingWorker is gross overkill and again a Swing Timer, which is much easier to implement, is the way to go.

As with all recommendations, the answers will depend on the specifics of your problem, and you may wish to tell us more.

爱你不解释 2025-01-01 17:24:44

您永远不应该在 UI 线程上调用 sleep,因为这会阻止所有与 UI 相关的操作(正如您刚刚发现的那样)。

UI 的典型用例是在工作线程上完成繁重的工作(例如由 SwingWorker 类提供的),并在工作完成时更新 UI(或以一定的时间间隔更新 UI)。显示进度)。

如果您想以一定的时间间隔执行某些代码,您可以使用 Timer(Swing 变体,而不是 java.util 版本),它将在正确的时间执行其代码线程并在两次执行之间保持 UI 线程空闲。

You should never call sleep on the UI thread, since this will block all UI related operations (as you just discovered).

The typical use-case with a UI is to do the heavy work on a worker thread (like for example provided by the SwingWorker class) and update your UI when the work is done (or at certain intervals to display progress).

If you want to execute some code at certain intervals, you use the Timer (the Swing variant, not the java.util version), which will execute its code on the correct thread and leave the UI thread free between two executions.

梦与时光遇 2025-01-01 17:24:44

您需要基本上记住循环的状态(您必须到达的位置等)并停止添加。让用户滚动,当您想要“取消暂停”时,从您所在的位置恢复循环。

或者,在单独的线程中执行循环,使用适当的 SwingWorker 等调用来封送回 UI 线程以影响 UI,然后在另一个线程中休眠。这不会影响用户界面。但它可能会变得相当繁琐。

You'll need to basically remember the state of the loop (where you'd got to etc) and stop adding. Let the user scroll around, and when you want to "unpause", resume the loop from where you'd got to.

Alternatively, perform the looping in a separate thread, using appropriate SwingWorker etc calls to marshal back to the UI thread to affect the UI, and then sleep in the other thread. That won't affect the UI. It's likely to get pretty fiddly though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文