执行被困在按钮内

发布于 2024-11-08 16:47:25 字数 269 浏览 0 评论 0原文

我有一个在按钮内部调用的方法,该方法几乎运行无限循环。运行此方法时我无法访问其他按钮。

在运行此方法时如何释放界面以访问其他按钮?

//methods inside the button
this.setCrawlingParameters();
webcrawler = MasterCrawler.getInstance();
webcrawler.resumeCrawling(); //<-- the infinite loop method

I have a method called inside a button that run almost an infinite loop. I can't access the other buttons while running this method.

How I make to free the interface to access other buttons while running this method?

//methods inside the button
this.setCrawlingParameters();
webcrawler = MasterCrawler.getInstance();
webcrawler.resumeCrawling(); //<-- the infinite loop method

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

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

发布评论

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

评论(2

朦胧时间 2024-11-15 16:47:25

你需要使用一个 SwingWorker

的方式Swing 的工作原理是它有一个主线程,即管理 UI 的事件调度线程 (EDT)。在 Swing 文档中,您将看到建议永远不要在 EDT 中运行长时间运行的任务,因为它管理 UI,如果您执行计算量大的操作,您的 UI 将冻结。这正是你正在做的事情。

因此,您需要让按钮调用 SwingWorker,以便在另一个线程中完成困难的工作。注意不要从 SwingWorker 修改 UI 元素;所有UI代码都需要在EDT中执行。

如果单击 SwingWorker 的链接,您将看到以下内容:

不应运行耗时的任务
在事件调度线程上。
否则应用程序将变成
没有反应。摆动组件应
可在事件调度上访问
仅线程

以及有关如何使用 SwingWorker 的示例的链接。

you need to use a SwingWorker

The way Swing works is that it has one main thread, the Event Dispatch Thread(EDT) that manages the UI. In the Swing documentation, you will see that it is recommended to never to long-running tasks in the EDT, because, since it manages the UI, if you do something computationally heavy your UI will freeze up. This is exactly what you are doing.

So you need to have your button invoke a SwingWorker so the hard stuff is done in another thread. Be careful not to modify UI elements from the SwingWorker; all UI code needs to be executed in the EDT.

If you click the link for SwingWorker, you will see this:

Time-consuming tasks should not be run
on the Event Dispatch Thread.
Otherwise the application becomes
unresponsive. Swing components should
be accessed on the Event Dispatch
Thread only

as well as links to examples on how to use a SwingWorker.

街角迷惘 2024-11-15 16:47:25

开始一个新线程:

// In your button:
Runnable runner = new Runnable()
{
    public void run()
    {
         setCrawlingParameters(); // I removed the "this", you can replace with a qualified this
         webcrawler = MasterCrawler.getInstance();
         webcrawler.resumeCrawling(); //<-- the infinite loop method
    }
}
new Thread(runner, "A name for your thread").start();

Start a new Thread:

// In your button:
Runnable runner = new Runnable()
{
    public void run()
    {
         setCrawlingParameters(); // I removed the "this", you can replace with a qualified this
         webcrawler = MasterCrawler.getInstance();
         webcrawler.resumeCrawling(); //<-- the infinite loop method
    }
}
new Thread(runner, "A name for your thread").start();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文