我的一个类返回一个 JPanel,它由其他类添加到 JFrame 上。
JPanel 包含一个 JTree 和一些按钮。在某些事件中,面板会再次创建并返回到 JFrame。
我的问题是,我必须再次将 JPanel 添加到 JFrame 的容器中,然后调整框架的大小以使更改可见。我不知道如何在不调整大小的情况下更新框架。我尝试删除旧对象并添加更新的对象,但仍然不起作用。
处理这个问题的最佳方法是什么?理想情况下,我会引用 JPanel,并且当 JPanel 更改时,框架也会更新。
One of my classes is returning a JPanel which is added on a JFrame by some other class.
The JPanel contains a JTree and some buttons. On some events the panel is created again and returned to the JFrame.
My problem is that I have to add the JPanel to the Container of the JFrame again and then resize the frame for changes to be visible. I can't figure how to have the frame update without resizing.I tried removing old objects and adding updated ones but still doesn't work.
What is the best way to deal with this issue? Ideally I would have a reference to the JPanel and when the JPanel is changed, the frame will also be updated.
发布评论
评论(2)
那么您的代码应该类似于:
也就是说您需要将新的 JTree 添加到 GUI,您不能只更改对树变量。
或者更简单,您不需要创建新的 JTree,只需使用以下命令替换现有树中的模型:
如果这仍然没有帮助,那么您需要发布您的 SSCCE 演示了问题,因为您的问题仍然不清楚。
Then your code should be something like:
That is you need to add the new JTree to the GUI, you can't just change the reference to the tree variable.
Or even easier, you don't need to create a new JTree, just replace the model in the existing tree using:
If this still doesn't help then you need to post your SSCCE that demonstrates the problem because your question still isn't clear.
首先尝试
JFrame.invalidate()
,然后调用JFrame.validate()
http://docs.oracle.com/javase/6/docs/api/java/awt/Container.html#invalidate% 28%29
try
JFrame.invalidate()
first, then callJFrame.validate()
http://docs.oracle.com/javase/6/docs/api/java/awt/Container.html#invalidate%28%29