簡體   English   中英

如何在操作仍在進行時關閉JFrame

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

在我的swings Windows應用程序中,單擊“運行”按鈕后,將執行某些操作。 如果在操作仍在進行時試圖關閉窗口,則關閉操作無效。 完成過程執行后,僅關閉窗口操作起作用。 否則,它將不會響應關閉操作。

我已經嘗試過以下提到的代碼。 但是那個並沒有停止工作

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);
        }
    });

如果單擊關閉按鈕,則我的窗口將終止當前執行的進程並關閉該窗口。

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

然后在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