Java Arraylist - 编译错误

标签 java

我正在尝试编译我的程序,但它给了我 5 个错误

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.text.*;
import java.net.*;
import java.util.Scanner;

public class AddressBook extends JFrame implements ActionListener
{

FlowLayout leftLayout;

    JFrame frame;
    JPanel panel;
    JTextField txtname,txtsurname, txtphone, txtmobile, txtaddress, txtpostcode;
    JButton btnadd, btnnext, btnprevious, btnsave, btndelete;
    JLabel jlbname, jlbsurname, jlbphone, jlbmobile, jlbaddress, jlbpostcode;

    List<Person> people = new ArrayList<Person>();




    int i = 0;


    public static void main(String[] args) throws IOException
    {
        new AddressBook();
    }

        public AddressBook()
        {

            //sets window
            frame = new JFrame();
            frame.setTitle("Bournemouth University Address Book");
            frame.setSize(760, 500);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //sets up panel
            panel = new JPanel();
            panel.setLayout(null);
            frame.getContentPane().add(panel);



            //Labels
            jlbname = new JLabel("Name:");
            jlbname.setBounds(10, 50, 100, 20);
            panel.add(jlbname);

            jlbsurname = new JLabel("Surname:");
            jlbsurname.setBounds(350, 50, 100, 20);
            panel.add(jlbsurname);

            jlbphone = new JLabel("Home Number:");
            jlbphone.setBounds(10, 90, 150, 20);
            panel.add(jlbphone);

            jlbmobile = new JLabel("Mobile:");
            jlbmobile.setBounds(350, 90, 150, 20);
            panel.add(jlbmobile);

            jlbaddress = new JLabel("Address:");
            jlbaddress.setBounds(10, 130, 200, 20);
            panel.add(jlbaddress);

            jlbpostcode = new JLabel("PostCode:");
            jlbpostcode.setBounds(10, 170, 250, 20);
            panel.add(jlbpostcode);

            //Text Fields
            txtname = new JTextField("");
            txtname.setBounds(120, 50, 200, 20);
            panel.add(txtname);

            txtsurname = new JTextField("");
            txtsurname.setBounds(440, 50, 200, 20);
            panel.add(txtsurname);

            txtphone = new JTextField("");
            txtphone.setBounds(120, 90, 200, 20);
            panel.add(txtphone);

            txtmobile = new JTextField("");
            txtmobile.setBounds(440, 90, 200, 20);
            panel.add(txtmobile);

            txtaddress = new JTextField("");
            txtaddress.setBounds(120, 130, 520, 20);
            panel.add(txtaddress);

            txtpostcode = new JTextField("");
            txtpostcode.setBounds(120, 170, 250, 20);
            panel.add(txtpostcode);




            //Buttons
            btnadd = new JButton("Add", new ImageIcon("../files/add.png"));
            btnadd.setBounds(330, 320, 100, 50);
            btnadd.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
            btnadd.addActionListener(new InnerAListener());
                panel.add(btnadd);

            btndelete = new JButton("Delete", new ImageIcon("../files/delete2.png"));
            btndelete.setBounds(390, 250, 100, 50);
            btndelete.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
        btndelete.setForeground(Color.red);
            btndelete.addActionListener(new InnerBListener());
                panel.add(btndelete);

            btnsave = new JButton("Save", new ImageIcon("../files/save.png"));
            btnsave.setBounds(490, 250, 100, 50);
            btnsave.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
            btnsave.addActionListener(new InnerCListener());
                panel.add(btnsave);

            btnprevious = new JButton("Prev", new ImageIcon("../files/left.png"));
            btnprevious.setBounds(280, 250, 100, 50);
            btnprevious.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
            btnprevious.addActionListener(new InnerDListener());
                panel.add(btnprevious);

            btnnext = new JButton("Next", new ImageIcon("../files/right.png"));
            btnnext.setBounds(180, 250, 100, 50);
            btnnext.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
            btnnext.addActionListener(new InnerEListener());
                panel.add(btnnext);

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


        JMenuBar mb = new JMenuBar();
                frame.setJMenuBar(mb);

                JMenu insert = new JMenu("Import");
                mb.add(insert);
                JMenuItem imp = new JMenuItem("Add New Contacts");
                insert.add(imp);
                imp.addActionListener(new InnerFListener());

            }




               private class InnerAListener
                       implements ActionListener
                       {
                            public void actionPerformed(final ActionEvent e)
                                            {
                                            Clearscreen();
                                            }
                       }

