2012-12-01 226 views
1

我做了一個聯繫人列表,我的代碼編譯並運行,當你添加聯繫人,鍵入或發送電子郵件或打電話給它時,我在這裏遇到的問題是當我添加聯繫人和按下一個或上一個,首先或最後它不讀取名稱聯繫人,但它讀取其他一切...它是否必須做一些間距?這裏是我...謝謝閱讀姓名聯繫人

import java.io.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.*; 



public class ContactSystem extends JFrame{ 
//Specify the size of five string fields in the record 
final static int NAME_SIZE = 32; 
final static int TYPE_SIZE = 32; 
final static int CITY_SIZE = 20; 
final static int PHONE_SIZE = 15; 
final static int EMAIL_SIZE = 15; 
final static int RECORD_SIZE = 
     (NAME_SIZE + TYPE_SIZE + CITY_SIZE + PHONE_SIZE + EMAIL_SIZE); 

//access contact.dat using RandomAccessFile 
private RandomAccessFile raf; 

//Text Fields 
private JTextField jbtName = new JTextField(NAME_SIZE); 
private JTextField jbtType = new JTextField(TYPE_SIZE); 
private JTextField jbtCity = new JTextField(CITY_SIZE); 
private JTextField jbtPhone = new JTextField(PHONE_SIZE); 
private JTextField jbtEmail = new JTextField(EMAIL_SIZE); 

//Buttons 
private JButton jbtAdd = new JButton("Add"); 
private JButton jbtFirst = new JButton("First"); 
private JButton jbtNext = new JButton("Next"); 
private JButton jbtPrevious = new JButton("Previous"); 
private JButton jbtLast = new JButton("Last"); 

public ContactSystem(){ 
    //open or create a random access file 

    try { 
     raf = new RandomAccessFile("contact.txt", "rw"); 
    } 
    catch(IOException ex) { 
     System.out.print("Error: " + ex); 
     System.exit(0); 
    } 

    //Panel p1 for holding labels Name , Type, Email or phone 
    JPanel p1 = new JPanel(); 
    p1.setLayout(new GridLayout(3,1)); 
    p1.add(new JLabel("Name Contact")); 
    p1.add(new JLabel("Email Address")); 
    p1.add(new JLabel("Type of Contact")); 

    //Panel jpPhone for holding Phone 
    JPanel jpPhone = new JPanel(); 
    jpPhone.setLayout(new BorderLayout()); 
    jpPhone.add(new JLabel("Phone"), BorderLayout.WEST); 
    jpPhone.add(jbtPhone, BorderLayout.CENTER); 

    //Panel jpEmail for holding Phone 
    JPanel jpEmail = new JPanel(); 
    jpEmail.setLayout(new BorderLayout()); 
    jpEmail.add(new JLabel("Phone"), BorderLayout.WEST); 
    jpEmail.add(jbtEmail, BorderLayout.CENTER); 

    // Panel p2 for holding jpPhone and jpEmail 
    JPanel p2 = new JPanel(); 
    p2.setLayout(new BorderLayout()); 
    p2.add(jpPhone, BorderLayout.WEST); 
    p2.add(jpPhone, BorderLayout.CENTER); 

    JPanel p3 = new JPanel(); 
    p3.setLayout(new BorderLayout()); 
    p3.add(jbtCity, BorderLayout.CENTER); 
    p3.add(p2, BorderLayout.EAST); 

    //panel p4 for holding jtfName, jtfType, and p3 
    JPanel p4 = new JPanel(); 
    p4.setLayout(new GridLayout(3,1)); 
    p4.add(jbtName); 
    p4.add(jbtType); 
    p4.add(p3); 

    //place p1 and p4 into jpContact 
    JPanel jpContact = new JPanel(new BorderLayout()); 
    jpContact.add(p1, BorderLayout.WEST); 
    jpContact.add(p4, BorderLayout.CENTER); 

    //set panel with line border 
    jpContact.setBorder(new BevelBorder(BevelBorder.RAISED)); 

    //add buttons to a panel 
    JPanel jpButton = new JPanel(); 
    jpButton.add(jbtAdd); 
    jpButton.add(jbtFirst); 
    jpButton.add(jbtNext); 
    jpButton.add(jbtPrevious); 
    jpButton.add(jbtLast); 

    //add jpContact and jpButton to the frame 

    add(jpContact, BorderLayout.CENTER); 
    add(jpButton, BorderLayout.SOUTH); 

    jbtAdd.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      writeContact(); 

     } 
    }); 
    jbtFirst.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      try { 
       if (raf.length() > 0) readContact(0); 
      } 
      catch(IOException ex){ 
       ex.printStackTrace(); 
      } 
     } 
    }); 
    jbtNext.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e) { 
      try { 
       long currentPosition = raf.getFilePointer(); 
       if (currentPosition < raf.length()) 
        readContact(currentPosition); 

      } 
      catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
    }); 

    jbtPrevious.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      try { 
       long currentPosition = raf.getFilePointer(); 
       if(currentPosition - 2 * RECORD_SIZE > 0) 
        //why 2 * 2 * RECORD_SIZE? see the the follow-up remarks 
        readContact(currentPosition - 2 * 2 * RECORD_SIZE); 
       else 
        readContact(0); 

      } 
      catch(IOException ex){ 
       ex.printStackTrace(); 
      } 
     } 
    }); 

    jbtLast.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      try { 
       long lastPosition = raf.length(); 
       if(lastPosition > 0) 
        //why 2 * RECORD_SIZE? see the follow up remarks 
        readContact(lastPosition - 2 * RECORD_SIZE); 

      } 
      catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
    }); 

    try { 
     if (raf.length() > 0) readContact(0); 

    } 

    catch (IOException ex) { 
     ex.printStackTrace(); 

    } 


} 

