-2
public AddressBookApp(){

    frame = new JFrame("Address Book");
    frame.setSize(500, 400);
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

    panel = new JPanel();
    panel.setBackground(Color.gray);
    panel.setLayout(null);

    frame.add(panel);
    frame.setVisible(true);

    JMenuBar menubar = new JMenuBar();
    frame.setJMenuBar(menubar);
    JMenu file = new JMenu("File");
    menubar.add(file);
    JMenuItem insert = new JMenuItem("Import");
            file.add(insert);
            insert.addActionListener(this);
            JMenuItem export = new JMenuItem("Export");
            file.add(export);
            export.addActionListener(this);
            JMenuItem exit = new JMenuItem("Exit");
            file.add(exit);
            exit.addActionListener(this);

    Font f = new Font("Helvetica", Font.BOLD, 10);
    btnadd = new JButton("Add");
    btnadd.setFont(f);
    btnadd.setBounds(200, 250, 80, 20);
    panel.add(btnadd);

    btnprev = new JButton("Previous");
    btnprev.setBounds(40, 250, 80, 20);
    btnprev.setFont(f);
    btnprev.addActionListener(this);
    panel.add(btnprev);

    btnnxt = new JButton("Next");
    btnnxt.setBounds(120, 250, 80, 20);
    btnnxt.setFont(f);
    btnnxt.addActionListener(this);
    panel.add(btnnxt);

    btndel = new JButton("Delete");
    btndel.setBounds(280, 250, 80, 20);
    btndel.setFont(f);
    panel.add(btndel);

    btnclear = new JButton("Clear");
    btnclear.setBounds(360, 250, 80, 20);
    btnclear.setFont(f);
    btnclear.addActionListener(this);
    panel.add(btnclear);

    txtname = new JTextField("");
    txtname.setBounds(210, 40, 160, 20);
    txtname.setFont(f);
    panel.add(txtname);

    txtnum = new JTextField("");
    txtnum.setBounds(210, 70, 160, 20);
    txtnum.setFont(f);
    panel.add(txtnum);

    txtmob = new JTextField("");
    txtmob.setBounds(210, 100, 160, 20);
    txtmob.setFont(f);
    panel.add(txtmob);

    txtadd1 = new JTextField("");
    txtadd1.setBounds(210, 130, 160, 20);
    txtadd1.setFont(f);
    panel.add(txtadd1);

    lblname = new JLabel("Name");
    lblname.setBounds(160, 40, 160, 20);
    lblname.setFont(f);
    panel.add(lblname);

    lblnum = new JLabel("Number");
    lblnum.setBounds(160, 70, 160, 20);
    lblnum.setFont(f);
    panel.add(lblnum);

    lblmob = new JLabel("Mobile");
    lblmob.setBounds(160, 100, 160, 20);
    lblmob.setFont(f);
    panel.add(lblmob);

    lbladd1 = new JLabel("Address ");
    lbladd1.setBounds(160, 130, 160, 20);
    lbladd1.setFont(f);
    panel.add(lbladd1);

}
 public static void main(String[] args) 
{
    AddressBookApp ab =  new AddressBookApp();
}

   public void actionPerformed(ActionEvent e)
   {
   if (e.getActionCommand().equals("Exit"))
       System.exit(0);
   else if (e.getActionCommand().equals("Import"))
   {
          importContacts();
   }
   else if (e.getActionCommand().equals("Export"));
   {
          exportContacts();
   }      
   if (e.getSource() == btnnxt)
   {
          nextContact();
   }
   else if (e.getSource() == btnprev)
   {
          prevContact();
   }
   }
    public void importContacts()
    {

    try{
        BufferedReader fileSize = new BufferedReader(new FileReader("../files/example.buab"));
        BufferedReader importContacts = new BufferedReader(new FileReader("../files/example.buab"));
        int i = 0;
        String contacts;
        while (( fileSize.readLine()) !=null)
        {
             details.add(importContacts.readLine());
             i++;
        }
        fileSize.close();
        int x = 0;
        int y = 0;
        for (x = 0, y = 0; x < details.size(); x++, y++) 
        {
        if (y == 4)
        {
            y = 0;
        }
        if (y == 0)
        {
            name.add(details.get(x));
        }
        if (y == 1)
        {
            phone.add(details.get(x));
        }
        if (y == 2)
        {
            mobile.add(details.get(x));
        }
        if (y == 3)
        {
            address.add(details.get(x));       
        }         
        } 
        }
        catch (IOException ioe)               
         {                       
             ioe.printStackTrace();                
         }                
            txtname.setText(name.get(0));                
            txtnum.setText(phone.get(0));               
            txtmob.setText(mobile.get(0));               
            txtadd1.setText(address.get(0));
    }

   public void exportContacts()
   {
   FileOutputStream file; 
   PrintStream out; 

   try {   file = new FileOutputStream("../files/example.buab", true);   
          out = new PrintStream(file);       
          out.println(txtname.getText());      
          out.println(txtnum.getText());     
          out.println(txtmob.getText());
          out.println(txtadd1.getText()); 

          System.err.println ("");                    
          out.close();              
       }            
          catch (Exception e)
                {                   
                 System.err.println ("Error in writing to file");          
                }
   }

    public void nextContact()
    {

       if(index < details.size() - 1)               
       {           

            index++; 


            txtname.setText(name.get(index));                
            txtnum.setText(phone.get(index));               
            txtmob.setText(mobile.get(index));               
            txtadd1.setText(address.get(index));            
       }                
       importContacts();
    }

    public void prevContact()
    {
    if (index > 0)               
     {                       
           index--;  


            txtname.setText(name.get(index));                
            txtnum.setText(phone.get(index));               
            txtmob.setText(mobile.get(index));               
            txtadd1.setText(address.get(index));               
     }           
     importContacts();

    }

}

4

2 回答 2

1

从代码中我看不出为什么它应该在按下下一个或上一个按钮后导出数据。阅读后不要忘记关闭流。

只是出于好奇——我已经从各种新的 SO 用户那里看到了很多针对这项任务的部分解决方案——而且他们的共同点是,受让人似乎缺乏一些基本的 OO 知识。谁建议您将联系人详细信息放在单独的列表中,而不是发明一个包含所有联系人属性的联系人类并将联系人对象放在单个(可排序的)列表中?你是否被迫逃课,或者你的老师只是让你一个人呆着,让你自己发现?

无论如何祝你好运:)

于 2009-11-30T11:06:52.967 回答
0

您的问题是此行末尾的分号:

else if (e.getActionCommand().equals("Export"));
于 2009-12-02T16:42:35.307 回答