日期:2014-05-20  浏览次数:20620 次

java键盘监视问题
Java code

package person.move;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
 
 

import javax.swing.*;
 

public class KeyListener1 extends   JFrame 
implements   ActionListener   {
    public JButton b1=new JButton("确定"); 
 
    public int x;
    public int y;
    public JPanel jpanel=new JPanel();
    public    JFrame frame = new JFrame("键盘响应");
    public void _int()
    
    { this.x=110;
    this.y=180;
    
         frame.setLocation(300, 300);
         frame.setSize(300, 300);
         frame.setResizable(false);
         frame.setVisible(true);
         frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
         
         frame.setContentPane(jpanel);
         jpanel.setBackground(Color.GREEN);
         
         jpanel.setLayout(null);
         jpanel.add(b1);
          
         b1.setBounds(this.x, this.y, 80, 25);
         
         b1.addActionListener(this); 
         //addMouseListener(new Move().m);
        
         
         jpanel.setFocusable(true);
         jpanel.addKeyListener(new KeyAdapter(){
             public void keyPressed(KeyEvent e){            
                 if(e.getKeyCode()==KeyEvent.VK_RIGHT)
                      b1.setBounds(++x, y, 80, 25);        
                   if(e.getKeyCode()==KeyEvent.VK_LEFT)
                      b1.setBounds(--x,y, 80, 25); 
                   if(e.getKeyCode()==KeyEvent.VK_UP)
                      b1.setBounds(x,--y, 80, 25); 
                  if(e.getKeyCode()==KeyEvent.VK_DOWN)
                      b1.setBounds(x,++y, 80, 25);
                 
           }
         });

         }
    
        
     public static void main(String[] args){
         KeyListener1 move=new KeyListener1();
         
         move._int();
         
      }


 

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        
    }
}



程序是做了一个键盘监视,用上下左右键可以移动按钮的位置,刚进去程序是键盘监听可以实现,为什么我单击一下按钮后,键盘监听就不能执行了呢?是不是按钮的响应屏蔽了键盘监听啊?该怎么做才不会这样子啊?


------解决方案--------------------
你点击鼠标后,焦点就未必落在你所监听的控件上面了,那么这个控件就监听不到任何键盘事件了,因为这些事件全都发给“焦点所在控件”了。

一种解决方案是,提升监听的级别,比如直接重写JFrame的processKeyEvent()函数(它相当于事件的分发者),不过要注意处理完毕后要调用super.processKeyEvent();不然全都没反应了。
------解决方案--------------------
b1.addActionListener(this);

jpanel.setFocusable(true); //不是给jpanel加监听 是给b1加监听
jpanel.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
b1.setBounds(++x, y, 80, 25);
if(e.getKeyCode()==KeyEvent.VK_LEFT)
b1.setBounds(--x,y, 80, 25); 
if(e.getKeyCode()==KeyEvent.VK_UP)
b1.setBounds(x,--y, 80, 25); 
if(e.getKeyCode()==KeyEvent.VK_DOWN)
b1.setBounds(x,++y, 80, 25);

}
});

按照你说的 键盘才是事件源,但是你给jpanel加了键盘监听.............
另外给b1加了个actionListener完全没有必要。

把b1.addActionListener(this); 这一句去掉
jpanel.addKeyListener改成b1.addKeyListener


------解决方案--------------------
探讨
我是监听jpanel里面的键盘响应,给jpanel加监听不对吗?

------解决方案--------------------
嗯,同意楼上说的。面板失去焦点了才会没反应的。



不过如果是我,会加按钮做监听而不是面板.......
------解决方案--------------------