0

如果我有一组对象:

public class Party {
    LinkedList<Guy> partyList = new LinkedList<Guy>();

    public void addGuy(Guy c) {
        partyList.add(c);
    }
}

还有一个 tabbedPane:

public class CharWindow
{
private JFrame  frame;

/**
 * Launch the application.
 */
public static void main(String[] args)
{
    EventQueue.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
                CharWindow window = new CharWindow();
                window.frame.setVisible(true);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public CharWindow()
{
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize()
{
    frame = new JFrame();
    frame.setBounds(100, 100, 727, 549);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP);
    frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);

    JTabbedPane PartyScreen = new JTabbedPane(SwingConstants.TOP);
    tabbedPane.addTab("Party Screen", null, PartyScreen, null);

    JTabbedPane tabbedPane_2 = new JTabbedPane(SwingConstants.TOP);
    tabbedPane.addTab("New tab", null, tabbedPane_2, null);

    JTabbedPane tabbedPane_3 = new JTabbedPane(SwingConstants.TOP);
    tabbedPane.addTab("New tab", null, tabbedPane_3, null);
}
}

如何将内容添加到 tabbedPane“派对屏幕”,以便它为我的 LinkedList 中的每个项目显示一个 JLabel“名称”和一个垂直的 JSeparator?

4

1 回答 1

1

首先,JTabbedPane 是为每个元素面板创建一个新选项卡的小部件。它不是选项卡式界面的子级。(例如,JTabbedPane 应该包含一个名为 partyScreen 的 JPanel。)

JTabbedPane tabbedPanel = new JTabbedPane(); // holds all tabs

// this is how you add a tab:
JPanel somePanel = new JPanel();
tabbedPanel.addtab("Some Tab", somePanel);

// this is how you'd add your party screen
JPanel partyScreen = new JPanel();
tabbedPanel.addTab("Party Screen", partyScreen);

请记住,Java 命名约定有一个以小写字母开头的变量——因此 partyScreen 优于 PartyScreen。

然后遍历每个Guy对象Party并添加适当的组件。我不知道您为什么使用 aLinkedList而不是 a List,但我假设您有充分的理由未包含在上面的代码中。

// myParty is an instance of Party; I assume you have some sort of accessor to 
// the partyList
LinkedList<Guy> partyList = myParty.getPartyList();
ListIterator<Guy> it = partyList.listIterator();
while( it.hasNext() ) {
    Guy g = it.next();
    partyScreen.add(new JLabel( g.getName() ));
    partyScreen.add(new JSeparator() );
}

根据您希望它在partyScreen面板中的排列方式,您可能需要查看Layout Manager

于 2012-06-14T20:39:23.940 回答