[java]代码库
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JianPan extends JFrame{
Wdmb mb=null;
public static void main(String[] args){
JianPan lx=new JianPan();
}
public JianPan(){
mb=new Wdmb();
this.add(mb);
this.addKeyListener(mb);
this.setSize(400,300);
this.setLocation(300,280);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class Wdmb extends JPanel implements KeyListener{
//int x=50,y=60;
public void paint(Graphics g){
super.paint(g);
//g.fillRect(x,y,30,30);
}
public void keyTyped(KeyEvent e){
//有字符输出的函数
}
public void keyPressed(KeyEvent e){
System.out.println("键盘被按下");
//System.out.println((char)e.getKeyCode());//将按下的键转换成字符 否则是键盘每个键代表的数字
// if(e.getKeyCode()==KeyEvent.VK_DOWN){
// System.out.println("下");
// //y+=10;
// }
// else if(e.getKeyCode()==KeyEvent.VK_UP){
// System.out.println("上");
// //y--;
// }
// else if(e.getKeyCode()==KeyEvent.VK_LEFT){
// System.out.println("左");
// //x--;
// }
// else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
// System.out.println("右");
// //x+=5;
// }
// this.repaint();
}
public void keyReleased(KeyEvent e){
//按键抬起
}
}
初级程序员
by: 云代码会员 发表于:2016-06-09 23:01:25 顶(0) | 踩(0) 回复
回复评论