下面是全部代码//A reusable GUI for the examples in this chapter package com.deitel.jhtp3.ch17; import java.awt.*; import javax.swing.*;public class BankUI extends JPanel{ protected final static String names[]={"Account number","First name","Last name","Balance","Transaction Amount" }; protected JLabel labels[]; protected JTextField fields[]; protected JButton doTask,doTask2; protected JPanel innerPanelCenter,innerPanelSouth; protected int size=4; public static final int ACCOUNT=0,FIRST=1,LAST=2,BALANCE=3,TRANSACTION=4;
public BankUI(){ this(4); }
public BankUI(int mySize){ size=mySize; labels=new JLabel[size]; fields=new JTextField[size];
public JButton getDoTask(){ return doTask;} public JButton getDoTask2(){return doTask2;} public JTextField[] getFields(){return fields;}
public void clearFields(){ for(int i=0;i<size;i++) fields[i].setText(""); }
public void setFieldValues(String s[]) throws IllegalArgumentException{ if(s.length!=size) throw new IllegalArgumentException( "There must be"+size+"strings in the array");
public CreateSequentialFile(){ super("Creating a Sequential File of objects"); getContentPane().setLayout(new BorderLayout()); userInterface=new BankUI();
enter=userInterface.getDoTask(); enter.setText("Enter"); enter.setEnabled(false); enter.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { addReccord(); } } );
addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e) { if(output!=null){ addRecord(); closeFile(); } else System.exit(0); } } );
open=userInterface.getDoTask2(); open.setText("Save as"); open.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { openFile(); } } );
package com.deitel.jhtp3.ch17;
import java.awt.*;
import javax.swing.*;public class BankUI extends JPanel{
protected final static String names[]={"Account number","First name","Last name","Balance","Transaction Amount"
};
protected JLabel labels[];
protected JTextField fields[];
protected JButton doTask,doTask2;
protected JPanel innerPanelCenter,innerPanelSouth;
protected int size=4;
public static final int ACCOUNT=0,FIRST=1,LAST=2,BALANCE=3,TRANSACTION=4;
public BankUI(){
this(4);
}
public BankUI(int mySize){
size=mySize;
labels=new JLabel[size];
fields=new JTextField[size];
for(int i=0;i<labels.length;i++)
labels[i]=new JLabel(names[i]);
for(int i=0;i<fields.length;i++)
fields[i]=new JTextField();
innerPanelCenter=new JPanel();
innerPanelCenter.setLayout(new GridLayout(size,2));
for(int i=0;i<size;i++){
innerPanelCenter.add(labels[i]);
innerPanelCenter.add(fields[i]);
}
doTask=new JButton();
doTask2=new JButton();
innerPanelSouth=new JPanel();
innerPanelSouth.add(doTask);
innerPanelSouth.add(doTask2);
setLayout(new BorderLayout());
add(innerPanelCenter,BorderLayout.CENTER);
add(innerPanelSouth,BorderLayout.SOUTH);
validate();
}
public JButton getDoTask(){ return doTask;}
public JButton getDoTask2(){return doTask2;}
public JTextField[] getFields(){return fields;}
public void clearFields(){
for(int i=0;i<size;i++)
fields[i].setText("");
}
public void setFieldValues(String s[]) throws IllegalArgumentException{
if(s.length!=size) throw new IllegalArgumentException(
"There must be"+size+"strings in the array");
for(int i=0;i<size;i++)
fields[i].setText(s[i]);
}
public String[] getFileValues(){
String values[]=new String[size];
for(int i=0;i<size;i++)
values[i]=fields[i].getText();
return values;
}
}
package com.deitel.jhtp3.ch17;
import java.io.Serializable;public class BankAccountRecord implements Serializable{
private int account;
private String firstName;
private String lastName;
private double balance;
public BankAccountRecord(){
this(0,"","",0.0);
}
public BankAccountRecord(int acct,String first,String last,double bal){
setAccount(acct);
setFirstName(first);
setLastName(last);
setBalance(bal);
}
public void setAccount(int acct){
account=acct;
}
public int getAccount(){return account; }
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 setBalance(double bal){
balance=bal;
}
public double getBalance(){return balance; }
}import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.deitel.jhtp3.ch17.BankUI;
import com.deitel.jhtp3.ch17.BankAccountRecord;public class CreateSequentialFile extends JFrame{
private ObjectOutputStream output;
private BankUI userInterface;
private JButton enter,open;
public CreateSequentialFile(){
super("Creating a Sequential File of objects");
getContentPane().setLayout(new BorderLayout());
userInterface=new BankUI();
enter=userInterface.getDoTask();
enter.setText("Enter");
enter.setEnabled(false);
enter.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
addReccord();
}
}
);
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
if(output!=null){
addRecord();
closeFile();
}
else
System.exit(0);
}
}
);
open=userInterface.getDoTask2();
open.setText("Save as");
open.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
openFile();
}
}
);
getContentPane().add(userInterface,BorderLayout.CENTER);
setSize(300,300);
show();
} private void openFile()
{
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result=fileChooser.showSaveDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
return;
File fileName=fileChooser.getSelectedFile();
if(fileName==null||fileName.getName().equals(""))
JOptionPane.showMessageDialog(this,"Invalid File Name",
"Invalid File Name",
JOptionPane.ERROR_MESSAGE);
else
try{
output=new ObjectOutputStream(
new FileOutputStream(fileName) );
open.setEnabled(false);
enter.setEnabled(true);
}catch(IOException e){
JOptionPane.showMessageDialog(this,"Error Opening File",
"Error",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
private void closeFile(){
try{
output.close();
System.exit(0);
}catch(IOException ex){
JOptionPane.showMessageDialog(this,"Error closing file",
"Error",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
public void addRecord(){
int accountNumber=0;
BankAccountRecord record;
String fieldValues[]=userInerface.getFieldValues();
if(!fieldValues[0].equals("")){
try{
accountNumber=Integer.parseInt(fieldValues[0]);
if(accountNumber>0){
record=new BankAccountRecord(accountNumber,fieldValues[1],
fieldValues[2],
Double.parseDouble(fieldValues[3]));
output.writeObject(record);
output.flush();
}
userInterface.clearFields();
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(this,"Bad Account number or balance",
"Invalid Number Format",
JOptionPane.ERROR_MESSAGE);
}
catch(IOException io){
closeFile();
}
}
}
public static void main(String args[]){
new CreateSequentialFile();
}}