一个地址薄的小程序,JAVA GUI界面,对文件进行随机读写。
2008-12-30 14:59
736 查看
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);
}
}
/*一个地址薄的小程序,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);
}
}
相关文章推荐
- 一个简单的mfc单页界面文件读写程序(MFC 程序入口和执行流程)
- 自定义字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,能够在读取的每行前面都加上有行号和冒号。
- java小程序:对一个文件进行加密
- java poi对同一个excel文件进行读写
- JAVA程序:UTF码进行文件的读写
- JAVA--第十周作业编写之一个Teacher类负责给出算术题目,随机给出两个整数并进行运算,并判断回答者的答案是否正确;编写一个GUI类ComputerFrame,回答者可以通过GUI看到题目并给出
- 一个简单的mfc单页界面文件读写程序(MFC 程序入口和执行流程)
- 《Java examples in a nutshell》书中一个关于列出某一目录下的所有目录与文件的程序源代码
- 编写一个程序,将 d:\java目录下的所有.java 文件复制到d:\jad目录下,并将原来文件的扩展名从.java 改为.jad。
- Java程序返回一个list类型而前台的的flex对其进行读取
- 一个Java文件扫描 统计程序
- 使用 jquery 的 上传文件插件 uploadify 3.1 配合 java 来做一个简单的文件上次功能。并且在界面上有radio 的选择内容也要上传
- 一个简单的matlab图形界面程序GUI
- 关于java读文件计算一个方法执行时间的程序的学习
- java轮子-使用springboot做一个带界面的web程序
- Java 如何对文件进行多个Object对象流的读写操作
- java中RandomAccessFile 类对文件进行读写
- 在对文件进行随机读写,RandomAccessFile类,如果提高其效率
- 一个简单的读写文件程序-适用于MTK平台资源管理
- 关于用java创建一个.properties文件,程序结果为空的问题