[英]How to create rounded and complex illustrated elements in Java Swing Gui?
[英]Create Swing GUI Elements for each item in a Collection - 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);
}
}
如何將內容添加到tabbedPane“方屏幕”,以便為我的LinkedList中的每個項目顯示JLabel“名稱”和垂直JSeparator?
首先,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。
然后遍歷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
面板中的partyScreen
,您可能需要查看Layout Manager 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.