Java 如何处置 JPanel - jPanel1.dispose() 或等效物

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21365570/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:19:09  来源:igfitidea点击:

How to Dispose a JPanel - jPanel1.dispose() Or equivalent

javaswinguser-interface

提问by Yousuf Soomro

How can we dispose() a JPanel?

我们如何处理()一个 JPanel?

I've done jPanel1.removeAll()and now I want JPanel to be disposed so that jPanelParent.getComponents()can't return it

我已经完成了jPanel1.removeAll(),现在我想要处理 JPanel,这样jPanelParent.getComponents()就无法返回它

Component[] components = this.jPanelParent.getComponents();  /// Returns 5 Components      
JPanel last = (JPanel)components[components.length];
last.removeAll();
//last.dispose()   NOT AVAILABLE

Now after above code if I run .getComponents() I don't want to get "last" in above code

现在在上面的代码之后,如果我运行 .getComponents() 我不想在上面的代码中得到“last”

采纳答案by arcy

This is a classic case of looking in the wrong place for an answer; your question presupposes a way to do things, and you ask how to do that presupposed and erroneous operation, and confusion ensues. It doesn't help that "dispose" is a term used for an operation you don't need at all, having to do with making memory available for reclaiming in an environment where programmers do these things.

这是一个在错误的地方寻找答案的经典案例;你的问题预设了一种做事的方式,你问如何去做那个预设的错误操作,然后就会出现混乱。“处置”是一个用于您根本不需要的操作的术语,这与在程序员执行这些操作的环境中使内存可用于回收有关,这无济于事。

I believewhat you want to do is remove the reference that the parent component has to the JPanel. That would fit your desire that getComponents() in the parent panel not return the panel you are getting rid of.

相信您想要做的是删除父组件对 JPanel 的引用。这将符合您的愿望,即父面板中的 getComponents() 不返回您正在摆脱的面板。

In order for the panel that you are removing to do this, it needs a reference to the parent panel, and then you can remove it from the parent panel just like you've removed components from the target panel. In fact, if the parent panel is the only place that references the target, and the target's components are only referenced in the target, you don't have to remove the target components if you just remove the parent panel reference to the target.

为了让您要删除的面板执行此操作,它需要对父面板的引用,然后您可以将其从父面板中删除,就像从目标面板中删除组件一样。实际上,如果父面板是唯一引用目标的地方,并且目标的组件只在目标中被引用,那么如果只是移除父面板对目标的引用,则不必移除目标组件。

So pass a reference to your parent panel to your target panel, and do a remove() of the target panel from the parent panel in place of your hypothesized dispose() call.

因此,将对您的父面板的引用传递到您的目标面板,并从父面板执行目标面板的 remove() 代替假设的 dispose() 调用。

回答by Douglas Tybel

Dispose em JPanel Netbeans java

处理 em JPanel Netbeans java

   Component comp = SwingUtilities.getRoot(this);
   ((Window) comp).dispose();

回答by gdbj

In my case, my one panel was on a JDialog, and the following can work:

就我而言,我的一个面板在 JDialog 上,以下可以工作:

public void dispose() {
    JFrame parent = (JFrame) this.getTopLevelAncestor();
    parent.dispose();
}

However, I think explicitly keeping a reference to the parent in the panel might be a less error prone way, as described in another answer.

但是,我认为在面板中明确保留对父级的引用可能是一种不太容易出错的方式,如另一个答案中所述。