简体   繁体   English

动态地将标签添加到JTabbedPane

[英]Dynamically adding tabs to JTabbedPane

In my main I loop over each League as below: 在我的主要任务中,我如下循环浏览每个League

for (League l : t.getLeagues()) {
    LeaguePanel leaguePanel = new LeaguePanel(l);
    roundTabs.addTab(l.getName(), leaguePanel);
}

This should then create a JPanel and add it to the tab. 然后,应该创建一个JPanel并将其添加到选项卡中。

public class LeaguePanel extends JPanel {

    private League league;
    private JComboBox roundComboBox;

    LeaguePanel(League l) {
        league = l;
        JPanel leagePanel = new JPanel();
        leagePanel.add(new JLabel("Tournament Information"));
    }

However, the tab gets created but nothing appears within it 但是,选项卡已创建,但其中未显示任何内容

Any ideas why? 有什么想法吗?

You want to add things to your LeaguePanel object, not to a new JPanel : 您想将内容添加到LeaguePanel对象中,而不是添加到新的JPanel

JPanel leagePanel = new JPanel();
leagePanel.add(new JLabel("Tournament Information"));

becomes 变成

this.add(new JLabel("Tournament Information"));

Because it's your LeaguePanel that you add to the tabbed pane . 因为是您添加到选项卡式窗格中的LeaguePanel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM