Java Swing JTabbedPane 将 JPanel 添加到选项卡然后修改它

发布于 2024-10-04 08:05:45 字数 1192 浏览 0 评论 0原文

我想创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春夜浅 2024-10-11 08:05:45

如果您查看 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 !"));

JPanelJComponent 的子类,即

If you take a look at Javadoc, you'll see that, indeed, JTabbedPane#getComponentAt(index) returns a Component. However, if you're sure it's a JPanel (which is more or less the case when accessing tabs of a JTabbedPane), you can always cast it :

((JPanel) tp.getComponentAt(0)).add(new JButton("come on, Java is nice enough, no ?"));

Or, even better if you know some things about Swing

((JCompoonent) tp.getComponentAt(0)).add(new JButton("No, Java and Swing positively rock hard awesome !"));

indeed, JPanel is a subclass of JComponent, which is

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文