?? fileio.java
字號:
//:FileIO.java
/**
*The programme used to realized the I/O operation for the file
*@author HaiXian Wu in Software College Grade 2003 Class 2 ID:1033710201
*@version 1.2
*Program Time:2004-12-3
*Modify Time:2004-12-5
*Modify Time:2004-12-7
*Modify Time:2004-12-8
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.text.*;
public class FileIO extends JFrame
{
private JLabel filePathLabel,
idLabel,nameLabel,scoreLabel,
idTip,nameTip,scoreTip,
totalLabel,averageLabel;
private JTextField filePathField,
idField,nameField,scoreField,
totalField,averageField;
private JButton skimButton,saveButton,averageButton;
private Record record = new Record();
private MyFile myfile;
private boolean isFirstTime = true; //Whether it is the first time to start the program
public FileIO()
{
//Create the panel of the part of the file path
filePathLabel = new JLabel("文件:");
filePathField = new JTextField(20);
skimButton = new JButton("瀏覽");
skimButton.setBackground(Color.cyan);
JPanel jp_1 = new JPanel();
jp_1.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED,Color.white,Color.black),"文件路徑"));
jp_1.setBackground(Color.orange);
jp_1.add(filePathLabel);
jp_1.add(filePathField);
jp_1.add(skimButton);
//Create the panel of the part of the record input
idLabel = new JLabel("學(xué)號:",JLabel.CENTER);
nameLabel = new JLabel("姓名:",JLabel.CENTER);
scoreLabel = new JLabel("成績:",JLabel.CENTER);
idTip = new JLabel("輸入000--100(不包括000)");
nameTip = new JLabel("字符數(shù)不超過20個");
scoreTip = new JLabel("分?jǐn)?shù)范圍:0-100");
idField = new JTextField();
nameField = new JTextField();
scoreField = new JTextField();
saveButton = new JButton("保存");
saveButton.setBackground(Color.cyan);
JPanel jp_2_1 = new JPanel(new GridLayout(3,3));
jp_2_1.setBackground(Color.orange);
jp_2_1.add(idLabel);
jp_2_1.add(idField);
jp_2_1.add(idTip);
jp_2_1.add(nameLabel);
jp_2_1.add(nameField);
jp_2_1.add(nameTip);
jp_2_1.add(scoreLabel);
jp_2_1.add(scoreField);
jp_2_1.add(scoreTip);
JPanel jp_2_2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
jp_2_2.setBackground(Color.orange);
jp_2_2.add(saveButton);
JPanel jp_2 = new JPanel(new BorderLayout());
jp_2.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED,Color.white,Color.black),"輸入記錄"));
jp_2.setBackground(Color.orange);
jp_2.add(jp_2_1,BorderLayout.CENTER);
jp_2.add(jp_2_2,BorderLayout.SOUTH);
//Create the panel of the part of the average output
totalLabel = new JLabel("總?cè)藬?shù):",JLabel.CENTER);
averageLabel = new JLabel("平均分:",JLabel.CENTER);
totalField = new JTextField();
totalField.setEditable(false);
totalField.setBackground(Color.white);
averageField = new JTextField();
averageField.setEditable(false);
averageField.setBackground(Color.white);
averageButton = new JButton("平均分");
averageButton.setBackground(Color.cyan);
JPanel jp_3_1 = new JPanel(new GridLayout(2,2));
jp_3_1.setBackground(Color.orange);
jp_3_1.add(totalLabel);
jp_3_1.add(totalField);
jp_3_1.add(averageLabel);
jp_3_1.add(averageField);
JPanel jp_3_2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
jp_3_2.setBackground(Color.orange);
jp_3_2.add(averageButton);
JPanel jp_3 = new JPanel(new BorderLayout());
jp_3.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED,Color.white,Color.black),"平均分"));
jp_3.setBackground(Color.orange);
jp_3.add(jp_3_1,BorderLayout.CENTER);
jp_3.add(jp_3_2,BorderLayout.SOUTH);
//Add the three panels to the frame
Container c = getContentPane();
c.add(jp_1,BorderLayout.NORTH);
c.add(jp_2,BorderLayout.CENTER);
c.add(jp_3,BorderLayout.SOUTH);
//Add listener to the button
skimButton.addActionListener(new SkimListener());
saveButton.addActionListener(new SaveListener());
averageButton.addActionListener(new AverageListener());
}
//Inner class to realize the ActionListener of the skimButton
class SkimListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFileChooser jfc = new JFileChooser();
int val = jfc.showOpenDialog(null);
if (val == JFileChooser.APPROVE_OPTION)
filePathField.setText(jfc.getSelectedFile().getAbsolutePath());
}
}
//Inner class to realize the ActionListener of the saveButton
class SaveListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//If you haven't designate a file in the filepath field
if (filePathField.getText().trim().length() == 0)
{
JOptionPane.showMessageDialog(null,"請指定文件!","Error message",JOptionPane.ERROR_MESSAGE);
return;
}
if (!record.setID(idField.getText().trim()) )
idField.setText("");
else if (!record.setName(nameField.getText().trim()) )
nameField.setText("");
else if (!record.setScore(scoreField.getText().trim()) )
scoreField.setText("");
else
{ //If the file is not the one you just worked with
//or it's the first time to start the program,
//you should open a new file!
if (isFirstTime == true || myfile.getFilePath() != filePathField.getText().trim())
{
myfile = new MyFile(filePathField.getText().trim());
isFirstTime = false;
}
//Add record into the file
myfile.addRecord(record);
//Clear the textfield
idField.setText("");
nameField.setText("");
scoreField.setText("");
}
}
}
//Inner class to realize the ActionListener of the averageButton
class AverageListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (filePathField.getText().trim().length() == 0)
{
JOptionPane.showMessageDialog(null,"請指定文件!","Error message",JOptionPane.ERROR_MESSAGE);
return;
}
if (isFirstTime == true || myfile.getFilePath() != filePathField.getText().trim())
{
myfile = new MyFile(filePathField.getText().trim());
isFirstTime = false;
}
int recordNumber = myfile.getRecordNumber();
if (recordNumber == 0)
{
JOptionPane.showMessageDialog(null,"No records in the file!",
"Error message",JOptionPane.ERROR_MESSAGE);
totalField.setText("");
averageField.setText("");
}
else
{
totalField.setText(recordNumber + "");
averageField.setText(myfile.getAverage() + "");
}
}
}
public static void main(String args[])
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
e.printStackTrace();
}
FileIO frame = new FileIO();
frame.setTitle("File I/O");
frame.setSize(500,400);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MyFile
{
private int recordNumber = 0;
private float totalScore = 0;
private String filePath;
private RandomAccessFile file;
private File f;
public MyFile(String path)
{
filePath = path;
f = new File(filePath);
if (filePath != null)
{
try
{
file = new RandomAccessFile(f,"rw");
//If the file is empty , initialize it
if (file.length() == 0)
for (int i = 0 ; i < 100 ; i++ )
{
file.writeInt(0);
byte[] buf = new byte[20];
file.write(buf);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -