简体   繁体   English

方法运行时,JFrame被完全锁定

[英]JFrame gets locked completely while method is running

I have a GUI(JFrame), with two Buttons and 1 Panel to show the result. 我有一个GUI(JFrame),带有两个按钮和1个面板以显示结果。 One Button is to start the algorithm, one for stopping it. 一键启动算法,一键停止算法。 By pressing start, a method is called and it starts running. 通过按开始,将调用一个方法并开始运行。 The runtime of this method varies from couple of seconds to 2-3 minutes, depending on the input. 此方法的运行时间从几秒钟到2-3分钟不等,具体取决于输入。

The problem I have hereby is, by pressing the start-button, the GUI gets completely locked. 我要解决的问题是,通过按开始按钮,GUI完全锁定。 I cannot press any button till the algorithm terminates. 在算法终止之前,我无法按任何按钮。 It would be great to be able to stop the algorithm and to visualize parts of the solution after a certain amound of time. 能够在一段时间后停止算法并可视化解决方案的各个部分,这将是非常棒的。

I checked every single line of the Frame, there is nothing that disables it. 我检查了框架的每一行,没有什么可以禁用它。

//If needed I can provide code, but its pretty long and just some hints and reasons for the problem would be great and I try to fix it by myself. //如果需要,我可以提供代码,但是它的时间很长,而且一些提示和问题的原因将非常有用,我会尝试自己解决。

thanks in advance. 提前致谢。

Don't put long-running tasks on the EDT, or the Event Dispatching Thread. 不要将长时间运行的任务放在EDT或事件调度线程上。 Use threading or a SwingWorker instead. 请使用线程或SwingWorker。 Hopefully that's enough google keywords to get you started. 希望有足够的Google关键字来帮助您入门。 :) :)

It sounds like your algorithm is running in the same thread as the UI components. 听起来您的算法与UI组件在同一线程中运行。 You probably want to read up on Concurrency and Concurrency in Swing to better understand how to create threads, monitor execution, integrating these concepts with a Swing-based user interface, and so forth. 您可能想阅读Swing中的 并发性并发性,以更好地了解如何创建线程,监视执行,将这些概念与基于Swing的用户界面集成在一起,等等。 At a very high level, you are going to need to somehow spawn a new thread when your algorithm starts and observe it for intermediate state changes to update the UI. 在很高的层次上,您将需要在算法启动时以某种方式生成一个新线程,并观察它的中间状态变化以更新UI。 You only want user interface related code running in the event dispatch thread . 您只希望在事件分发线程中运行与用户界面相关的代码。

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

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