简体   繁体   English

从另一个面板中删除一个面板时,停止执行线程

[英]Stop execution of a thread when a panel is removed from another panel

I have a panel (B) inside a panel (A). 我在面板(A)中有一个面板(B)。 Panel (B) starts executing a thread and updating its own GUI. 面板(B)开始执行线程并更新其自己的GUI。

But there is a case where user logs out of panel (B) and some other panel ( say Panel (C) ) comes in place of (B), while the thread keeps on executing. 但是在某些情况下,用户注销面板(B),而其他一些面板(例如Panel(C))代替(B),而线程继续执行。

I want to interrupt (stop) this thread when Panel (B) is no more visible ...any suggestions ? 当面板(B)不再可见时,我想中断(停止)该线程...有什么建议吗?

One option would be to add a ContainerListener to Panel A. Then check if Panel B is being removed and call "stopThread()" on Panel B. What I'd suggest is creating an interface with a "stopThread()" method (call it interface C), create your own Panel class for B such that B extends Panel implements C. 一种选择是将一个ContainerListener添加到面板A。然后检查是否删除了面板B并在面板B上调用“ stopThread()”。我建议使用“ stopThread()”方法创建一个接口(调用介面C),为B建立您自己的Panel类,以使B扩展Panel实现C。

Then in the ContainerListener.componentRemoved method, test the removed component: 然后在ContainerListener.componentRemoved方法中,测试已删除的组件:

if (component instanceof C)
{
  C c = (C)component;
  c.stopThread();
}

You could even include a startThread() and call that when added using a similar technique. 您甚至可以包含一个startThread(),并在使用类似技术添加时调用它。

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

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