我有一个客户对象列表。我想为List中的每个对象创建JButtons。 这是我到目前为止的代码,
public class Customer_List extends javax.swing.JFrame {
List<Customers> cus_list = new ArrayList();
public Customer_List() {
cus_list = showCustomers();
contentPane = new JPanel();
displayCustomers();
initComponents();
}
public void displayCustomers(){
JButton[] buttons = new JButton[cus_list.size()];
System.out.println(cus_list.size());
for(int i = 0; i < cus_list.size(); i++){
buttons[i] = new JButton();
buttons[i].setText(cus_list.get(i).getName());
contentPane.add(buttons[i]);
}
add(contentPane);
contentPane.validate();
}
我是一个相当新的挥杆。当我运行它时,JPanel显示但没有按钮。如果我删除initcomponents()方法,按钮会显示但没有JPanel。我究竟做错了什么? 这是initcomponent方法的内容,
private void initComponents() {
contentPane = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
contentPane.setBorder(javax.swing.BorderFactory.createTitledBorder("Customer List"));
contentPane.setMinimumSize(new java.awt.Dimension(500, 500));
jLabel1.setText("Customer List");
javax.swing.GroupLayout contentPaneLayout = new javax.swing.GroupLayout(contentPane);
contentPane.setLayout(contentPaneLayout);
contentPaneLayout.setHorizontalGroup(
contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(contentPaneLayout.createSequentialGroup()
.addGap(190, 190, 190)
.addComponent(jLabel1)
.addContainerGap(233, Short.MAX_VALUE))
);
contentPaneLayout.setVerticalGroup(
contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(contentPaneLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addContainerGap(452, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(contentPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(contentPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
`