三个文件:
public class Person {
private String name;
private String memo;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMom() {
return memo;
}
public void setMom(String mom) {
this.memo = mom;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString(){
return "the person' information is:"+name+"\n"+memo+"\n"+age;
}
}
import java.util.*;
import java.io.*;
public class ReadPersons {
public static Vector getInfo(){
Vector v =new Vector();
try{
File f =new File("info.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s = br.readLine();
while(s!=null){
Person p =new Person();
p.setName(s);
s=br.readLine();
p.setMom(s);
s=br.readLine();
p.setAge(Integer.parseInt(s));
v.add(p);
s=br.readLine();
}
br.close();
fr.close();
}catch(IOException e)
{
e.printStackTrace();
}
return v;
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
public class GuiView extends JFrame implements ActionListener {
public JTextField tf;
public JLabel l1;
public JLabel l2;
public JLabel l3;
public JButton jb1;
public JButton jb2;
public int jb1flg = 2;
public int jb2flg = 0;
Vector v = ReadPersons.getInfo();
Person p0;
public GuiView(){
tf = new JTextField("请点击按钮读取人员信息数据库");
tf.setEditable(false);
l1 = new JLabel("姓名:");
l2 = new JLabel("名言:");
l3 = new JLabel("年龄:");
jb1 = new JButton("上一条");
jb1.addActionListener(this);
jb1.setActionCommand("上一条");
jb2 = new JButton("下一条");
jb2.addActionListener(this);
jb2.setActionCommand("下一条");
JPanel p1 =new JPanel(new GridLayout(3,1));
p1.add(l1);
p1.add(l2);
p1.add(l3);
JPanel p2 = new JPanel();
p2.add(jb1);
p2.add(jb2);
this.add(tf,"North");
this.add(p1);
this.add(p2,"South");
this.setTitle("人员参看信息程序");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400, 400);
this.setLocation(300, 300);
this.setVisible(true);
}
public static void main(String[] args) {
new GuiView();
}
public void actionPerformed(ActionEvent e){
String s =e.getActionCommand();
if(s.equals("上一条")){
p0=(Person)v.get(jb1flg);
jb1flg=(jb1flg==0)?2:(jb1flg-1);
l1.setText(p0.getName());
l2.setText(p0.getMom());
l3.setText(""+p0.getAge());
System.out.println(p0.toString());
}else if(s.equals("下一条")){
p0=(Person)v.get(jb2flg);
jb2flg=(jb2flg+1)%3;
l1.setText(p0.getName());
l2.setText(p0.getMom());
l3.setText(""+p0.getAge());
System.out.println(p0.toString());
}
}
}