package itat;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.Box;
public class Example10_12 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new CommFrame();
}
}
class InputArea extends Panel implements ActionListener{
File f=null;
RandomAccessFile out;
Box baseBox,boxV1,boxV2;
TextField name,email,phone;
Button button;
InputArea(File f) {
// TODO Auto-generated constructor stub
setBackground(Color.cyan);
this.f=f;
name=new TextField(12);
email=new TextField(12);
phone=new TextField(12);
button=new Button("录入");
button.addActionListener(this);
boxV1=Box.createVerticalBox();
//boxV1=Box.createHorizontalBox() ;
boxV1.add(new Label("输入姓名"));
boxV1.add(Box.createVerticalStrut(8));
//boxV1.add(Box.createHorizontalGlue());
boxV1.add(new Label("输入email"));
boxV1.add(Box.createVerticalStrut(8));
// boxV1.add(Box.createVerticalGlue(8));
boxV1.add(new Label("输入电话"));
boxV1.add(Box.createVerticalStrut(8));
// boxV1.add(Box.createVerticalGlue(8));
boxV1.add(new Label("单击录入"));
boxV2=Box.createVerticalBox();
boxV2.add(name);
boxV2.add(Box.createHorizontalStrut(8));
boxV2.add(email);
boxV2.add(Box.createHorizontalStrut(8));
boxV2.add(phone);
boxV2.add(Box.createHorizontalStrut(8));
boxV2.add(button);
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV2);
add(baseBox);
//add(boxV1);
//add(boxV2);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
RandomAccessFile out=new RandomAccessFile(f,"rw");
if(f.exists()){
long length=f.length();
out.seek(length);
}
out.writeUTF("姓名:"+name.getText());
out.writeUTF("email:"+email.getText());
out.writeUTF("电话:"+phone.getText());
out.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException ee) {
// TODO Auto-generated catch block
ee.printStackTrace();
}
}}
class CommFrame extends Frame implements ActionListener{
File file=null;
MenuBar bar;
Menu fileMenu;
MenuItem login,xianshi;
TextArea show;
InputArea inputMessage;
CardLayout card=null;
Panel pCenter;
CommFrame(){
file=new File("tongxunlu.txt");
login=new MenuItem("录入");
xianshi=new MenuItem("显示");
bar=new MenuBar();
fileMenu=new Menu("菜单选项");
fileMenu.add(login);
fileMenu.add(xianshi);
bar.add(fileMenu);
setMenuBar(bar);
login.addActionListener(this);
xianshi.addActionListener(this);
inputMessage=new InputArea(file);
show=new TextArea(12,20);
card=new CardLayout();
pCenter=new Panel();
pCenter.setLayout(card);
pCenter.add("录入",inputMessage);
pCenter.add("显示",show);
add(pCenter,BorderLayout.CENTER);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);
setBounds(100,50,420,380);
validate();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==login){
card.show(pCenter,"录入");
}else if(e.getSource()==xianshi){
int number=1;
show.setText(null);
card.show(pCenter,"显示");
try {
RandomAccessFile in=new RandomAccessFile(file,"r");
String name=null;
while((name=in.readUTF())!=null){
show.append("/n"+number+""+name );
show.append( in.readUTF() );
show.append( in.readUTF() );
show.append("/n--------------------- ");
number++;
}
in.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException eee) {
// TODO Auto-generated catch block
eee.printStackTrace();
}
}
}
}