java 面板从 JFrame 中移除后如何处理 JPanel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4463026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can JPanel be disposed after the panel has been removed from the JFrame
提问by Jaguar
I have created 2 Jpanel which will be added to a JFrame.
At first one of the JPanel is added to the JFrame.
I have used the "add()" method of JFrame to add the JPanel.
我已经创建了 2 个 Jpanel,它将被添加到一个 JFrame 中。
首先将 JPanel 中的一个添加到 JFrame。
我已经使用 JFrame 的“add()”方法来添加 JPanel。
JPanel panel = new JPanel();
JFrame j = new JFrame();
j.getContentPane().add(panel);
The JFrame has a JMenuBar set on it.
With 2 JMenuItems added to a JMenu which is finally added to the JMenuBar.
The 1st JMenuItem when clicked remove the earliar panel from the JFrame and add the other JPanel to the JFrame.
The 2nd JMenuItem does the inverse,removing the earliar JPanel and placing the newer JPanel.
JFrame 上设置了 JMenuBar。
将 2 个 JMenuItems 添加到一个最终添加到 JMenuBar 的 JMenu。
单击第一个 JMenuItem 从 JFrame 中删除早期面板并将另一个 JPanel 添加到 JFrame。第二个 JMenuItem 执行相反的操作,删除早期的 JPanel 并放置较新的 JPanel。
JMenuItem a = new JMenuItem("p1");
a.addActionListener(new...
{
Frame2 ob = new Frame2();//another class which adds components on the panel.
JPanel p1 = ob.getPanel();//method used to return the JPanel from another class
j.getContentPane().remove(0);
j.getContentPane().add(p1);
});
JMenuItem b = new JMenuItem("p2");
a.addActionListener(new...
{
Frame3 ob2 = new Frame3();//another class which adds components on the panel.
JPanel p2 = ob2.getPanel();//method used to return the JPanel from another class
j.getContentPane().remove(0);
j.getContentPane().add(p2);
});
The problem i face now is that the panels donot get disposed when removed and somewhere in the memory the panels are occupuing memory.
Although the previous panel is not visible and the newer panel can be seen, but the memory that the previous panel(not visible panel) is taking up can be seen in the task manager.
And as i switch between the panels the memory they ocuppy goes on increasing, as a new instance of the panel is being created every time.
I want to dispose the panels when removed, but there is no method to dispose a JPanel like the dispose() method for JFrame.
我现在面临的问题是面板在移除时没有得到处理,并且面板在内存中的某个地方占用了内存。
虽然上一个面板不可见,可以看到新的面板,但是在任务管理器中可以看到上一个面板(不可见面板)占用的内存。
当我在面板之间切换时,它们占用的内存不断增加,因为每次都会创建面板的新实例。我想在移除时处理面板,但是没有像 JFrame 的 dispose() 方法那样处理 JPanel 的方法。
回答by Pakka Techie
JFrame is window and JPanel is a container. The moment the JPanel instance loses its reference, it will be garbage collected.
JFrame 是窗口,JPanel 是容器。JPanel 实例丢失其引用的那一刻,它将被垃圾收集。
回答by agent-p
Looking at the code above, it looks like a step is missed. "remove()" will remove the reference from the content pane, but if the panel variable is not set to null or goes out of scope, the panel will still have a reference and it will not be garbage collected.
看上面的代码,好像漏了一步。“remove()”将从内容窗格中删除引用,但如果面板变量未设置为 null 或超出范围,面板仍将具有引用并且不会被垃圾收集。