                       private class InnerBListener
                       implements ActionListener
                       {
                            public void actionPerformed(final ActionEvent e)
                                            {
                                            Delete();
                                            }
                       }

                       private class InnerCListener
                       implements ActionListener
                       {
                            public void actionPerformed(final ActionEvent e)

                                            {
                                            Save();
                                            }
                       }



                       private class InnerDListener
                       implements ActionListener
                       {
                            public void actionPerformed(final ActionEvent e)
                                            {
                                            Previous();
                                            }
                       }

                       private class InnerEListener
                       implements ActionListener
                       {
                            public void actionPerformed(final ActionEvent e)
                                            {
                                            Next();
                                            }
                       }

                       private class InnerFListener
                       implements ActionListener
                       {
                        public void actionPerformed(final ActionEvent e)

                                {
                                ImportContacts();
                                }
                       }




        public void previous()
        {
                if (i > 0)
                {
                    i--;
                }

                display(people.get(i));    
        }

        public void Next()
        {
                if(i < people.size() - 1)
                    {
                    i++;
                }

                display(people.get(i));
        }

        private void display(final Person person)
        {
                txtname.setText(person.getname());
                txtsurname.setText(person.getsurname());
                txtphone.setText(person.getphone());
                txtmobile.setText(person.getmobile());
                txtaddress.setText(person.getaddress());
                txtpostcode.setText(person.getpostcode());
        }

        public void Save()
        {
                try
                {
                BufferedWriter fileOut = new BufferedWriter(new FileWriter("../files/contacts.buab", true)); 
                fileOut.append(txtname.getText());
                fileOut.append("\n");    
                fileOut.append(txtsurname.getText());
                fileOut.append("\n");   
                fileOut.append(txtphone.getText());
                fileOut.append("\n");       
                fileOut.append(txtmobile.getText());
                fileOut.append("\n");  
                fileOut.append(txtaddress.getText());
                fileOut.append("\n");   
                fileOut.append(txtpostcode.getText() + "\r");



                fileOut.close(); 
                }
                    catch (IOException ioe)
                        {
                        JOptionPane.showMessageDialog(null, ioe.getMessage());
                                }

                }


        public void Clearscreen()

            {
            txtname.setText("Add new details here");
            txtsurname.setText("");
            txtphone.setText("");
            txtmobile.setText("");
            txtaddress.setText("");
            txtpostcode.setText("");
            }

        public void ImportContacts
        {

        public void actionPerformed(ActionEvent event)
            {
            JFileChooser fileopen = new JFileChooser();

        int ret = fileopen.showDialog(null, "Open file");

            if (ret == JFileChooser.APPROVE_OPTION) 

            {
                    try
            {
                    final File    file;
                    final Scanner scanner;

                    file    = new File("../files/contacts.buab");
                    scanner = new Scanner(file);

                    while(scanner.hasNextLine())
                {
                final Person person;

                person = new Person(scanner);
                people.add(person);
                }
                }
                catch (IOException ioe)
                {
                    JOptionPane.showMessageDialog(null, ioe.getMessage());
                }

                display(people.get(0)); 

        }

     }     
    };

}

