用java如何将一个键盘发生的事件记录到记事本里呀?我代码如下:但不知出错在哪里啦?
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyEventTest extends JFrame {
private String output;
public KeyEventTest(){
super( "显示键盘事件演示程序 ");
addKeyListener(new KeyEventHandler());
setSize(200,200);
show();
}
public static void main(String args[]){
KeyEventTest app=new KeyEventTest();
}
class KeyEventHandler implements KeyListener{
private char output;
private String output1;
public void keyPressed(KeyEvent e){
output1= "键盘按下: "+e.getKeyText(e.getKeyCode());
}
public void keyTyped(KeyEvent e){
output=e.getKeyChar();
}
public void keyReleased(KeyEvent e){}
private void show() throws IOException {
File file1=new File( "e:\\keyrecord.txt ");
try{
FileOutputStream fout=new FileOutputStream(file1);
fout.write(output);
fout.close();
}
catch (FileNotFoundException e)
{ System.out.println(e);}
catch(IOException e)
{ System.out.println(e);}
}
}
}
------解决方案--------------------
帮顶
------解决方案--------------------
KeyEventTest 中的output和KeyEventHandler 中的output分别指向不同对象,在需要输出时自然没有内容输出了。。。