java 键盘事件
原创
©著作权归作者所有:来自51CTO博客作者scx_white的原创作品,请联系作者获取转载授权,否则将追究法律责任
package 键盘事件;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
KeyEvent1 win=new KeyEvent1();
win.setTitle("键盘事件");
}
}
package 键盘事件;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class KeyEvent1 extends JFrame implements KeyListener{
JTextArea textInput,textShow;
KeyEvent1(){
setLayout(new FlowLayout());
init();
setBounds(10,10,446,236);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init()
{
textInput=new JTextArea(10,15);
textShow=new JTextArea(10,15);
add(new JScrollPane(textInput));
add(new JScrollPane(textShow));
textInput.addKeyListener(this);
}
public void keyPressed(KeyEvent e){
textShow.append(KeyEvent.getKeyText(e.getKeyCode())+"\n");
}
public void keyTyped(KeyEvent e){
}
public void keyReleased(KeyEvent e){
}
}