从 Array[] 填充 JList
好吧,伙计们,对即将发生的文字墙感到抱歉,但我有一个非常烦人的问题,我似乎无法解决。我目前正在尝试从 String[] 填充 JList,但流血的东西拒绝填充。我的程序愉快地写入我的源文件,并愉快地从中读取(它向我抛出 0 个错误),但是当我实际尝试用包含的数据填充我的 JList 时......好吧,什么也没有发生,它只是拒绝做任何事。您能看一下代码,并尝试帮我找出问题吗?我真的不知道为什么这个极其简单的任务给我带来了这么多问题。
package loyaltyguis;
import javax.swing.DefaultListModel;
import loyaltyscheme.CustomerList;
import loyaltyscheme.Customer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JOptionPane;
import loyaltyguis.MainForm;
/**
*
* @author Liam
*/
public class MainForm extends javax.swing.JFrame {
public static CustomerList cust;
private DefaultListModel lModel;
private File filename;
private FileInputStream inputFile;
private ObjectInputStream objectInput;
/** Creates new form MainForm */
public MainForm() {
filename = new File("\\customer_list.dat");
readDataFromFile();
lModel = new DefaultListModel();
cust = new CustomerList();
refreshList();
initComponents();
}
private void readDataFromFile() {
try {
inputFile = new FileInputStream(filename);
objectInput = new ObjectInputStream(inputFile);
cust = (CustomerList)objectInput.readObject();
objectInput.close();
}
catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage(),
"I/O Error", JOptionPane.ERROR_MESSAGE);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void refreshList()
{
String [] customers = cust.getCustomerNames();
lModel.clear();
for(String m: customers)
{
lModel.addElement(m);
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
tbFirstName = new javax.swing.JTextField();
tbLastName = new javax.swing.JTextField();
tbEmail = new javax.swing.JTextField();
tbNumber = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
btnEdit = new javax.swing.JButton();
btnSave = new javax.swing.JButton();
btnDelete = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
lstDisplay = new javax.swing.JList();
jPanel2 = new javax.swing.JPanel();
tbCurrPoints = new javax.swing.JTextField();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
btnAddPoints = new javax.swing.JButton();
btnRedeemPoints = new javax.swing.JButton();
rdSales1 = new javax.swing.JRadioButton();
rdSales2 = new javax.swing.JRadioButton();
jPanel3 = new javax.swing.JPanel();
btnAddNewCus = new javax.swing.JButton();
btnExit = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel1.setText("Customer Loyalty Points");
jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
tbFirstName.setText(" ");
tbFirstName.setEnabled(false);
tbLastName.setText(" ");
tbLastName.setEnabled(false);
tbEmail.setText(" ");
tbEmail.setEnabled(false);
tbNumber.setText(" ");
tbNumber.setEnabled(false);
jLabel2.setText("First Name(s)");
jLabel3.setText("Last Name");
jLabel4.setText("Email");
jLabel5.setText("Telephobe Number");
btnEdit.setText("Edit");
btnEdit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEditActionPerformed(evt);
}
});
btnSave.setText("Save");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
btnDelete.setText("Delete");
lstDisplay.setModel(lModel);
jScrollPane1.setViewportView(lstDisplay);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
.addComponent(btnDelete, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(btnEdit, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(tbNumber, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tbEmail, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tbLastName, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tbFirstName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel4)
.addComponent(jLabel3)
.addComponent(jLabel2)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnEdit)
.addComponent(btnSave))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnDelete)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
tbCurrPoints.setText(" ");
jLabel6.setText("Current Points");
jLabel7.setText("Points to Add / Redeem");
jTextField1.setText(" ");
btnAddPoints.setText("Add Points");
btnRedeemPoints.setText("Redeem Points");
buttonGroup1.add(rdSales1);
rdSales1.setText("Sales Cat 1");
buttonGroup1.add(rdSales2);
rdSales2.setText("Sales Cat 2");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnAddPoints, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
.addComponent(jLabel6)
.addComponent(tbCurrPoints, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
.addComponent(jLabel7)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
.addComponent(btnRedeemPoints, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(rdSales1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
.addComponent(rdSales2)))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tbCurrPoints, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(3, 3, 3)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(rdSales1)
.addComponent(rdSales2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnAddPoints)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnRedeemPoints)
.addContainerGap(18, Short.MAX_VALUE))
);
jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
btnAddNewCus.setText("Add New Customer");
btnAddNewCus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddNewCusActionPerformed(evt);
}
});
btnExit.setText("Exit Program");
btnExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnExitActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btnExit, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
.addComponent(btnAddNewCus, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE))
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(btnAddNewCus, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, 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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jLabel1))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(98, 98, 98))
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
pack();
}// </editor-fold>
private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
tbFirstName.enable(true);
tbLastName.enable(true);
tbEmail.enable(true);
tbNumber.enable(true);
}
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
tbFirstName.enable(false);
tbLastName.enable(false);
tbEmail.enable(false);
tbNumber.enable(false);
}
private void btnAddNewCusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAddNewCus;
private javax.swing.JButton btnAddPoints;
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnEdit;
private javax.swing.JButton btnExit;
private javax.swing.JButton btnRedeemPoints;
private javax.swing.JButton btnSave;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
private javax.swing.JList lstDisplay;
private javax.swing.JRadioButton rdSales1;
private javax.swing.JRadioButton rdSales2;
private javax.swing.JTextField tbCurrPoints;
private javax.swing.JTextField tbEmail;
private javax.swing.JTextField tbFirstName;
private javax.swing.JTextField tbLastName;
private javax.swing.JTextField tbNumber;
// End of variables declaration
}
这也是我用于填充数组的类( CustomerList ):
package loyaltyscheme;
import java.util.ArrayList;
/**
* CustomerList - holds details of all the customers enrolled in the loyalty
* scheme. The current implementation is based on a simple ArrayList.
* @author Liam
*/
public class CustomerList {
private ArrayList<Customer> list;
/**
* An integer indicating that a deletion was successful.
*/
public static final int CUSTOMERDELETED = 0;
/**
* An integer indicating that an attempt to delete a customer was
* unsuccessful because the index was out of range of the collection.
*/
public static final int INVALIDINDEX = 1;
/**
* An integer indicating that a customer could not be deleted because
* the customer still has unused points.
*/
public static final int POINTSNOTZERO = 2;
/**
* Constructor - creates a new empty collection of customers,
* currently uses an ArrayList.
*/
public CustomerList() {
list = new ArrayList<Customer>();
}
/**
* Retrieves the full name of each customer.
* @return - a String array containing the full name of each customer,
* in the order in which they are stored in the collection.
*/
public String[] getCustomerNames() {
String names[] = new String[list.size()];
int pos = 0;
for (Customer customer : list) {
names[pos] = customer.getFullName();
pos ++;
}
return names;
}
/**
* Retrieves a customer from a particular location in the list. The index
* gives the location required (numbered starting from zero).
* @param index - the location of the customer required, in the range
* 0 to (list size - 1).
* @return - the Customer object at the given position in the collection.
* If the index passed in by the parameter is outside the range of the
* collection, the value 'null' will be returned.
*/
public Customer getCustomerAt(int index) {
if (index < 0 || index >= list.size())
return null;
else
return list.get(index);
}
/**
* Adds a customer to the end of the collection.
* @param customer - the new Customer object to be added.
*/
public void addCustomer(Customer customer) {
list.add(customer);
}
/**
* Removes the customer at the location indicated by the parameter.
* @param index - the location of the customer to be removed.
* @return - an integer indicating
*/
public int removeCustomerAt(int index) {
if (index < 0 || index >= list.size())
return INVALIDINDEX;
else if (list.get(index).getCurrentPoints() != 0)
return POINTSNOTZERO;
else {
list.remove(index);
return CUSTOMERDELETED;
}
}
/**
* Retrieves the grand total of unredeemed points held by all customers
* in the scheme.
* @return - the total number of points.
*/
public int getTotalPoints() {
int points = 0;
for (Customer customer : list)
points += customer.getCurrentPoints();
return points;
}
/**
* Finds out the size of the collection.
* @return - the number of customers in the list.
*/
public int size() {
return list.size();
}
/**
* Returns details of the current state of the collection.
* @return - a string containing the total size of the list and the
* total number of points held by all the customers.
*/
@Override
public String toString() {
return "CustomerList{has " + size() + " customers and "
+ getTotalPoints() + " points"
+ '}';
}
}
抱歉,如果我错过了如此简单的可笑的东西,但这让我发疯,我只是无法发现问题,因为它不是'根本不会抛出任何错误,它只是根本没有这样做。
Ok guys, sorry for the wall of text that is about to ensue, but I have a really annoying problem that I just can't seem to work out. I'm currently trying to populate a JList from a String[], but the bleeding thing refuses to populate. I have my program happily writing TO my source file, and happily reading from it ( it is throwing 0 errors at me ), but when I actually try to populate my JList with the contained data...well, nothing happens, it just refuses to do anything. Could you have a look at the code, and try to identify the problem for me? I've genuinely no idea why this incredibly simple task is giving me so many problems.
package loyaltyguis;
import javax.swing.DefaultListModel;
import loyaltyscheme.CustomerList;
import loyaltyscheme.Customer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JOptionPane;
import loyaltyguis.MainForm;
/**
*
* @author Liam
*/
public class MainForm extends javax.swing.JFrame {
public static CustomerList cust;
private DefaultListModel lModel;
private File filename;
private FileInputStream inputFile;
private ObjectInputStream objectInput;
/** Creates new form MainForm */
public MainForm() {
filename = new File("\\customer_list.dat");
readDataFromFile();
lModel = new DefaultListModel();
cust = new CustomerList();
refreshList();
initComponents();
}
private void readDataFromFile() {
try {
inputFile = new FileInputStream(filename);
objectInput = new ObjectInputStream(inputFile);
cust = (CustomerList)objectInput.readObject();
objectInput.close();
}
catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage(),
"I/O Error", JOptionPane.ERROR_MESSAGE);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void refreshList()
{
String [] customers = cust.getCustomerNames();
lModel.clear();
for(String m: customers)
{
lModel.addElement(m);
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
tbFirstName = new javax.swing.JTextField();
tbLastName = new javax.swing.JTextField();
tbEmail = new javax.swing.JTextField();
tbNumber = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
btnEdit = new javax.swing.JButton();
btnSave = new javax.swing.JButton();
btnDelete = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
lstDisplay = new javax.swing.JList();
jPanel2 = new javax.swing.JPanel();
tbCurrPoints = new javax.swing.JTextField();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
btnAddPoints = new javax.swing.JButton();
btnRedeemPoints = new javax.swing.JButton();
rdSales1 = new javax.swing.JRadioButton();
rdSales2 = new javax.swing.JRadioButton();
jPanel3 = new javax.swing.JPanel();
btnAddNewCus = new javax.swing.JButton();
btnExit = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel1.setText("Customer Loyalty Points");
jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
tbFirstName.setText(" ");
tbFirstName.setEnabled(false);
tbLastName.setText(" ");
tbLastName.setEnabled(false);
tbEmail.setText(" ");
tbEmail.setEnabled(false);
tbNumber.setText(" ");
tbNumber.setEnabled(false);
jLabel2.setText("First Name(s)");
jLabel3.setText("Last Name");
jLabel4.setText("Email");
jLabel5.setText("Telephobe Number");
btnEdit.setText("Edit");
btnEdit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEditActionPerformed(evt);
}
});
btnSave.setText("Save");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
btnDelete.setText("Delete");
lstDisplay.setModel(lModel);
jScrollPane1.setViewportView(lstDisplay);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
.addComponent(btnDelete, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(btnEdit, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(tbNumber, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tbEmail, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tbLastName, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tbFirstName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel4)
.addComponent(jLabel3)
.addComponent(jLabel2)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnEdit)
.addComponent(btnSave))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnDelete)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
tbCurrPoints.setText(" ");
jLabel6.setText("Current Points");
jLabel7.setText("Points to Add / Redeem");
jTextField1.setText(" ");
btnAddPoints.setText("Add Points");
btnRedeemPoints.setText("Redeem Points");
buttonGroup1.add(rdSales1);
rdSales1.setText("Sales Cat 1");
buttonGroup1.add(rdSales2);
rdSales2.setText("Sales Cat 2");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnAddPoints, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
.addComponent(jLabel6)
.addComponent(tbCurrPoints, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
.addComponent(jLabel7)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
.addComponent(btnRedeemPoints, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(rdSales1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
.addComponent(rdSales2)))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tbCurrPoints, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(3, 3, 3)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(rdSales1)
.addComponent(rdSales2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnAddPoints)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnRedeemPoints)
.addContainerGap(18, Short.MAX_VALUE))
);
jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
btnAddNewCus.setText("Add New Customer");
btnAddNewCus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddNewCusActionPerformed(evt);
}
});
btnExit.setText("Exit Program");
btnExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnExitActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btnExit, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
.addComponent(btnAddNewCus, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE))
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(btnAddNewCus, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, 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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jLabel1))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(98, 98, 98))
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
pack();
}// </editor-fold>
private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
tbFirstName.enable(true);
tbLastName.enable(true);
tbEmail.enable(true);
tbNumber.enable(true);
}
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
tbFirstName.enable(false);
tbLastName.enable(false);
tbEmail.enable(false);
tbNumber.enable(false);
}
private void btnAddNewCusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAddNewCus;
private javax.swing.JButton btnAddPoints;
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnEdit;
private javax.swing.JButton btnExit;
private javax.swing.JButton btnRedeemPoints;
private javax.swing.JButton btnSave;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
private javax.swing.JList lstDisplay;
private javax.swing.JRadioButton rdSales1;
private javax.swing.JRadioButton rdSales2;
private javax.swing.JTextField tbCurrPoints;
private javax.swing.JTextField tbEmail;
private javax.swing.JTextField tbFirstName;
private javax.swing.JTextField tbLastName;
private javax.swing.JTextField tbNumber;
// End of variables declaration
}
Here is also the Class ( CustomerList ) which I am using for the array from which I am populating:
package loyaltyscheme;
import java.util.ArrayList;
/**
* CustomerList - holds details of all the customers enrolled in the loyalty
* scheme. The current implementation is based on a simple ArrayList.
* @author Liam
*/
public class CustomerList {
private ArrayList<Customer> list;
/**
* An integer indicating that a deletion was successful.
*/
public static final int CUSTOMERDELETED = 0;
/**
* An integer indicating that an attempt to delete a customer was
* unsuccessful because the index was out of range of the collection.
*/
public static final int INVALIDINDEX = 1;
/**
* An integer indicating that a customer could not be deleted because
* the customer still has unused points.
*/
public static final int POINTSNOTZERO = 2;
/**
* Constructor - creates a new empty collection of customers,
* currently uses an ArrayList.
*/
public CustomerList() {
list = new ArrayList<Customer>();
}
/**
* Retrieves the full name of each customer.
* @return - a String array containing the full name of each customer,
* in the order in which they are stored in the collection.
*/
public String[] getCustomerNames() {
String names[] = new String[list.size()];
int pos = 0;
for (Customer customer : list) {
names[pos] = customer.getFullName();
pos ++;
}
return names;
}
/**
* Retrieves a customer from a particular location in the list. The index
* gives the location required (numbered starting from zero).
* @param index - the location of the customer required, in the range
* 0 to (list size - 1).
* @return - the Customer object at the given position in the collection.
* If the index passed in by the parameter is outside the range of the
* collection, the value 'null' will be returned.
*/
public Customer getCustomerAt(int index) {
if (index < 0 || index >= list.size())
return null;
else
return list.get(index);
}
/**
* Adds a customer to the end of the collection.
* @param customer - the new Customer object to be added.
*/
public void addCustomer(Customer customer) {
list.add(customer);
}
/**
* Removes the customer at the location indicated by the parameter.
* @param index - the location of the customer to be removed.
* @return - an integer indicating
*/
public int removeCustomerAt(int index) {
if (index < 0 || index >= list.size())
return INVALIDINDEX;
else if (list.get(index).getCurrentPoints() != 0)
return POINTSNOTZERO;
else {
list.remove(index);
return CUSTOMERDELETED;
}
}
/**
* Retrieves the grand total of unredeemed points held by all customers
* in the scheme.
* @return - the total number of points.
*/
public int getTotalPoints() {
int points = 0;
for (Customer customer : list)
points += customer.getCurrentPoints();
return points;
}
/**
* Finds out the size of the collection.
* @return - the number of customers in the list.
*/
public int size() {
return list.size();
}
/**
* Returns details of the current state of the collection.
* @return - a string containing the total size of the list and the
* total number of points held by all the customers.
*/
@Override
public String toString() {
return "CustomerList{has " + size() + " customers and "
+ getTotalPoints() + " points"
+ '}';
}
}
Sorry if I'm missing something so ridiculously straightforward, but it's driving me insane and I just can't spot the problem as it isn't throwing ANY ERRORS at all, it just quite simply isn't doing it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在您的代码中没有看到任何告诉图形更新的地方。您只需将其添加到
refreshList
方法中:g
是一个Graphics
对象,我认为您也没有。阅读 文档如果这不起作用。I don't see any point in your code where you tell the graphics to update. You just need to add this to the
refreshList
method:g
is aGraphics
object, which I don't think you have either. Read up on the documentation if that doesn't work.