當前位置:編程學習大全網 - 編程語言 - java 圖形界面編程 讀取文件操作問題

java 圖形界面編程 讀取文件操作問題

我在妳給的代碼的基礎上進行了妳要求的功能的實現:

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();

}

}

  • 上一篇:數字密碼鎖C語言編程
  • 下一篇:求三年級300字作文
  • copyright 2024編程學習大全網