首 页 行业资讯 新车 试驾评测 养车用车 车型库

java 图形界面编程 读取文件操作问题

发布网友 发布时间:2022-04-22 22:52

我来回答

1个回答

热心网友 时间:2023-10-07 07:04

我在你给的代码的基础上进行了你要求的功能的实现:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

import javax.swing.*;

class guiView extends JFrame implements ActionListener {
JButton shoutiao;
JButton motiao;
JButton xiatiao;
JButton shangtiao;
JLabel xuehao;
JLabel xingming;
JLabel xingbie;
JLabel shengri;
JLabel jiangxuejin;
JLabel jianli;
JTextField JT1;
JTextField JT2;
JTextField JT3;
JTextField JT4;
JTextField JT5;
JTextField JT6;

JFrame f;

int flag = 1; //用于标记当前行的位置
int sum = 0; //用户统计文件的总行数

public void init() {
f = new JFrame("学生信息浏览窗口");
JPanel j1 = new JPanel();
JPanel j2 = new JPanel();
JPanel j3 = new JPanel();
JPanel j4 = new JPanel();
JPanel j5 = new JPanel();
JPanel j6 = new JPanel();
JPanel j7 = new JPanel();
JPanel j8 = new JPanel();
JPanel j9 = new JPanel();
JPanel j10 = new JPanel();

FlowLayout flow = new FlowLayout();
j1.setLayout(flow);
j2.setLayout(flow);
j3.setLayout(flow);
j4.setLayout(flow);
j5.setLayout(flow);
j6.setLayout(flow);
j7.setLayout(flow);
j10.setLayout(flow);
f.getContentPane().setLayout(new BorderLayout());
j8.setLayout(new BorderLayout());
j9.setLayout(new BorderLayout());

shoutiao = new JButton("首条");
shoutiao.addActionListener(this);//每个按钮都添加事件*
shoutiao.setActionCommand("first");
motiao = new JButton("末条");
motiao.addActionListener(this);//每个按钮都添加事件*
motiao.setActionCommand("last");
xiatiao = new JButton("下条");
xiatiao.addActionListener(this);//每个按钮都添加事件*
xiatiao.setActionCommand("next");
shangtiao = new JButton("上条");
shangtiao.addActionListener(this);//每个按钮都添加事件*
shangtiao.setActionCommand("previous");

xuehao = new JLabel("学号 :");
xingming = new JLabel("姓名 :");
xingbie = new JLabel("性别 :");
shengri = new JLabel("生日 :");
jiangxuejin = new JLabel("奖学金:");
jianli = new JLabel("简历 :");
JT1 = new JTextField(12);
JT2 = new JTextField(12);
JT3 = new JTextField(12);
JT4 = new JTextField(12);
JT5 = new JTextField(12);
JT6 = new JTextField(12);

j1.add(xuehao);
j1.add(JT1);
j2.add(xingming);
j2.add(JT2);
j3.add(xingbie);
j3.add(JT3);
j4.add(shengri);
j4.add(JT4);
j5.add(jiangxuejin);
j5.add(JT5);
j6.add(jianli);
j6.add(JT6);
j7.add(shoutiao);
j7.add(shangtiao);
j7.add(xiatiao);
j7.add(motiao);
j8.add("North", j1);
j8.add("Center", j2);
j8.add("South", j3);
j9.add("North", j4);
j9.add("Center", j5);
j9.add("South", j6);
j10.add(j8);
j10.add(j9);
f.getContentPane().add("Center", j10);
f.getContentPane().add("South", j7);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500, 200);
f.setVisible(true);

getData();//初始化显示文件的第一行
}

//对点击首行,上一行,下一行,最后一行按钮触发的操作进行事件响应处理
public void actionPerformed(ActionEvent e) {
String event = e.getActionCommand();
if (event.equals("first")) {
flag = 1;
getData();
} else if (event.equals("last")) {
flag = sum;
getData();
} else if (event.equals("next")) {
if(flag<sum){
flag++;
};// 考虑读到文件末尾的情况,再选择下一条仍是最后一条
getData();
} else if (event.equals("previous")) {
if (flag > 1) {
flag--;
}// 考虑读到文件第一行的情况,再选择上一条仍是第一条
getData();
} else {
System.err.println("error!");
System.exit(-1);
}
}

//通过flag标记当前读到了文件的第几行。然后循环读至该行
public void getData() {
File file = new File("F:/java/data.txt");
String str=null;
StringTokenizer st;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
for(int i=0;i<flag;i++){
str = br.readLine();
}
if (str == null) {
System.err.println("no data!");
} else {
st = new StringTokenizer(str);//每行信息的6个字段分别用空格隔开的
this.JT1.setText(st.nextToken());
this.JT2.setText(st.nextToken());
this.JT3.setText(st.nextToken());
this.JT4.setText(st.nextToken());
this.JT5.setText(st.nextToken());
this.JT6.setText(st.nextToken());
}
br.close();//一定要关闭资源
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//统计文件总共有多少行,方便判断是否已到达文件末尾,统计的行数放在sum里面
public void countLines() {
File file = new File("F:/java/data.txt");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
while (br.readLine() != null) {
sum++;
}
br.close();//一定要关闭资源
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] s) {
guiView c = new guiView();
c.countLines();//统计文件行数
c.init();
}

}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com