java 如何从使用 BorderLayout 的 JFrame 中删除组件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1228861/
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 do I remove component from JFrame that uses BorderLayout
提问by user69514
The container uses a BorderLayout. I have a JPanel that I added to the CENTER. However the JPanel doesn't have a variable name for it.
容器使用 BorderLayout。我有一个添加到 CENTER 的 JPanel。但是 JPanel 没有它的变量名。
I could do contents.remove(nameofPanel)
我可以做 contents.remove(nameofPanel)
But since I added it like this contents.add(new CustomJPanel(), BorderLayout.CENTER);
但是因为我像这样添加它 contents.add(new CustomJPanel(), BorderLayout.CENTER);
Now I'm trying to remove the current CustomJPanel and add a new one.
现在我试图删除当前的 CustomJPanel 并添加一个新的。
How do I do this?
我该怎么做呢?
回答by Sbodd
While Carl's answer is probably the best one, a less-pleasant alternative if for some reason you can't modify the original add() call:
虽然 Carl 的答案可能是最好的答案,但如果由于某种原因您无法修改原始的 add() 调用,那么这是一种不太令人愉快的选择:
contents.remove(((BorderLayout)getLayout()).getLayoutComponent(BorderLayout.CENTER));
contents.add(someNewPanel);
Though if you think you need to do this, you may want to step back and evaluate why you're trying to do it.
但是,如果您认为需要这样做,您可能需要退后一步并评估您尝试这样做的原因。
回答by Carl Manaster
Your best way is to extract the constructor call into a named variable - probably a field, actually - and then reduce to the previous case.
最好的方法是将构造函数调用提取到一个命名变量中——实际上可能是一个字段——然后减少到前一种情况。
contents.add(new CustomJPanel(), BorderLayout.CENTER);
becomes
变成
nameOfPanel = new CustomJPanel();
contents.add(nameOfPanel, BorderLayout.CENTER);
回答by Karl
Or you can list all the elements in the container with the http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Container.html#getComponents()function, and search your Panel by an other attribute (if you can).
或者您可以使用http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Container.html#getComponents()函数列出容器中的所有元素,然后通过另一个属性(如果可以的话)。
The http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Component.html#getName()attribute is useful for this purpose, e.g. you set a name for your panel before insertion and you can use that name as a search key.
该http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Component.html#getName()属性是用于此目的,例如,你在插入之前为你的面板上设置名称和您可以使用该名称作为搜索键。
回答by Philippe Carriere
I strongly suggest you declare a global CustomJPanel variable, instantiate it with your first panel, then add the panel. When you want to remove it, you use the same object. Then you assign the new object to the variable, and add it the same way.
我强烈建议您声明一个全局 CustomJPanel 变量,用您的第一个面板实例化它,然后添加面板。当您想删除它时,您可以使用相同的对象。然后将新对象分配给变量,并以相同的方式添加它。
Anonymous object are okay when you don't need to refer to them. But you do. So you should avoid using the anonymous way.
当您不需要引用匿名对象时,匿名对象是可以的。但你做了。所以你应该避免使用匿名方式。

