如何将值从文本字段传递到对象或文件(可序列化文件)?

时间:2012-03-08 11:01:39

标签: java swing

我已准备好布局代码(我有3个班级)。 在我输入之后 第一个名字,姓氏,地址,年龄和工资然后我点击“保存”按钮,它应保存在Employee.ser文件中,每次保存输入信息时都不应该覆盖。

import javax.swing.*;    
import java.awt.*;    
import java.io.*;    
import java.util.*;    
public class EmployeeApp extends JFrame    
{    
    private ArrayList <Employee> list;    

public EmployeeApp()
{
    list = new ArrayList<Employee>();
}

JPanel panel;
JLabel label;
JTextField field;
JFrame frame;
JButton save;


public void initialize()
{
    panel = new JPanel();
    frame = new JFrame("Mark");
    save = new JButton("save");
    frame.add(BorderLayout.SOUTH, save);
    getContentPane().setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(350,350);
    frame.setVisible(true);
    panel.setLayout(new GridLayout(12,0));

    JLabel label = new JLabel("Enter First Name ");
    field = new JTextField(20);
    frame.add(BorderLayout.CENTER,panel);
    panel.add(label);
    panel.add(field);


    label = new JLabel("Enter Last Name ");
    field = new JTextField(20);
    panel.add(label);
    panel.add(field);

    label = new JLabel("Enter Adress ");
    field = new JTextField(20);
    panel.add(label);
    panel.add(field);

    label = new JLabel("Enter Age ");
    field = new JTextField(20);
    panel.add(label);
    panel.add(field);

    label = new JLabel("Enter Salary ");
    field = new JTextField(20);
    panel.add(label);
    panel.add(field);


}


public void start()
{
    initialize();
}

private void load()
{
    File empFile = new File("Employee.ser");
    if(empFile.exists())
    {
        try
        {
            FileInputStream fis = new FileInputStream(empFile);
            ObjectInputStream ois = new ObjectInputStream(fis);
            Employee emp = null;
            while((emp = (Employee) ois.readObject()) != null)
            {
                list.add(emp);
            }
        }
        catch(Exception e){}
    }
}    
}

import java.io.*;    

public class Employee implements Serializable    
{    

private String firstName;
private String lastName;
private String address;
private int age;
private double salary;




    public void setFirstName(String first) {
            firstName = first;
    }
    public String getFirstName() {
            return firstName;
    }

    public void setLastName(String last) {
            lastName = last;
    }
    public String getLastName() {
            return lastName;
    }

    public void setAddress(String ad) {
            address = ad;
    }
    public String getAddress() {
            return address;
    }

    public void setAge(int ag){
        age = ag;
    }
    public int getAge(){
        return age;
    }

    public void setSalary(double sal){
        salary = sal;
    }
    public double getSalary(){
        return salary;
    }    
}    

     public class EmployeeLauncher    
{    
    public static void main(String[] args) throws Exception    
    {    
        EmployeeApp em = new EmployeeApp();    
        em.start();    
    }    
}    

2 个答案:

答案 0 :(得分:1)

  1. 将ActionListener添加到保存按钮。
  2. 覆盖actionPerformed以处理保存操作(从textFields获取值)。
  3. 加载文件中的所有对象(您在load()中完成的操作)
  4. 创建FileOutputSteram并使用writeObject() ObjectOutputStream方法编写列表中的所有对象。

答案 1 :(得分:0)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class EmployeeApp extends JFrame implements ActionListener
{
    private ArrayList     <Employee> list;

public EmployeeApp()
{
    list = new ArrayList<Employee>();
}
Employee obj = new Employee();

JPanel panel;
JLabel label;
JTextField field;
JFrame frame;
JButton save;
private JTextField nameField;

public void initialize()
{
    nameField = new JTextField();
    panel = new JPanel();
    frame = new JFrame("Mark");
    save = new JButton("save");
    frame.add(BorderLayout.SOUTH, save);
    getContentPane().setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(350,350);
    frame.setVisible(true);
    panel.setLayout(new GridLayout(12,0));

    JLabel label = new JLabel("Enter First Name ");
    field = new JTextField(20);
    frame.add(BorderLayout.CENTER,panel);
    panel.add(label);
    panel.add(field);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    label = new JLabel("Enter Last Name ");
    field = new JTextField(20);
    panel.add(label);
    panel.add(field);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    label = new JLabel("Enter Adress ");
    field = new JTextField(20);
    panel.add(label);
    panel.add(field);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    label = new JLabel("Enter Age ");
    field = new JTextField(20);
    panel.add(label);
    panel.add(field);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    label = new JLabel("Enter Salary ");
    field = new JTextField(20);
    panel.add(label);
    panel.add(field);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    save.addActionListener(this);
    load();




}

public void actionPerformed(ActionEvent e)
{
    load();
    obj.setFirstName(nameField.getText());
    obj.setLastName(nameField.getText());
    obj.setAge(nameField.getText());
    obj.setAddress(nameField.getText());
    obj.setSalary(nameField.getText());

}


public void start()
{
    initialize();

}

private void load()
{
    File empFile = new File("Employee.ser");
    if(empFile.exists())
    {
        try
        {
            FileInputStream fis = new FileInputStream(empFile);
            ObjectInputStream ois = new ObjectInputStream(fis);

            Employee emp = null;
            while((emp = (Employee) ois.readObject()) != null)
            {
                list.add(emp);
            }
        }
        catch(Exception e){}
    }
}
  private void saveObject()
{
    try
    {
        File empFile = new File("Employee.ser");
        FileOutputStream fos = new FileOutputStream(empFile);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        for(Employee emp : list)
        {
            oos.writeObject(emp);
        }
        oos.close();
    }
    catch(Exception e){}
}

}