错误全部开始:

    public void ImportContacts
    {

他们说 { 应该是 (,然后它说表达式的非法开始,转到下面的行

     public void actionPerformed(ActionEvent event)

出了什么问题?

最佳答案

首先,这是 Java,因此请遵循命名约定和良好的编程实践:

List<String> name = new ArrayList<String>();
List<String> surname = new ArrayList<String>();
List<String> phone = new ArrayList<String>();
List<String> mobile = new ArrayList<String>();
List<String> address = new ArrayList<String>();
List<String> temp = new ArrayList<String>();

try
{
    BufferedReader infoReader = new BufferedReader(new FileReader("../files/contacts.buab"));
    int i = 0;
    String loadContacts;

    while ((loadContacts = infoReader.readLine()) !=null)
    {
        temp.add(loadContacts);
        i++;
    }

    int a = 0;
    int b = 0;

    for (a = 0, b = 0; a < temp.size(); a++, b++)
    {
        if (b == 0)
        {
            name.add(temp.get(a));    
        }
        else if (b == 1)
        {
            surname.add(temp.get(a));    
        }
        else if (b == 2)
        {
            phone.add(temp.get(a));    
        }
        else if (b == 3)
        {
            mobile.add(temp.get(a));    
        }   
        else if (b == 4)
        {
            address.add(temp.get(a));    
        }         
        else if (b == 5)
        {
            b = 0;
        }
    }
}
catch (IOException ioe)
{
    JOptionPane.showMessageDialog(null, ioe.getMessage());
}

txtName.setText(name.get(0));
txtSurname.setText(surname.get(0));
txtPhone.setText(phone.get(0));
txtMobile.setText(mobile.get(0));
txtAddress.setText(address.get(0));

public void previous()
{
    if (i > 0)
    {
        i--;
    }

    txtName.setText(name.get(i));
    txtSurname.setText(surname.get(i));
    txtPhone.setText(phone.get(i));
    txtMobile.setText(mobile.get(i));
    txtAddress.setText(address.get(i));
}

public void Next()
{
    if(i < temp.size() - 1)
    {
        i++;
    }

    txtName.setText(name.get(i));
    txtSurname.setText(surname.get(i));
    txtPhone.setText(phone.get(i));
    txtMobile.setText(mobile.get(i));
    txtAddress.setText(address.get(i));
 }

Next Java 是一种面向对象语言,所以让我们使用这些功能:

public class Person
{
    private final String firstName;
    private final String lastName;
    private final String phone;
    private final String mobile;
    private final String address;

    public Person(final BufferedReader reader)
        throws IOException,
               InvalidPersonException
    {
        firstName = readLine(reader, "First name");
        lastName  = readLine(reader, "Last name");
        phone     = readLine(reader, "Phone");
        mobile    = readLine(reader, "Mobile");
        address   = readLine(reader, "Address");
    }

    private static String readLine(final BufferedReader reader, 
                                   final String type)
        throws IOException,
               InvalidPersonException
    {
        final String line;

        line = reader.readLine();

        if(line == null)
        {
            throw new InvalidPersonException("missing: " + type);
        }

        return (line);
    }

    public String getFirstName()
    {
        return (firstName);
    }

    public String getLastName()
    {
        return (lastName);
    }

    public String getPhone()
    {
        return (phone);
    }

    public String getMobile()
    {
        return (mobile);
    }

    public String getAddress()
    {
        return (address);
    }
}

我宁愿使用 Scanner 而不是 BufferedReader,因为它更灵活,所以我会将构造函数更改为:

    public Person(final Scanner scanner)
        throws IOException,
               InvalidPersonException
    {
        firstName = readLine(reader, "First name");
        lastName  = readLine(reader, "Last name");
        phone     = readLine(reader, "Phone");
        mobile    = readLine(reader, "Mobile");
        address   = readLine(reader, "Address");
    }

    private static String readLine(final BufferedReader reader, 
                                   final String type)
        throws IOException,
               InvalidPersonException
    {
        final String line;

        if(!(scanner.hasNextLine()))
        {
            throw new InvalidPersonException("missing: " + type);
        }

        line = scanner.nextLine();

        return (line);
    }

这会将代码简化为(以及一些其他更改):

List<Person> people = new ArrayList<Person>();

try
{
    final File    file;
    final Scanner scanner;

    file    = new File("../files/contacts.buab");
    scanner = new Scanner(file);

    while(scanner.hasNextLine())
    {
        final Person person;

        person = new Person(scanner);
        people.add(person);
    }
}
catch (IOException ioe)
{
    JOptionPane.showMessageDialog(null, ioe.getMessage());
}

display(people.get(0); 

public void previous()
{
    if (i > 0)
    {
        i--;
    }

    display(people.get(i));    
}

public void Next()
{
    if(i < people.size() - 1)
    {
        i++;
    }

    display(people.get(i));
 }

 private void display(final Person person)
 {
     txtName.setText(person.getFirstName());
     txtSurname.setText(person.getLastName());
     txtPhone.setText(person.getPhone());
     txtMobile.setText(person.getMobile());
     txtAddress.setText(person.getAddress());
 }

即使你不采用 Person 类的想法,也要采用显示的想法,这样你就不会在 3 个地方有相同的代码......这会让你的生活更轻松。

现在唯一的问题是您从未发布过错误是什么,所以我(还)无法真正帮助您解决该部分。

Blockquote

关于Java Arraylist - 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1804223/

相关文章:

java - 在 Java 中增量更改字符串中的数字

java - Else 语句不在 SQL 数据库的 while 循环内执行

java - 如何避免java.lang.OutOfMemoryError : PermGen space

java - Android 应用程序在第 54 行(Integer.parseInt)处崩溃,并且不完全确定原因

java - 二维数组尝试正确显示

javascript - JavaScript 中的日期和时间验证

java - 更改 TextSize 后 TextView 不会调整大小

java - 避免对属性文件路径规范进行硬编码

java - 是否有与 java.lang.Number 等效的 C++?

具有指定证书的 Java MySQL SSL 连接