Не работает обработчик нажатия клавиш - Java
Формулировка задачи:
Собственно сам обработчик
А это находится в конструкторе формы
Класс имплементит обработчик.
Хочу заметить, что мышка обрабатывается без проблем.
Что я делаю не так, спасибо)
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode())
{
case KeyEvent.VK_ESCAPE:
System.out.print("dsfgdfg");
System.exit(0);
break;
default:
}
}addKeyListener(this);
Решение задачи: «Не работает обработчик нажатия клавиш»
textual
Листинг программы
public class Screenshot extends JPanel implements MouseListener, MouseMotionListener, KeyListener {
static int x = 50;
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode())
{
case KeyEvent.VK_ESCAPE:
System.out.print("dsfgdfg");
System.exit(0);
break;
default:
}
}
public static BufferedImage image;
public Screenshot()
{
JFrame frame = new JFrame();
Dimension sSize = Toolkit.getDefaultToolkit ().getScreenSize ();
int Height = sSize.height;
int Width = sSize.width;
frame.setExtendedState(MAXIMIZED_BOTH);
frame.setUndecorated(true);
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
getScreen(Width,Height);
// frame.setContentPane();
frame.getContentPane().add(this);
frame.pack();
frame.setVisible(true);
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image,0,0, null);
g.setColor(Color.red);
g.drawRect(coords,40,12,456);
}
private void getScreen(int x, int y)
{
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screen=env.getDefaultScreenDevice();
Robot robot = null;
try {
robot=new Robot(screen);
} catch (AWTException ex) {
}
image = robot.createScreenCapture(new Rectangle(0,0,x,y));
if (image==null) System.exit(0);
}
public static void main(String[] arg) throws InterruptedException
{
Screenshot frame = new Screenshot();
Thread.sleep(6000);
x = 150;
frame.repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
x = 50;
repaint();
}
@Override
public void mousePressed(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseReleased(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseEntered(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseExited(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseDragged(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseMoved(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}