0

at the moment I'm learning Java, and I'm stuck at a problem.

I try to catch KeyEvent, but it doesn't work.

Maybe one of you got a solution?

package test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SpielFenster extends JFrame implements KeyListener {
    int posX = 100;
    int posY = 100;
    SpielFenster(){
        this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 
        this.setSize( 400, 600 );
        this.setTitle( "Hindi Bones - Lost in Labyrinth" );

        this.setVisible( true );
    }
    public void paint(Graphics g) { 
        super.paint(g);
        g.setColor(Color.ORANGE);
        g.fillOval(posX, posY, 20, 20); 
    }

    public void keyPressed(KeyEvent e) {
        test("test");
    }

    public void keyReleased(KeyEvent e) { test("test");}
    public void keyTyped(KeyEvent e) { test("test");}
    public void test(String x){
        System.out.println(x);
    }

    public static void main(String[] args){
        SpielFenster fenster = new SpielFenster();
        fenster.test("yoo");

    }
}

Thanks in advance! :)

SeniorJD
  • 6,946
  • 4
  • 36
  • 53
Leitwerk
  • 37
  • 5
  • Where do you catch the events? – Roman C Feb 02 '16 at 11:38
  • 1
    Don't use `KeyListener`, it suffers from focus related issues, use the [Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) API instead which will solve them – MadProgrammer Feb 02 '16 at 11:58
  • Also take a look at [this](http://stackoverflow.com/questions/13734069/how-can-i-set-in-the-midst/13734319#13734319), [this](http://stackoverflow.com/questions/16473627/java-jframe-setsizex-y-not-working/16473639#16473639), [this](http://stackoverflow.com/questions/13457237/how-to-get-the-exact-middle-of-a-screen-even-when-re-sized/13460914#13460914) and [this](http://stackoverflow.com/questions/13313084/graphics-rendering-in-title-bar/13316131#13316131) for reasons why you shouldn't override `paint` of top level conatiners like `JFrame` – MadProgrammer Feb 02 '16 at 12:02

2 Answers2

2

Because you didn't register the listener on any swing component:

someComponent.addKeyListener(this);

See: How to Write a Key Listener

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
1

You forgot to add the keylistener to any of the JComponents that you have... The only one you have is the JFrame so do it on the JFrame, in the constructor : this.addKeyListener(this);

Here it is inside your application:

package alltests;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SpielFenster extends JFrame implements KeyListener{
    int posX = 100;
    int posY = 100;

    SpielFenster(){
        this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 
        this.setSize( 400, 600 );
        this.setTitle( "Hindi Bones - Lost in Labyrinth" );
        this.addKeyListener(this);//here <<<<<
        this.setVisible( true );
    }
    public void paint(Graphics g) { 
        super.paint(g);
        g.setColor(Color.ORANGE);
        g.fillOval(posX, posY, 20, 20); 
    }

    public static void main(String[] args){
        SpielFenster fenster = new SpielFenster();
    }

    @Override
    public void keyPressed(KeyEvent e) {
        test("test");
    }

    @Override
    public void keyReleased(KeyEvent e) {
        test("test");           
    }

    @Override
    public void keyTyped(KeyEvent e) {
        test("test");           
    }
    public void test(String x){
        System.out.println(x);
    }
}

Hope it helps

  • Btw this isn't the best way of doing it cause it can get very confusing when you have to add the keylistener to the jframe... as they are both in the contructor and can only be reference with `this`. – Filippo Cardano Feb 02 '16 at 12:02