谈谈JInternalFrame的对象释放问题,很有意思的,不信,那就看看
代码如下:
import javax.swing.*;
public class Untitled1 extends JInternalFrame{
public JPanel panel = null;
JTextField textField = new JTextField();
JTextField textField2 = new JTextField();
JTextField textField3 = new JTextField();
public JPanel panel2 = new JPanel();
public Untitled1(JPanel b) {
super();
panel = b;
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void jbInit() {
panel.add(panel2,null);
panel2.add(textField,null);
panel2.add(textField2,null);
panel2.add(textField3,null);
this.getContentPane().add(panel);
}
public void dispose() {
panel2.removeAll();
panel.remove(panel2);
this.getContentPane().remove(panel);
textField = null;
textField2 = null;
textField3 = null;
panel2 = null;
panel = null;
}
public static void main(String[] args) {
Untitled1 a = new Untitled1(new JPanel());
Untitled1 b = new Untitled1(new JPanel());
Untitled1 c = new Untitled1(new JPanel());
java.lang.ref.WeakReference ref1 = new java.lang.ref.WeakReference(a);
java.lang.ref.WeakReference ref2 = new java.lang.ref.WeakReference(b);
java.lang.ref.WeakReference ref3 = new java.lang.ref.WeakReference(c);
a.dispose();
b.dispose();
c.dispose();
a = null;
b = null;
c = null;
System.gc();
try {
Thread.sleep(2000);
}catch(Exception ex) {
}
if (ref1.get()!=null)
System.out.println("ref1 is not null!");
if (ref2.get()!=null)
System.out.println("ref2 is not null!");
if (ref3.get()!=null)
System.out.println("ref3 is not null!");
}
}
按照常理说应该没有语句打印出来,因为对象都已经显示的设成null,没有对象是可达的,可是却打印出“ref1 is not null!”证明还有JInternalFrame的一个实例没有回收,我一直想不明白,请各位大虾帮助!3q