2012-06-14 117 views
0

如果我有對象的集合:創建集合中的每個項目的Swing GUI元素 - JAVA

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); 
} 
} 

如何將內容添加到選項卡窗格「派對屏幕「,這樣它會顯示我的LinkedList中的每個項目的JLabel」Name「和垂直JSeparator?

+0

什麼是'PartyScreen'?它在哪裏定義?另外,[Character](http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Character.html)是原始'char'的包裝類 - 這就是你意? –

+0

只是修復了名稱衝突,而'PartyScreen'是一個tabbedPane – GrumpyTechDude

+0

D'oh。我需要學習更仔細地閱讀。抱歉。 –

回答

1

首先,JTabbedPane是爲每個元素面板創建一個新選項卡的小部件。它是而不是一個選項卡式界面的子項。 (如:JTabbedPane的應持有的JPanel稱爲partyScreen。)

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。

然後遍歷Party中的每個Guy對象並添加相應的組件。我不知道你爲什麼使用LinkedList而不是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

+0

這工作得很好,謝謝! – GrumpyTechDude

相關問題