我对JOptionPane.showInputDialog(...)方法(以及JJOptionPane的其他方法)有一种奇怪的行为。 创建的对话框似乎永远不会消亡。当对话框消失后调用Window.getWindows()时,窗口数量增加了!
测试这个程序,你明白我的意思了:
public static void main(String[] args) {
final JFrame frame = new JFrame();
final JPanel panel = new JPanel();
final JButton button = new JButton("Show Dialog");
panel.add(button);
frame.add(panel);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
JOptionPane.showInputDialog(frame, "Enter some text : ");
System.out.println(Window.getWindows().length);
}
});
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
有人可以解释发生了什么吗?
答案 0 :(得分:4)
有人明白会发生什么?
Window.getWindows()
始终返回!isDisplayable
中创建的每个Top-Level Containers(没有容器返回current JVM
),这些容器永远不会从JVM memory
返回,也不会GC'ed
,因为它们来自Native OS
,more here