2011-12-12 172 views
-2

好吧,對於即將發生的文本牆感到抱歉,但我有一個非常惱人的問題,我似乎無法解決。我目前正在嘗試從String []填充JList,但出血的東西拒絕填充。我的程序開心地寫入我的源文件,並且很高興地從它讀取(它向我拋出0錯誤),但是當我實際上嘗試用包含的數據填充我的JList時......沒有任何反應,它只是拒絕做任何事。你能看看代碼,並嘗試爲我找出問題嗎?我真的不知道爲什麼這個令人難以置信的簡單任務給了我很多問題。從數組填充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" 
       + '}'; 
    } 
} 

很抱歉,如果我想的東西如此可笑的簡單,但它的駕駛我瘋狂,我只是不能發現問題,因爲它根本不會拋出任何錯誤,它只是簡單的不做。

+1

這是相當多的代碼要經歷...你確定你不能縮小更多? –

+0

將其降到最低限度 - 只是數據,列表,觸發顯示的按鈕。順便說一句,沒有人想通過噸生成的代碼:-) – kleopatra

回答

0

我在代碼中看不到任何指示要更新的圖形的點。你只需要它添加到refreshList方法:

public void refreshList() { 
    ... 
    repaint(g); 
} 

gGraphics對象,我不認爲你要麼。如果這不起作用,請閱讀documentation