package 键盘事件;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
public class Test extends JFrame{
public static void main(String[] args) {
Test test=new Test();
test.loading();
}
int width=500;
int height=250;
public void loading()
{
setSize(width, height);
setLocationRelativeTo(null);
//关闭事件
setDefaultCloseOperation(3);
//用户不能调整窗口大小
setResizable(false);
//标题
setTitle("键盘事件");
//窗口可见
setVisible(true);
this.addKeyListener(new demo());
}
}
package 键盘事件;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class demo implements KeyListener{
@Override
public void keyPressed(KeyEvent e) {
int key;
key=e.getKeyCode();
System.out.println(key);
}
/*
@Override
public void keyReleased(KeyEvent e) {
int key;
key=e.getKeyCode();
System.out.println(key);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}*/
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
?
|