简体   繁体   English

如何在操作仍在进行时关闭JFrame

[英]How to close JFrame while operation is still in progress

In my swings windows application after click the run button some operation is executed. 在我的swings Windows应用程序中,单击“运行”按钮后,将执行某些操作。 If i am try to close window when operation is still in progress, close operation is not working. 如果在操作仍在进行时试图关闭窗口,则关闭操作无效。 After complete the execution of process then only close window operation is working. 完成过程执行后,仅关闭窗口操作起作用。 Otherwise it will not responds the close operations. 否则,它将不会响应关闭操作。

I have already tried below mentioned codes. 我已经尝试过以下提到的代码。 But that one is not stopped working 但是那个并没有停止工作

addWindowListener(new WindowAdapter()
 {
   public void windowClosing(WindowEvent we) 
   {
    System.exit(0); 
            or
    System.exit(1);
            or
    setDefaultCloseOperation(EXIT_ON_CLOSE);     
            or
    setDefaultCloseOperation(3);
                or
        dispose();
    }

}); 
    JButton jb = new JButton("Run");        
    add(jb);         
    jb.setBounds(10, 30, 100, 30);        
    setSize(150,150);  
    setLayout(null);  
    setVisible(true); 
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent we) {
      System.exit(0);
    }
    });
    jb.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            // some operations . For Example here i'm add 1E5 data,s in my collection object. again i will replace data's places of each element.
            for(int i=0;i<1E5;i++)
            {
                ll.add(i);
            }
            for(int i=0;i<1E5;i++)
            {
                ll.add(1,i);
            }
            for(int i=0;i<1E5;i++)
            {
                ll.add(0,i);
            }
            System.out.println(ll);
        }
    });

If I am clicking close button, my window terminate the currently executed process and Close the window. 如果单击关闭按钮,则我的窗口将终止当前执行的进程并关闭该窗口。

class MyThread extends Thread {      
    public void run() {
        // some operations . For Example here i'm add 1E5 data,s in my collection object
        ..
        ..        
    }
}

Then in your actionPerformed method on your JButton actionlistener you just start the thread : 然后在JButton actionlistener上的actionPerformed方法中,仅启动线程:

public void actionPerformed(ActionEvent e) {
    new MyThread().start();
}

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

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