代码如下:import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class test extends JFrame implements KeyListener,ActionListener{ JButton a1;
JButton a2;
JButton b1;
JButton b2;
JPanel jp;
public test()
{
a1=new JButton("a1");
a2=new JButton("a2");
b2=new JButton("b1");
b1=new JButton("b2");
jp=new JPanel();
jp.setBorder(BorderFactory.createEtchedBorder());

getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT,30,30));
getContentPane().add(a1);
getContentPane().add(a2);
getContentPane().add(jp);
jp.add(b1);
jp.add(b2);
pack();

setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a1.addActionListener(this);
a2.addActionListener(this);


b1.addKeyListener(this);
b2.addKeyListener(this);
getContentPane().addKeyListener(this);
jp.addKeyListener(this);    
//到底要怎么加这个键盘监听啊??  我现在按键都没反应啊...
}

public static void main(String[] args) {
// TODO 自动生成方法存根
new test(); } public void keyPressed(KeyEvent e) {
// TODO 自动生成方法存根
int keyCode=e.getKeyCode();
switch(keyCode)
{
case KeyEvent.VK_UP:
b1.setBackground(Color.BLUE);
break;
case KeyEvent.VK_DOWN:
b2.setBackground(Color.RED);
break;
case KeyEvent.VK_LEFT:
b1.setBackground(Color.CYAN);
break;
case KeyEvent.VK_RIGHT:
b2.setBackground(Color.BLACK);
break;
default:
}
} public void keyReleased(KeyEvent e) {
// TODO 自动生成方法存根

} public void keyTyped(KeyEvent e) {
// TODO 自动生成方法存根

} public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
if(e.getSource()==a1)
{
b1.setBackground(Color.YELLOW);
}
else if(e.getSource()==a2)
{
b2.setBackground(Color.magenta);
}
}
}
/****************************************************/
这片程序原来是想实现 
1.按a1 a2按钮 触发action事件然后改变b1 b2按钮颜色
2.按键盘上下左右键 也实现b1,b2按钮颜色的改变..但为什么我这里只能实现功能1  却实现不了功能2呢??
我到底该如何添加监听器啊!??

解决方案 »

  1.   

    如果你想在a1 a2按钮上按键盘上下左右键改变b1,b2按钮颜色.如下:
    a1.addKeyListener(this);
    a2.addKeyListener(this);
      

  2.   

    如你所说的..
    选上b1 b2按钮后确实响应了按键 
    或者加上a1,a2的按键监听后他也可以实现b1b2的颜色改变但这样都有个必须的是你要选中这几个按钮之一..
    但我希望即使那几个按钮没被选中, 只要主窗口在前台显示着. 就能实现按键的反应
    那么我应该怎么加addKeyListener()呢??
      

  3.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class test extends JFrame implements KeyListener,ActionListener{JButton a1;
    JButton a2;
    JButton b1;
    JButton b2;
    JPanel jp;
    public test()
    {
    a1=new JButton("a1");
    a2=new JButton("a2");
    b2=new JButton("b1");
    b1=new JButton("b2");
    jp=new JPanel();
    //jp.setBorder(BorderFactory.createEtchedBorder());getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT,30,30));
    a1.addActionListener(this);
    a2.addActionListener(this);
    a1.addKeyListener(this);
    a2.addKeyListener(this);
    getContentPane().add(a1);
    getContentPane().add(a2);
    jp.add(b1);
    jp.add(b2);
    getContentPane().add(jp);
    pack();setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //getContentPane().addKeyListener(this);
    //jp.addKeyListener(this);    
    //到底要怎么加这个键盘监听啊??  我现在按键都没反应啊...
    }public static void main(String[] args) {
    // TODO 自动生成方法存根
    new test();}public void keyPressed(KeyEvent e) {int keyCode=e.getKeyCode();
      switch(keyCode)
      {
         case KeyEvent.VK_UP:
         b1.setBackground(Color.BLUE);
         break;
         case KeyEvent.VK_DOWN:
         b2.setBackground(Color.RED);
         break;
         case KeyEvent.VK_LEFT:
         b1.setBackground(Color.CYAN);
         break;
         case KeyEvent.VK_RIGHT:
         b2.setBackground(Color.BLACK);
         break;
         default:
      }
    }public void keyReleased(KeyEvent e) {
    }public void keyTyped(KeyEvent e) {
      
     }public void actionPerformed(ActionEvent e) {  if(e.getSource()==a1)
      {
         b1.setBackground(Color.YELLOW);
      }
      else if(e.getSource()==a2)
      {
      b2.setBackground(Color.magenta);
      }}
    }
    这样可以吗
      

  4.   

    还是有问题的..那样的话如果鼠标点在b1,b2上就无法实现键盘监听了..我现在知道了有两个解决方法.
    1.  a1,a2,b1,b2都给  addkeylistenter(this)
    2.  a1,a2,b1,b2都 setenabled(false);  再给主框架 this.addkeylistener(this);我感觉 如果框架里有按钮可用的话  那么"焦点"  (可以这么说吧?)  就会自动在这几个按钮上,  即使你点击主框架的边框  焦点还是停留在原来的按钮上..  
    所以如果把4个按钮都设置为不可用的话..  焦点就自动回到主框架上了  那么方法2就可以实现所有的键盘监听.    否则就得使用方法1...
    但我有疑问的就是   添加事件监听到底是怎么个监听法? 
    A.add****Listener(B)  ;  里面的  A到底该是什么  B又到底是什么???  我现在感觉很混乱..
    书上讲的事件触发的部分都很少   而且只讲应用, 没讲他到底是怎么一回事...
    郁闷啊....