我想创建一个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是一个组件吗?那么吗?
答案 0 :(得分:1)
如果你看看Javadoc,你会发现JTabbedPane#getComponentAt(index)
确实会返回Component
。但是,如果您确定它是JPanel
(访问JTabbedPane
的标签时或多或少),您可以随时投射它:
((JPanel) tp.getComponentAt(0)).add(new JButton("come on, Java is nice enough, no ?"));
或者,如果你对Swing有所了解,那就更好了
((JCompoonent) tp.getComponentAt(0)).add(new JButton("No, Java and Swing positively rock hard awesome !"));
的确,JPanel
是JComponent
的子类,即