Liny_@NotePad

沉迷ACG中

AWT事件监听作业学习例子(4) - KeyEvent

YOYO posted @ 2008年8月12日 04:12 in 【Java SE】 with tags java AWT ActionEvent 监听 , 2538 阅读
【事件执行情况】组件中发生击键时执行。
【传递的接口名】KeyListener
【需要实现方法】keyPressed - 按下某个键时调用此方法;keyReleased - 释放某个键时调用此方法;keyTyped - 键入某个键时调用此方法
【样例】按下上下左右方向时移动矩形/显示键入的键(非上下左右时)

【代码】

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class CallByKey {
  6.  
  7.         /**
  8.          * @author Linyq.
  9.          */
  10.         public static void main(String[] args) {
  11.                 JFrame.setDefaultLookAndFeelDecorated(true);
  12.                 MyFrame frm = new MyFrame();
  13.                 frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14.         }
  15.  
  16. }
  17.  
  18. class MyFrame extends JFrame{
  19.        
  20.         public MyFrame(){
  21.                 this.setTitle("KeyEvent 测试");
  22.                 this.setSize(260,240);
  23.                 this.setLayout(new BorderLayout());
  24.                
  25.                 final JLabel label = new JLabel("等待操作....");
  26.                 label.setLayout(null);
  27.                 this.getContentPane().add(label,"North");
  28.                
  29.                 final DrawPanel panel = new DrawPanel();
  30.                 panel.setSize(200,200);
  31.                 panel.setBackground(Color.DARK_GRAY);
  32.                 panel.setLayout(new FlowLayout(FlowLayout.CENTER));
  33.                 this.getContentPane().add(panel,"Center");
  34.                
  35.                 //      用于接收键盘事件(击键)的侦听器接口。
  36.                 this.addKeyListener(new KeyListener(){
  37.  
  38.                         public void keyPressed(KeyEvent e) {
  39.                                 //      按下某个键时调用此方法
  40.                                 if(e.getKeyCode()==KeyEvent.VK_UP){
  41.                                         panel.moveItem(0,-10);
  42.                                 }
  43.                                 if(e.getKeyCode()==KeyEvent.VK_DOWN){
  44.                                         panel.moveItem(0,10);
  45.                                 }
  46.                                 if(e.getKeyCode()==KeyEvent.VK_LEFT){
  47.                                         panel.moveItem(-10,0);
  48.                                 }
  49.                                 if(e.getKeyCode()==KeyEvent.VK_RIGHT){
  50.                                         panel.moveItem(10,0);
  51.                                 }
  52.                         }
  53.  
  54.                         public void keyReleased(KeyEvent e) {
  55.                                 //      释放某个键时调用此方法
  56.                                 label.setText("释放了刚刚按下的键");
  57.                         }
  58.  
  59.                         public void keyTyped(KeyEvent e) {
  60.                                 //      键入某个键时调用此方法
  61.                                 label.setText("刚刚键入了"+e.getKeyChar() + "键");
  62.                         }
  63.                        
  64.                 });
  65.                
  66.                 this.setVisible(true);
  67.         }
  68.        
  69. }
  70.  
  71. class DrawPanel extends JPanel{
  72.        
  73.         private int x = 120;
  74.         private int lx = x;
  75.         private int y = 80;
  76.         private int ly = y;
  77.         private int w = 10;
  78.         private int h = 10;
  79.        
  80.         public void paintComponent(Graphics g){
  81.                 super.paintChildren(g);
  82.                 g.clearRect(lx-1, ly-1, w+2, h+2);
  83.                 g.drawRect(x,y,w,h);
  84.                 this.lx = this.x;
  85.                 this.ly = this.y;
  86.         }
  87.        
  88.         public void moveItem(int x,int y){
  89.                 this.x += x;
  90.                 if(this.x>240) this.x = 0;
  91.                 if(this.x<0) this.x = 240;
  92.                 this.y += y;
  93.                 if(this.y>175) this.y = 0;
  94.                 if(this.y<0) this.y = 175;
  95.                 repaint();
  96.         }
  97.        
  98. }

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter