从swingWorker放置框架 [英] disposing frame from swingWorker

查看:53
本文介绍了从swingWorker放置框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我已经从do-in-Background方法中的swing worker类中的一个帧(假设)A ..中调用了swing工人,我有某些db查询,我也正在done()中调用了Frame B ..方法,但是我想放置框架A ..我该怎么做..?我无法在框架A类中编写dispose(),因为这会导致在看到新框架(框架B)之前就进行框架的处置...请帮助!

actually i have called the swing worker from a frame (Suppose) A.. in the swing worker class in do-in-Background method i have certain db queries and i am calling frame B too.. in the done() method however i want to dispose the frame A.. how can i do that..? i cannot write dispose() in frame A class because that results in disposing of frame before the new frame(frame B) is visible... Please help!!

class frameA extends JFrame{
public frameA(){
//done some operations..
SwingWorker worker=new Worker();
       worker.execute();

}
public static void main(string[] args){
  new frameA();
}

}

和工人阶级

class Worker extends SwingWorker<Void, String> {



public Worker() {
    super();


}

//Executed on the Event Dispatch Thread after the doInBackground method is finished
@Override
protected void done() {
    //want to dispose the frameA here..


}

@Override
protected Void doInBackground() throws Exception {
    // some db queries
  new frameB().setVisible(true);  
  // call to frameb
}

推荐答案

  1. 通常会覆盖SwingWorkerdone()方法以显示最终结果.之上 doInBackground()完成后,SwingWorker自动调用 EDT中的done().因此,在此函数中添加框架的不可见代码和可见代码.

  1. The done() method of the SwingWorker is usually overridden to display the final result. Upon completion of doInBackground() , the SwingWorker automaticlly invokes done() in the EDT. So put your frame's invisible and visible code in this function.

doInBackground()不能执行任何GUI渲染任务.您可以从doInBackground()函数调用publish(V),该函数又调用process(V)方法以在EDT中运行并执行GUI渲染任务.

The doInBackground() is not meant to do any GUI rendering task. You can invoke publish(V) from doInBackground() function which in turn invokes The process(V) method to run inside the EDT and performing GUI rendering task.

因此,示例解决方案将是:

So a sample solution would be:

class Worker extends SwingWorker<Void, String> {

  JFrame frameA;

  public Worker(JFrame frameA) {
    this.frameA = frameA;

  }

  @Override
  protected void done() {
    frameA.dispose();
    new frameB().setVisible(true); 

  }
  //other code
}

现在,通过将目标框架传递给它的构造函数来创建SwingWorker实例:new Worker(frame);在您的情况下,您可能可以使用this

Now, create you SwingWorker instance by passing the target frame to it's constructor: new Worker(frame); For your context you probably could make use of this

但是,您不应该真正将应用程序设计为依赖多个JFrame.有理由不使用多个JFrame窗口.有关更多信息,请参见 多个JFrame的使用,良好/不良做法? .有关需要多个框架的用例的一般解决方法是

However, you should not really design your application to be dependent on multiple JFrame. There are reasons for not to use multiple JFrame window. For more, see The Use of Multiple JFrames, Good/Bad Practice?. A general work around with use case where multiple frame would be needed is explained here.

这篇关于从swingWorker放置框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