import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class plc extends JPanel{
plc()
{
this.addKeyListener(new MyKeyBoardListner());
System.out.println("key listner added");
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawRect(0,0,10,60);
g.fillRect(0,0,10,60);
}
private class MyKeyBoardListner extends KeyAdapter
{
public void keyPressed(KeyEvent ke)
{
System.out.println(ke.getKeyCode());
}
}
}
public class plank {
public static void main(String[] args) {
JFrame frame =new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
frame.setVisible(true);
plc pl=new plc();
frame.add(pl);
}
}
This doesnt work when i press a key the event is not executed.