一个地址薄的小程序,JAVA GUI界面,对文件进行随机读写。

来源:互联网 发布:企业法人电话搜索软件 编辑:程序博客网 时间:2024/04/28 01:02

JAVA语言程序设计(基础篇) 最后的GUI例子,设计GUI小程序的时候可以参考下

 

/*一个地址薄的小程序,JAVA GUI界面,对文件进行随机读写。*
 * 可以作为小程序的范例,包括 界面初始化,界面居中,按钮事件响应*/

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

import javax.swing.*;
import javax.swing.border.*;


public class AddressBook extends JFrame implements ActionListener {
 final static int NAME_SIZE = 32;
 final static int STREET_SIZE = 32;
 final static int CITY_SIZE = 20;
 final static int STATE_SIZE = 2;
 final static int ZIP_SIZE = 5;
 final static int RECORD_SIZE =
  NAME_SIZE + STREET_SIZE + CITY_SIZE + STATE_SIZE + ZIP_SIZE;
 
 private RandomAccessFile raf;
 
 private JTextField jtfName = new JTextField(NAME_SIZE);
 private JTextField jtfStreet = new JTextField(STREET_SIZE);
 private JTextField jtfCity = new JTextField(CITY_SIZE);
 private JTextField jtfState = new JTextField(STATE_SIZE);
 private JTextField jtfZip = new JTextField(ZIP_SIZE);

 private JButton jbtAdd = new JButton("Add");
 private JButton jbtFirst = new JButton("First");
 private JButton jbtNext = new JButton("Next");
 private JButton jbtPrevious = new JButton("Previous");
 private JButton jbtLast = new JButton("Last");
 
 public AddressBook(){
  /*读取文件,如果无法打开或者新建文件就可以结束了*/
  try{
   raf = new RandomAccessFile("address.dat", "rw");
  }
  catch(IOException ex){
   System.out.println("Error:" + ex);
   System.exit(0);
  }  

  /*》》》让界面可以居中显示*/
  Toolkit kit=Toolkit.getDefaultToolkit();
  Dimension screenSize=kit.getScreenSize();
  int x=(screenSize.width-300)/2;
  int y=(screenSize.height-300)/2;
  setLocation(x,y);
  
  /*对界面进行排版,主要的思想是两个合成一个,不断由小到大*/
  JPanel p1 = new JPanel();
  p1.setLayout(new GridLayout(3,1));
  p1.add(new JLabel("Name"));
  p1.add(new JLabel("Stress"));
  p1.add(new JLabel("City"));
  
  JPanel jpState = new JPanel();
  jpState.setLayout(new BorderLayout());
  jpState.add(new JLabel("State"), BorderLayout.WEST);
  jpState.add(jtfState, BorderLayout.CENTER);
  
  JPanel jpZip = new JPanel();
  jpZip.setLayout(new BorderLayout());
  jpZip.add(new JLabel("Zip"), BorderLayout.WEST);
  jpZip.add(jtfZip, BorderLayout.CENTER);
  
  JPanel p2 = new JPanel();
  p2.setLayout(new BorderLayout());
  p2.add(jpState, BorderLayout.WEST);
  p2.add(jpZip, BorderLayout.CENTER);
  
  JPanel p3 = new JPanel();
  p3.setLayout(new BorderLayout());
  p3.add(jtfCity, BorderLayout.WEST);
  p3.add(p2, BorderLayout.CENTER);
  
  JPanel p4 = new JPanel();
  p4.setLayout(new GridLayout(3, 1));
  p4.add(jtfName);
  p4.add(jtfStreet);
  p4.add(p3);
  
  JPanel jpAddress = new JPanel();
  jpAddress.setLayout(new BorderLayout());
  jpAddress.add(p1, BorderLayout.WEST);
  jpAddress.add(p4, BorderLayout.CENTER);
  
  jpAddress.setBorder(new BevelBorder(BevelBorder.RAISED));
  
  JPanel jpButton = new JPanel();
  jpButton.add(jbtAdd);
  jpButton.add(jbtFirst);
  jpButton.add(jbtNext);
  jpButton.add(jbtPrevious);
  jpButton.add(jbtLast);
  
  getContentPane().add(jpAddress, BorderLayout.CENTER);
  getContentPane().add(jpButton, BorderLayout.SOUTH);
  
  jbtAdd.addActionListener(this);
  jbtFirst.addActionListener(this);
  jbtNext.addActionListener(this);
  jbtPrevious.addActionListener(this);
  jbtLast.addActionListener(this);
  
  try{
   if(raf.length() > 0)
    readAddress(0);
  }
  catch(IOException ex){
   ex.printStackTrace();
  }  
 }
 
