[英]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.