Java Update JPanel组件没有删除

时间:2014-02-05 09:07:18

标签: java jpanel

我有一个复杂的JPanel,它有各种嵌套的JPanel作为组件。

我想用新的嵌套面板替换其中一个嵌套面板。但我无法让顶级面板重新绘制新组件,除非我删除并再次添加它们:

JPanel topLevel = new JPanel();
JPanel upperPane = new JPanel();
JPanel lowerPane = new JPanel();

JPanel infoPane = new JPanel();

upperPanel.add(infoPane);
topLevel.add(UpperPane);
topLevel.add(lowerPane);

//以上(伪代码)很好地显示。 //现在我想:

infoPane = new JPanel(); //Changed this Panel somehow;

topLevel.revalidate();
topLevel.repaint();

//以上两行不显示新信息。

如何更新?

2 个答案:

答案 0 :(得分:0)

如果您正在呼叫new JPanel(),那么您正在引用内存中的不同点。因此,对JPanel上的旧upperPanel的引用将丢失,之后对infoPane所做的更改将不会显示。

我自己没有尝试过,但是在不调用new的情况下更改infoPane,这可能是您的答案。

答案 1 :(得分:0)

FWIW,你描述的设计范例对我来说似乎很尴尬。继续使用原始的infoPane实例并简单地重绘其内容会不会更直接?然后您的topLevel面板不必参与更改。

所以,当你调用new()时,让你的infoPane显示你的库给你的东西,并使用new()在你的infoPane中放置新的WhateverItIs()。

这会在你的背景下发挥作用吗?

希望这有帮助。

相关问题