求助,JAVA程序逐行解释/用英文和中文。详细解释

wuyuhan 2003-02-07 03:49:05
// Fig. 16.14: ReadRandomFile.java
// This program reads a random-access file sequentially and
// displays the contents one record at a time in text fields.

// Java core packages
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.DecimalFormat;

// Java extension packages
import javax.swing.*;

// Deitel packages
import com.deitel.jhtp4.ch16.*;

public class ReadRandomFile extends JFrame {
private BankUI userInterface;
private RandomAccessFile input;
private JButton nextButton, openButton;

// set up GUI
public ReadRandomFile()
{
super( "Read Client File" );

// create reusable user interface instance
userInterface = new BankUI( 4 ); // four textfields
getContentPane().add( userInterface );

// configure generic doTask1 button from BankUI
openButton = userInterface.getDoTask1Button();
openButton.setText( "Open File for Reading..." );

// register listener to call openFile when button pressed
openButton.addActionListener(

// anonymous inner class to handle openButton event
new ActionListener() {

// enable user to select file to open
public void actionPerformed( ActionEvent event )
{
openFile();
}

} // end anonymous inner class

); // end call to addActionListener

// configure generic doTask2 button from BankUI
nextButton = userInterface.getDoTask2Button();
nextButton.setText( "Next" );
nextButton.setEnabled( false );

// register listener to call readRecord when button pressed
nextButton.addActionListener(

// anonymous inner class to handle nextButton event
new ActionListener() {

// read a record when user clicks nextButton
public void actionPerformed( ActionEvent event )
{
readRecord();
}

} // end anonymous inner class

); // end call to addActionListener

// register listener for window closing event
addWindowListener(

// anonymous inner class to handle windowClosing event
new WindowAdapter() {

// close file and terminate application
public void windowClosing( WindowEvent event )
{
closeFile();
}

} // end anonymous inner class

); // end call to addWindowListener

setSize( 300, 150 );
show();
}

// enable user to select file to open
private void openFile()
{
// display file dialog so user can select file
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_ONLY );

int result = fileChooser.showOpenDialog( this );

// if user clicked Cancel button on dialog, return
if ( result == JFileChooser.CANCEL_OPTION )
return;

// obtain selected file
File fileName = fileChooser.getSelectedFile();

// display error is file name invalid
if ( fileName == null ||
fileName.getName().equals( "" ) )
JOptionPane.showMessageDialog( this,
"Invalid File Name", "Invalid File Name",
JOptionPane.ERROR_MESSAGE );

else {

// open file
try {
input = new RandomAccessFile( fileName, "r" );
nextButton.setEnabled( true );
openButton.setEnabled( false );
}

// catch exception while opening file
catch ( IOException ioException ) {
JOptionPane.showMessageDialog( this,
"File does not exist", "Invalid File Name",
JOptionPane.ERROR_MESSAGE );
}
}

} // end method openFile

// read one record
public void readRecord()
{
DecimalFormat twoDigits = new DecimalFormat( "0.00" );
RandomAccessAccountRecord record =
new RandomAccessAccountRecord();

// read a record and display
try {

do {
record.read( input );
} while ( record.getAccount() == 0 );

String values[] = {
String.valueOf( record.getAccount() ),
record.getFirstName(),
record.getLastName(),
String.valueOf( record.getBalance() ) };
userInterface.setFieldValues( values );
}

// close file when end-of-file reached
catch ( EOFException eofException ) {
JOptionPane.showMessageDialog( this, "No more records",
"End-of-file reached",
JOptionPane.INFORMATION_MESSAGE );
closeFile();
}

// process exceptions from problem with file
catch ( IOException ioException ) {
JOptionPane.showMessageDialog( this,
"Error Reading File", "Error",
JOptionPane.ERROR_MESSAGE );

System.exit( 1 );
}

} // end method readRecord

// close file and terminate application
private void closeFile()
{
// close file and exit
try {
if ( input != null )
input.close();

System.exit( 0 );
}

// process exception closing file
catch( IOException ioException ) {
JOptionPane.showMessageDialog( this,
"Error closing file",
"Error", JOptionPane.ERROR_MESSAGE );

System.exit( 1 );
}
}

// execute application
public static void main( String args[] )
{
new ReadRandomFile();
}

} // end class ReadRandomFile



...全文
105 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
smwu 2003-02-07
  • 打赏
  • 举报
回复
这个太长了吧 弟兄 想入门还是看看书吧 ...给钱我就干
dd777 2003-02-07
  • 打赏
  • 举报
回复
too tooo long
nirvana_hg 2003-02-07
  • 打赏
  • 举报
回复
哇……太长了吧!

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