[英]Can't add component to JPanel in Swing
I want to add at runtime a JLabel under the purple pane which contains already some components, say under the progress bar: 我想在运行时添加一个JLabel,在紫色窗格下面已经包含了一些组件,比如在进度条下面:
Here is the structure of the elements: 这是元素的结构:
And this is my code which is issued when an event occurs (it's getting there i checked with debug) : 这是我在事件发生时发出的代码(它是通过调试检查的):
jPanel1.add(new JLabel("Stack Overflow"));
jPanel1.revalidate();
jPanel3.revalidate();
I'm not seeing any changes whatsoever and have no clue where to go from here. 我没有看到任何变化,也不知道从哪里开始。 When i put a textarea in the purple pane and then call it's setText() method at the same place i try to add the JLabel component it works.
当我将一个textarea放在紫色窗格中然后在同一个地方调用它的setText()方法时,我尝试添加它工作的JLabel组件。
You need to learn more about layouts and how they work. 您需要了解有关布局及其工作原理的更多信息。 I strongly suggest you read the entire layout manager tutorial, since understanding layouts are the solution here, and just using BorderLayout isn't the way to solve it.
我强烈建议你阅读整个布局管理器教程,因为理解布局是这里的解决方案,而使用BorderLayout并不是解决问题的方法。 You'll likely want to nest layouts, perhaps using BorderLayout for the overall GUI, and having a central JPanel use BoxLayout to allow you to stack components on top of each other inside of it.
您可能希望嵌套布局,可能使用BorderLayout作为整个GUI,并且让中央JPanel使用BoxLayout来允许您在其中叠加组件。 Then perhaps add this JPanel to the main JPanel that uses BorderLayout in the BorderLayout.CENTER position.
然后可能将此JPanel添加到在BorderLayout.CENTER位置使用BorderLayout的主JPanel。
Just a hunch, but maybe you need to call repaint() in addition to revalidate() 只是预感,但除了revalidate()之外,你可能还需要调用repaint()
Java Swing revalidate() vs repaint() Java Swing revalidate()vs repaint()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.