2011-06-10 48 views
0

正如我試過的,看起來像那樣是不可能的,沒有成功,因爲我試過了,或者以另一種方式存在?真的不可能在運行時刪除任何JDialog或JWindow

import java.awt.*; 
import java.awt.event.WindowEvent; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.*; 

public class SuperConstructor extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private int i = 1; 
    private boolean runProcess; 
    private int top = 20; 
    private int left = 20; 

    public SuperConstructor() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setPreferredSize(new Dimension(300, 300)); 
     setTitle("Super constructor"); 
     setLocation(150, 150); 
     pack(); 
     setVisible(true); 
     Point loc = this.getLocation(); 
     top += loc.x; 
     left += loc.y; 
     runProcess = true; 
     Thread th = new Thread(new AddTask()); 
     th.setDaemon(false); 
     th.setPriority(Thread.MIN_PRIORITY); 
     th.start(); 
    } 

    private class AddTask implements Runnable { 

     @Override 
     public void run() { 
      while (runProcess) { 
       for (int j = 0; j < 6; j++) { 
        if (j % 2 == 0) { 
         EventQueue.invokeLater(new Runnable() { 

          @Override 
          public void run() { 
           SecondDialog secondDialog = new SecondDialog(); 
          } 
         }); 
        } else { 
         EventQueue.invokeLater(new Runnable() { 

          @Override 
          public void run() { 
           FirstDialog firstDialog = new FirstDialog(); 
          } 
         }); 
        } 
        Window[] wins = Window.getWindows(); 
        for (int i = 0; i < wins.length; i++) { 
         if (wins[i] instanceof JFrame) { 
          System.out.print("JFrame"); 
         } else if (wins[i] instanceof JDialog) { 
          System.out.print(", JDialog"); 
         } else if (wins[i] instanceof JWindow) { 
          System.out.print(", JWindow"); 
         } 
        } 
        System.out.println(" "); 
        try { 
         Thread.sleep(1000); 
        } catch (InterruptedException ex) { 
         Logger.getLogger(SuperConstructor.class.getName()).log(Level.SEVERE, null, ex); 
        } 
       } 
       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException ex) { 
        Logger.getLogger(SuperConstructor.class.getName()).log(Level.SEVERE, null, ex); 
       } 
       runProcess = false; 
      } 
      remWins(); 
     } 
    } 

    public void remWins() { 
     runProcess = true; 
     Thread th = new Thread(new RemTask()); 
     th.setDaemon(false); 
     th.setPriority(Thread.MIN_PRIORITY); 
     th.start(); 
    } 

    private class RemTask implements Runnable { 

     @Override 
     public void run() { 
      while (runProcess) { 
       Window[] wins = Window.getWindows(); 
       for (int i = 0; i < wins.length; i++) { 
        if (wins[i] instanceof JFrame) { 
         System.out.print("JFrame"); 
        } else if (wins[i] instanceof JDialog) { 
         System.out.print(", Remove JDialog"); 
         wins[i].setVisible(false); 
         wins[i].dispose(); 
         WindowEvent windowClosing = new WindowEvent(wins[i], WindowEvent.WINDOW_CLOSING); 
         wins[i].dispatchEvent(windowClosing); 
         Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowClosing); 
         Runtime runtime = Runtime.getRuntime(); 
         runtime.gc(); 
         runtime.runFinalization(); 
        } else if (wins[i] instanceof JWindow) { 
         System.out.print(", Remove JWindow"); 
         wins[i].setVisible(false); 
         wins[i].dispose(); 
         WindowEvent windowClosing = new WindowEvent(wins[i], WindowEvent.WINDOW_CLOSING); 
         wins[i].dispatchEvent(windowClosing); 
         Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowClosing); 
         Runtime runtime = Runtime.getRuntime(); 
         runtime.gc(); 
         runtime.runFinalization(); 
        } 
        try { 
         Thread.sleep(1000); 
        } catch (InterruptedException ex) { 
         Logger.getLogger(SuperConstructor.class.getName()).log(Level.SEVERE, null, ex); 
        } 
       } 
       System.out.println(" Remove Done "); 
       Runtime.getRuntime().runFinalization(); 
       Runtime.getRuntime().gc(); 
       System.out.println(" Checking if still exists any of TopLayoutContainers "); 
       Window[] wins1 = Window.getWindows(); 
       for (int i = 0; i < wins1.length; i++) { 
        if (wins1[i] instanceof JFrame) { 
         System.out.print("JFrame"); 
         wins1[i].setVisible(true); 
        } else if (wins1[i] instanceof JDialog) { 
         System.out.print(", JDialog"); 
         wins1[i].setVisible(true); 
        } else if (wins1[i] instanceof JWindow) { 
         System.out.print(", JWindow"); 
         wins1[i].setVisible(true); 
        } 
       } 
       System.out.println(" "); 
       System.out.println(" No Idea, That's it "); 
       runProcess = false; 
      } 
     } 
    } 

    private class FirstDialog extends JWindow { 

     private static final long serialVersionUID = 1L; 

     FirstDialog(final Frame parent) { 
      super(parent); 
      Point loc = parent.getLocation(); 
      setLocation(top, left); 
      top += 20; 
      left += 20; 
      setPreferredSize(new Dimension(200, 200)); 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      pack(); 
      setVisible(true); 
     } 

     private FirstDialog() { 
      setLocation(top, left); 
      top += 20; 
      left += 20; 
      setPreferredSize(new Dimension(200, 200)); 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      pack(); 
      setVisible(true); 
     } 
    } 

    private class SecondDialog extends JDialog { 

     private static final long serialVersionUID = 1L; 

     SecondDialog(final Frame parent) { 
      super(parent, "SecondDialog " + (i++)); 
      setLocation(top, left); 
      top += 20; 
      left += 20; 
      setPreferredSize(new Dimension(200, 200)); 
      setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
      setModalityType(Dialog.ModalityType.MODELESS); 
      pack(); 
      setVisible(true); 
     } 

     private SecondDialog() { 
      setTitle("SecondDialog " + (i++)); 
      setLocation(top, left); 
      top += 20; 
      left += 20; 
      setPreferredSize(new Dimension(200, 200)); 
      setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
      setModalityType(Dialog.ModalityType.MODELESS); 
      pack(); 
      setVisible(true); 
     } 
    } 

    public static void main(String args[]) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       SuperConstructor superConstructor = new SuperConstructor(); 
      } 
     }); 
    } 
} 
+0

@mKorbel,你是什麼意思的「刪除」?我看到你正在嘗試處理頂層組件,但是你仍然在數組中引用它們...... – mre 2011-06-10 10:46:28

+0

@mre你能更具體嗎1)你的意思是重置數組爲null,2)檢查因爲存在頂層組件在單獨的void/class 3)另一個WooDoo – mKorbel 2011-06-10 10:59:32

+0

@mKorbel,我認爲持有對數組中這些對象的引用使它們不符合垃圾回收的條件。另外,您應該仔細閱讀[dispose]的規範(http://download.oracle.com/javase/6/docs/api/java/awt/Window.html#dispose%28%29)。也許它沒有做你期望的事情。 – mre 2011-06-10 11:22:00

回答

2

最起碼,你應該RemTask上EDT運行,(該任務使用的invokeLater以及)而不是一些任意線程。我非常懷疑你應該在美國東部時區以外撥打Windows.getWindows()

+0

沒有什麼改變,謝謝你的好主意 – mKorbel 2011-06-10 13:17:15

+0

只爲reall發表的主題+1 – mKorbel 2011-06-10 16:07:21

相關問題