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文件的程序,我想把讀取的內容顯示在窗體中,但是不知道該如何編寫,
希望各位大神能給我一些幫助,感激不盡!!!
其實稍微修改下,可以不需要鍵盤響應事件的,直接用JText,然後提供一個保存按鈕就可以了。修改下你的代碼如下:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
public class KeyBoardListener extends JFrame implements ActionListener {
private JTextArea text_receiver;
private JButton saveBtn;
JPanel jPanel;
JLabel UserName, PassWord;
char charA;
public KeyBoardListener() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Keylogger");
this.addKeyListener(new MyKeyListener());
this.text_receiver = new JTextArea();
this.text_receiver.setEditable(true);
this.getContentPane().add(new JScrollPane(this.text_receiver));
this.text_receiver.append("請通過鍵盤輸入信息,下面將會回顯您輸入的信息:");
JToolBar toolBar = new JToolBar();
this.getContentPane().add(toolBar, "South");
saveBtn = new JButton("保存");
saveBtn.addActionListener(this);
toolBar.add(saveBtn);
this.setSize(400, 400);
this.setVisible(true);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
KeyBoardListener m = new KeyBoardListener();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("保存"))
{
//保存信息
String input = this.text_receiver.getText();
//寫入到文件中
System.out.println("input :"+input);
}
}
}
你可以在保存按鈕事件裡面保存用戶在文本框裡輸入的信息。注意設置窗體大小的代碼必須在所有的元素都初始化完成後,放在最後。就是
this.setSize(400, 400);
this.setVisible(true);必須放在最後,否則頁面組件初始化時是看不見的。