public KeyBoardListener() throws FileNotFoundException {
this.setTitle("监听键盘事件"); this.setSize(800, 600); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.addKeyListener(new KeyAdapter() { @Override public void keyReleased(KeyEvent e) { char ch = e.getKeyChar(); int num = e.getKeyCode() ;
System.out.println("键盘="+ch+",代表数字="+num); }
}); }
public static void main(String[] args) throws FileNotFoundException { new KeyBoardListener(); } }
public class KeyDemo extends JFrame{ this.setSize(400,400); this.setVisible(true); } public class MyListener extends KeyAdapter{ public void keyPressed(KeyEvent arg0) { System.out.print(arg0.getKeyCode()); } } public static void main(String[] aregs){ new KeyDemo(); } }
package com.tmzs.pc.jsoup;import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;import javax.swing.JFrame;public class KeyBoardListener extends JFrame {
public KeyBoardListener() throws FileNotFoundException {
this.setTitle("监听键盘事件");
this.setSize(800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.addKeyListener(new KeyAdapter() { @Override
public void keyReleased(KeyEvent e) {
char ch = e.getKeyChar();
int num = e.getKeyCode() ;
System.out.println("键盘="+ch+",代表数字="+num);
}
});
}
public static void main(String[] args) throws FileNotFoundException {
new KeyBoardListener();
}
}
this.setVisible(true);
}
public class MyListener extends KeyAdapter{
public void keyPressed(KeyEvent arg0) {
System.out.print(arg0.getKeyCode());
}
}
public static void main(String[] aregs){
new KeyDemo();
}
}