 /*从文件中读取一条记录*/
 public void readAddress(long position) throws IOException{
  raf.seek(position);
  
  String name = readFixedLengthString(NAME_SIZE, raf);  
  String street = readFixedLengthString(STREET_SIZE, raf); 
  String state = readFixedLengthString(STATE_SIZE, raf);  
  String city = readFixedLengthString(CITY_SIZE, raf);   
  String zip = readFixedLengthString(ZIP_SIZE, raf); 
  
  jtfName.setText(name);
  jtfStreet.setText(street);
  jtfState.setText(state);
  jtfCity.setText(city);
  jtfZip.setText(zip);
 }
 
 /*写一条记录到文件*/
 public void writeAddress() throws IOException{
  raf.seek(raf.length());
  writeFixedLengthString(jtfName.getText(), NAME_SIZE, raf);
  writeFixedLengthString(jtfStreet.getText(), STREET_SIZE, raf);
  writeFixedLengthString(jtfState.getText(), STATE_SIZE, raf);
  writeFixedLengthString(jtfCity.getText(), CITY_SIZE, raf);
  writeFixedLengthString(jtfZip.getText(), ZIP_SIZE, raf);
 }
 
 /*》》》实现ActionListener接口的方法,对按钮的事件的监听*/
 public void actionPerformed(ActionEvent e) {
  try{
   if(e.getSource() == jbtAdd){
    writeAddress();
   }
   else if(e.getSource() == jbtFirst){
    if(raf.length() > 0 ) readAddress(0);
   }
   else if(e.getSource() == jbtNext){
    long currentPosition = raf.getFilePointer();
    if(currentPosition < raf.length())
     readAddress(currentPosition);
   }
   else if(e.getSource() == jbtPrevious){
    long currentPosition = raf.getFilePointer();
    /*为什么要乘以2?因为一个字符占2字节*/
    if(currentPosition - 2 * RECORD_SIZE > 0)
     readAddress(currentPosition - 2 * 2 * RECORD_SIZE);
    else
     readAddress(0);
   }
   else if(e.getSource() == jbtLast){
    long lastPosition = raf.length();
    if(lastPosition > 0)
     readAddress(lastPosition - 2 * RECORD_SIZE);
   }
  }
  catch(IOException ex){
   System.out.println("Error:" + ex);
  }
 }
 
 /*将字符串中固定长度的字符写到文件*/
 public static void writeFixedLengthString(String s, int size,
   DataOutput out) throws IOException{
  char[] chars = new char[size];
  
  s.getChars(0, Math.min(s.length(), chars.length), chars, 0);
  for(int i = Math.min(s.length(), size); i < chars.length; i++)
   chars[i] = ' ';
  
  out.writeChars(new String(chars));
 }
 
 /*从文件读出固定长度的字符*/
 public static String readFixedLengthString(int size,
   DataInput in) throws IOException{
  char[] chars = new char[size];
  
  for(int i = 0; i < size; i++)
   chars[i] = in.readChar();
  
  return new String(chars);
 }

 public static void main(String[] args){
  /*》》》界面初始化*/
  AddressBook frame = new AddressBook();
  frame.pack();
  frame.setTitle("AddressBook");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
 }
}