/** |
* 功能:键盘事件监听简单例子 |
* 1.在面板上画一个小球,让它能通过键盘上下左右的动 |
*/ |
package com.zjr; |
import java.awt.*; |
import javax.swing.*; |
import java.awt.event.*; |
public class Test1 extends JFrame{ |
MyPanel mp= null ; |
public static void main(String[] args) { |
// TODO Auto-generated method stub |
Test1 test1= new Test1(); |
} |
public Test1(){ |
|
mp= new MyPanel(); |
|
this .add(mp); |
|
this .addKeyListener(mp); |
|
this .setSize( 400 , 300 ); |
this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
this .setVisible( true ); |
} |
|
} |
class MyPanel extends JPanel implements KeyListener |
{ |
int x= 10 ; |
int y= 10 ; |
//重写paint方法 |
public void paint(Graphics g){ |
|
super .paint(g); |
|
g.fillOval(x, y, 12 , 12 ); |
//g.drawRect(50, 50, 10,5); |
|
} |
@Override |
public void keyTyped(KeyEvent e) { |
// TODO Auto-generated method stub |
//System.out.println("被按下的是"+e.getKeyChar()); |
|
|
|
} |
@Override |
public void keyPressed(KeyEvent e) { |
// TODO Auto-generated method stub |
if (e.getKeyCode()==KeyEvent.VK_UP){ |
y-= 10 ; |
|
} else if (e.getKeyCode()==KeyEvent.VK_DOWN){ |
y+= 10 ; |
} else if (e.getKeyCode()==KeyEvent.VK_LEFT){ |
|
x-= 10 ; |
} else if (e.getKeyCode()==KeyEvent.VK_RIGHT){ |
|
x+= 10 ; |
} |
this .repaint(); |
|
} |
@Override |
public void keyReleased(KeyEvent e) { |
// TODO Auto-generated method stub |
|
} |
|
} |
初级程序员
by: lbq 发表于:2017-09-13 19:22:42 顶(0) | 踩(0) 回复
lihai
回复评论