Reputation: 4373
I want to make a class extend from JLabel and implement KeyListener, but KeyListener doesn't work.
Now MouseListener works but KeyListner doesn't work, but why?
Another problem is when setting the layout of JFrame to null
, it doesn't show (images car or car object).
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
}
}
In main:
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);
Upvotes: 1
Views: 3137
Reputation: 1169
try this
Jlabel.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
//Do whatever You want
}
});
Upvotes: 0
Reputation: 12572
I don't think JLabel
can gain focus, thus a KeyEvent
would never be fired. See this documentation for further details.
Upvotes: 6