该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
因为太长百度知道不好使只能来贴吧了TAT
这个错误比较诡异,第一次javac会提示出错,可是再运行一次就没问题,当然,java运行后的结果也不是书上说的那样,把class文件删掉之前的错误才会再次出现。话不多说,上代码——
Example10_21.java
public class Example10_21
{
public static void main(String[] args)
{
new CommFrame();
}
}
InputArea.java
import java.io.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;
public class InputArea extends JPanel implements ActionListener
{
File f=null;
RandomAccessFile out;
Box baseBox,boxV1,boxV2;
JTextField name,email,phone;
JButton button;
InputArea(File f)
{
setBackground(Color.cyan);
this.f=f;
name=new JTextField(12);
email=new JTextField(12);
phone=new JTextField(12);
button=new JButton("录入");
button.addActionListener(this);
boxV1=Box.createVerticalBox();
boxV1.add(new JLabel("输入姓名"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new JLabel("输入email"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new JLabel("输入电话"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new JLabel("单击录入"));
boxV2=Box.createVerticalBox();
boxV2.add(name);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(email);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(phone);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(button);
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV2);
add(baseBox);
}
public void actionPerformed(ActionEvent e)
{
try
{
RandomAccessFile out=new RandomAccessFile(f,"rw");
if(f.exists())
{
long length=f.length();
out.seek(length);
}
out.writeUTF("姓名:"+name.getTxet());
out.writeUTF("eamil:"+email.getText());
out.writeUTF("电话:"+phone.getText());
out.close();
}
catch(IOException ee){}
}
}
CommFrame.java
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CommFrame extends JFrame implements ActionListener
{
File file=null;
JMenuBar bar;
JMenu fileMenu;
JMenuItem inputMenuItem,showMenuItem;
JTextArea show; //显示信息
InputArea inputMessage; //录入信息
CardLayout card=null; //卡片式布局
JPanel pCenter;
CommFrame()
{
file=new File("通讯录.TXT");
inputMenuItem=new JMenuItem("录入");
showMenuItem=new JMenuItem("显示");
bar=new JMenuBar();
fileMenu=new JMenu("菜单选项");
fileMenu.add(inputMenuItem);
fileMenu.add(showMenuItem);
bar.add(fileMenu);
setJMenuBar(bar);
inputMenuItem.addActionListener(this);
showMenuItem.addActionListener(this);
inputMessage=new InputArea(file);
show=new JTextArea(12,20);
card=new CardLayout();
pCenter=new JPanel();
pCenter.setLayout(card);
pCenter.add("inputMenuItem",inputMessage);
pCenter.add("showMenuItem",show);
add(pCenter,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(100,50,420,380);
validate();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==inputMenuItem)
card.show(pCenter,"inputMenuItem");
else if(e.getSource()==showMenuItem);
{
int number=1;
show.setText(null);
card.show(pCenter,"showMenuItem");
try
{
RandomAccessFile in=new RandomAccessFile(file,"r");
String name=null;
while((name=in.readUTF())!=null)
{
show.append("\n"+number+" "+name);
show.append("\t "+in.readUTF()); //读取email
show.append("\t"+in.readUTF()); //读取phone
show.append("\n---------------------------------");
number++;
}
in.close();
}
catch (Exception ee){}
}
}
}
三个文件做出来的(不知道我这个说法准确不准确),挺难的,我检查了好几遍眼睛都花了也没发现哪里不对,老师也没抽不出时间。还是我太无知了,只能在这里请求各位大佬来帮帮我了(跪谢)!
(ps:第一章图片是我做出来的效果,第二张图片是书上说的应该有的样子)