package keylogger;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
//创建窗口
public class readFile extends JFrame{
JPanel JPanel1;
JLabel reading;
public readFile(){
this.setSize(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Keylogger!");
this.setVisible(true);
JPanel1 = new JPanel();
add(JPanel1);
reading = new JLabel();//感觉应该在这里添加输入的内容,但是不知道怎么调用下面读取的内容
JPanel1.add(reading);
}
//读取TXT文件
public static void readFileMessage(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("按顺序读取文件的内容如下:");
reader = new BufferedReader(new FileReader(file));
String string = null;
int line = 1;
// 按行读取内容,直到读入null则表示读取文件结束
while ((string = reader.readLine()) != null) {
System.out.println("line " + line + ": " + string);
line++;
}
reader.close();
}
catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
//输出结果
public static void main(String[] args) {
String fileName = "D:/temp/test.txt";
System.out.println("输出文件的内容:");
readFile.readFileMessage(fileName);
readFile r = new readFile();
}
}
这是一个读取TXT文件的程序,我想把读取的内容显示在窗体中,但是不知道该如何编写,
希望各位大神能给我一些帮助,感激不尽!!
有没有具体一点的。。。
你从txt文件中读出来的内容不是放到string中去了吗?
JLable是用来显示标签的,那你得给它个文本才能显示吧。。
调用reading.setText(string)方法,这不就行了?
用setText("输出内容")方法和getText()方法。只能帮你到这了。
直接用J Text Area,将读取内容append这个组件上去就可以了,在构造函数中读取并显示。有问题私信联系。
既然会用bufferedreader,那就用bufferwriter就好了