[英]JPanel inside a JPanel inside a JTabbedPane doesn't show up
I have a JPanel (firstPanel) which is embedded into an other JPanel (secondPanel). 我有一个嵌入到另一个JPanel(secondPanel)中的JPanel(firstPanel)。 I want to create a JTabbedPane with two tabs: one for the firstPanel and one for the secondPanel.
我想创建一个带有两个选项卡的JTabbedPane:一个用于firstPanel,一个用于secondPanel。 However, when I switch to secondPanel, the embedded firstPanel won't show up.
但是,当我切换到secondPanel时,嵌入式的firstPanel将不会显示。
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class Application extends JFrame
{
public Application()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setMinimumSize(new Dimension(300, 300));
JPanel firstPanel = new JPanel();
firstPanel.add(new JLabel("first"));
JPanel secondPanel = new JPanel();
// JPanel thirdPanel = new JPanel();
// thirdPanel.add(new JLabel("third"));
//
// secondPanel.add(thirdPanel);
secondPanel.add(firstPanel);
secondPanel.add(new JLabel("second"));
JTabbedPane mainPanel = new JTabbedPane();
mainPanel.addTab("First", firstPanel);
mainPanel.addTab("Second", secondPanel);
this.add(mainPanel);
}
public static void main(String[] args)
{
Application app = new Application();
app.pack();
app.setVisible(true);
}
}
When I add the thirdPanel instead of the first, it works as expected. 当我添加thirdPanel而不是第一个时,它可以按预期工作。 Am I missing something obvious?
我是否缺少明显的东西? Thanks you!
谢谢!
This is because you are trying to add a single panel to two different parents, one being your secondPanel
, and the other being your tab. 这是因为您试图将一个面板添加到两个不同的父对象,一个是您的
secondPanel
,另一个是您的标签。 You can't do that. 你不能那样做。
Can I ask why you think you want to do this? 请问您为什么认为自己想这样做? There is probably a better solution out there for you.
可能有一个更好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.