import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.util.*;
public class BankOutput extends JFrame implements ActionListener
{
JPanel jp1,jp2,jp3,jp4,jp5;
JLabel jl1,jl2,jl3,jl4;
JTextField jtf1,jtf2,jtf3,jtf4;
JButton jb1,jb2;
JFileChooser jfc;
ObjectOutputStream oos;
ArrayList list;
int count;
String str[]=new String[4];
public BankOutput()
{
Container c=getContentPane();
c.setLayout(new GridLayout(5,1,5,5));
list=new ArrayList();
count=0;
jp1=new JPanel();jp2=new JPanel();jp3=new JPanel();jp4=new JPanel();jp5=new JPanel();
jl1=new JLabel("Account number:");jl2=new JLabel("Name:");jl3=new JLabel("Age:");jl4=new JLabel("Money:");
jtf1=new JTextField(10);jtf2=new JTextField(10);jtf3=new JTextField(10);jtf4=new JTextField(10);
jb1=new JButton("Save");jb2=new JButton("Enter");
jp1.add(jl1);jp1.add(jtf1);
jp2.add(jl2);jp2.add(jtf2);
jp3.add(jl3);jp3.add(jtf3);
jp4.add(jl4);jp4.add(jtf4);
jp5.add(jb1);jp5.add(jb2);
c.add(jp1);c.add(jp2);c.add(jp3);c.add(jp4);c.add(jp5);
setSize(600,400);
show();
jb1.addActionListener(this);
jb2.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
});
}
public static void main(String[] args) throws Exception
{
BankOutput bo=new BankOutput();
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s=="Save")
{
jfc=new JFileChooser();
jfc.setFileFilter(new FileFilter()
{
public boolean accept(File filename)
{
return filename.getName().endsWith(".txt");
}
public String getDescription()
{
return "txt";
}
});
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = jfc.showSaveDialog(this);
File fileName=jfc.getSelectedFile();
setFile(fileName);
}
if(s=="Enter")
{
setWord();
count++;
}
}
public void setFile(File f)
{
if(f!=null)
try
{
oos=new ObjectOutputStream(new FileOutputStream(f));
}
catch (Exception e)
{
e.getMessage();
}
}
public void setWord()
{
str[0]=jtf1.getText();
str[1]=jtf2.getText();
str[2]=jtf3.getText();
str[3]=jtf4.getText();
}
}
我的程序是这样写的:想写一个学生管理器,每个学生有四个属性,通过JTestField输入,现在的问题是:如果我输入很多学生的话,怎么保存在ArrayList里面呢?因为他存储的是对象.