//write a record at the end of file 

public void writeContact() { 

    try { 
     raf.seek(raf.length()); 
     FixedLengthStringIO.writeFixedLengthString(
       jbtName.getText(), NAME_SIZE, raf); 
     FixedLengthStringIO.writeFixedLengthString(
       jbtType.getText(), TYPE_SIZE, raf); 
     FixedLengthStringIO.writeFixedLengthString(
       jbtCity.getText(), CITY_SIZE, raf); 
     FixedLengthStringIO.writeFixedLengthString(
       jbtPhone.getText(), PHONE_SIZE, raf); 
     FixedLengthStringIO.writeFixedLengthString(
       jbtEmail.getText(), EMAIL_SIZE, raf); 

    } 
    catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
} 

//read a contact at the specific position 
public void readContact(long position) throws IOException{ 
    raf.seek(position); 
    String name = FixedLengthStringIO.readFixedLengthString(
      NAME_SIZE, raf); 
    String type = FixedLengthStringIO.readFixedLengthString(
      TYPE_SIZE, raf); 
    String city = FixedLengthStringIO.readFixedLengthString(
      CITY_SIZE, raf); 
    String phone = FixedLengthStringIO.readFixedLengthString(
      PHONE_SIZE, raf); 
    String email = FixedLengthStringIO.readFixedLengthString(
      EMAIL_SIZE, raf); 

    jbtName.setText(name); 
    jbtType.setText(type); 
    jbtCity.setText(city); 
    jbtName.setText(phone); 
    jbtName.setText(email); 

} 



/** 
* @param args 
*/ 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    ContactSystem frame = new ContactSystem(); 
    frame.pack(); 
    frame.setTitle("Contact System"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 

} 

} 

fixedlengthstringIO類

import java.io.*; 


    public class FixedLengthStringIO { 

//read fixed number of characters from datainput stream 
public static String readFixedLengthString(int size, 
     DataInput in) throws IOException { 
    //declare an array of characters 
    char[] chars = new char[size]; 

    //read fixed number of characters to the array 
    for(int i = 0; i < size; i++) 
     chars[i] = in.readChar(); 
    return new String (chars); 
} 

//write fixed number of characters to a dataoutput stream 

public static void writeFixedLengthString(String s, int size, 
     DataOutput out) throws IOException { 
    char[] chars = new char[size]; 

    //fill an array of characters from the string 
    s.getChars(0, Math.min(s.length(), size), chars, 0); 

    //fill in blank characters in the rest of the array 
    for (int i = Math.min(s.length(), size); i < chars.length; i++) 
     chars[i] = ' '; 

    //create and write a new string padded with blank characters 
    out.writeChars(new String(chars)); 
} 
} 

這裏是當我輸入聯繫人的名稱,類型,電子郵件等進入榜單

enter image description here

當我按下一個或上一個,或第一個或最後一個,它不讀取名稱..

enter image description here

+2

請降低你的代碼是沒有按特定部分不要做它應該做的事情。向我們解釋它應該做什麼,以及它做什麼。最好使用示例輸入和輸出。 –

+0

+1好實驗。 – vels4j

回答

1

毛刺是在你的readContact方法,也會改變該

public void readContact(long position) throws IOException { 
     raf.seek(position); 
     String name = FixedLengthStringIO.readFixedLengthString(
       NAME_SIZE, raf); 
     String type = FixedLengthStringIO.readFixedLengthString(
       TYPE_SIZE, raf); 
     String city = FixedLengthStringIO.readFixedLengthString(
       CITY_SIZE, raf); 
     String phone = FixedLengthStringIO.readFixedLengthString(
       PHONE_SIZE, raf); 
     String email = FixedLengthStringIO.readFixedLengthString(
       EMAIL_SIZE, raf); 

     jbtName.setText(name); 
     jbtType.setText(type); 
     jbtCity.setText(city); 
     jbtPhone.setText(phone); 
     jbtEmail.setText(email); 

    } 

,而不是添加單獨的ActionListner命令按鈕就可以實現像

public class ContactSystem extends JFrame implements ActionListener 
{ 
    ..... 
    jbtAdd.addActionListener(this); 
    jbtFirst.addActionListener(this); 
    ..... 

    @Override 
    public void actionPerformed(ActionEvent e) { 
    String actionCommand = e.getActionCommand(); 
    if(actionCommand.equals("Add")) { 
     //do add stuff 
    } else if (actionCommand.equals("First")) { 
     // move first 
    } 
    } 
} 
+0

*「而不是添加單獨的'ActionListner'命令按鈕,你可以實現像」*是的你可以,但人們通常建議爲每個需要它們的按鈕/菜單項添加單獨的'ActionListener'或'Action'。 –

+0

@AndrewThompson是湯普森,我讀過一些文件。你是對的。謝謝。 – vels4j