Java Swing JTabbedPane 将 JPanel 添加到选项卡然后修改它
我想创建一个 JTabbedPane,向每个人添加一个 JPanel,然后向 JPanel 添加一些内容:
private void initTabbedPane(JTabbedPane tp)
{
System.out.println("FestplattenreinigerGraphicalUserInterface::initTabbedPane()");
// Init Tab-Names
Vector<String> tabNames = new Vector<String>();
tabNames.addElement("Startseite");
tabNames.addElement("Konfiguration");
tabNames.addElement("Hilfe");
// Init Tabs
tp = new JTabbedPane();
JPanel tmpPanel;
for(int i = 0; i < tabNames.size(); i++)
{
tmpPanel = new JPanel();
tp.addTab(tabNames.elementAt(i), tmpPanel);
}
tp.setFont(new Font("Calibri", Font.BOLD, 11));
initPanelsInTabbedPane(tp);
this.getContentPane().add(tp, BorderLayout.CENTER);
}
private void initPanelsInTabbedPane(JTabbedPane tp)
{
System.out.println("FestplattenreinigerGraphicalUserInterface::initPanelsInTabbedPane()");
tp.getComponentAt(0).add(new JButton("HELLOSTUPIDJAVAIHATEU"));
}
嗯,它说: 不兼容的类型 发现:java.awt.Component 必需:javax.swing.JPanel JPanel p = tp.getComponentAt(0);
但我的书说,使用 Component getComponentAt(int index),我可以访问它的内容,我记得 JButton 是一个组件,对吧?那么呢?
I want to create a JTabbedPane, add a JPanel to everyone and then add something to the JPanel:
private void initTabbedPane(JTabbedPane tp)
{
System.out.println("FestplattenreinigerGraphicalUserInterface::initTabbedPane()");
// Init Tab-Names
Vector<String> tabNames = new Vector<String>();
tabNames.addElement("Startseite");
tabNames.addElement("Konfiguration");
tabNames.addElement("Hilfe");
// Init Tabs
tp = new JTabbedPane();
JPanel tmpPanel;
for(int i = 0; i < tabNames.size(); i++)
{
tmpPanel = new JPanel();
tp.addTab(tabNames.elementAt(i), tmpPanel);
}
tp.setFont(new Font("Calibri", Font.BOLD, 11));
initPanelsInTabbedPane(tp);
this.getContentPane().add(tp, BorderLayout.CENTER);
}
private void initPanelsInTabbedPane(JTabbedPane tp)
{
System.out.println("FestplattenreinigerGraphicalUserInterface::initPanelsInTabbedPane()");
tp.getComponentAt(0).add(new JButton("HELLOSTUPIDJAVAIHATEU"));
}
Well it says:
incompatible types
found : java.awt.Component
required: javax.swing.JPanel
JPanel p = tp.getComponentAt(0);
But my book says that with, Component getComponentAt(int index), i can access it's content and i remember that JButton is a Component right? So wth?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您查看 Javadoc,您实际上会发现
JTabbedPane#getComponentAt(index)
返回一个Component
。但是,如果您确定它是JPanel
(访问JTabbedPane
的选项卡时或多或少是这种情况),您始终可以对其进行强制转换:或者,甚至更好如果您确实了解 Swing
,
JPanel
是JComponent
的子类,即If you take a look at Javadoc, you'll see that, indeed,
JTabbedPane#getComponentAt(index)
returns aComponent
. However, if you're sure it's aJPanel
(which is more or less the case when accessing tabs of aJTabbedPane
), you can always cast it :Or, even better if you know some things about Swing
indeed,
JPanel
is a subclass ofJComponent
, which is