将KeyListener添加到JLabel

时间:2011-07-06 09:48:49

标签: java jframe jlabel keylistener

我想从Jlabel延伸到mage类,并将KeyListner实现到它中,但keyListner不起作用 现在MouseListner工作,但KeyListner不起作用,为什么??? 另一个问题是当JFrame的setLayout为null时,它不显示(图像car或car对象);

    public class car extends JLabel implements KeyListener,MouseListener{
        private ImageIcon imgico;//to set Image icon of car
        public car(int car_num,boolean line1,Dimension position){
            //car_num variable check which of Image of car load
            //if line1 is true .line 1 else car load in line 2
            super();
            this.imgico = this.car_load(car_num, line1);
            super.setIcon(this.imgico);
            super.setVisible(true);
            super.setSize(this.imgico.getIconWidth(),this.imgico.getIconHeight());
            super.setLocation(position.width, position.height);
            this.addKeyListener(this);
            this.addMouseListener(this);

        }
        private ImageIcon car_load(int car_num,boolean line1){
            //load image icon of car
            if (car_num == 1 && line1 == true)
                return (new ImageIcon("Images/cars/01a.PNG"));
            else if (car_num == 1 && line1 == false)
                return (new ImageIcon("Images/cars/01b.PNG"));
            else if (car_num == 2 && line1 == true)
                return (new ImageIcon("Images/cars/02a.PNG"));
            else if (car_num == 2 && line1 == false)
                return (new ImageIcon("Images/cars/02b.PNG"));
            else if (car_num == 3 && line1 == true)
                return (new ImageIcon("Images/cars/03a.PNG"));
            else if (car_num == 3 && line1 == false)
                return (new ImageIcon("Images/cars/03b.PNG"));
            else if (car_num == 4 && line1 == true)
                return (new ImageIcon("Images/cars/04a.PNG"));
            else if (car_num == 4 && line1 == false)
                return (new ImageIcon("Images/cars/04b.PNG"));
            else if (car_num == 5 && line1 == true)
                return (new ImageIcon("Images/cars/05a.PNG"));
            else if (car_num == 5 && line1 == false)
                return (new ImageIcon("Images/cars/05b.PNG"));
            else if (car_num == 6 && line1 == true)
                return (new ImageIcon("Images/cars/06a.PNG"));
            else
                return (new ImageIcon("Images/cars/06b.PNG"));
            // by deafult its 06b 
        }
        @Override
        public void keyPressed(KeyEvent arg0) {
            // TODO Auto-generated method stub
            System.out.println(arg0.getKeyCode());
        }
        @Override
        public void keyReleased(KeyEvent arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public void keyTyped(KeyEvent arg0) {
            // TODO Auto-generated method stub
            System.out.println(arg0.getKeyCode());

        }
        @Override
        public void mouseClicked(MouseEvent arg0) {
            if (arg0.getSource() == this)
                System.out.println("Hi");
        }
        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public void mousePressed(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }
    }

中的

    JFrame x = new JFrame();
    x.setSize(500,500);
    x.setVisible(true);
    Dimension v = new Dimension(5,5);
    car t =new car(1,true,v);
    x.setLayout(null);
    x.add(t);

2 个答案:

答案 0 :(得分:6)

我认为JLabel无法获得焦点,因此KeyEvent永远不会被解雇。有关详细信息,请参阅此documentation

答案 1 :(得分:0)

试试这个

Jlabel.addMouseListener(new MouseAdapter()  
{  
  public void mouseClicked(MouseEvent e)  
  {  
    //Do whatever You want
  }  
});