Amit
Amit

Reputation: 177

How do I fix the KeyListener

why isnt this working the JFrame is made and the paint is working but I can't get the keylistener to work. Ive tried to print something inside the keylistener but it did not show when left arrow was pressed.

import java.awt.event.KeyEvent;


public class movingsquare extends runpaintgui{
    public void key(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT){

            x = x - 5;
            repaint(); 
            System.out.println( x);
        }

    }
}

other class

import java.awt.Graphics;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;

public class runpaintgui extends JFrame{    
int x = 30;
    public static void main(String[] args){
        runpaintgui frame = new runpaintgui();
        frame.setSize(1275, 775);
        frame.setResizable(false);
        frame.setTitle("game");
        frame.setVisible(true);    

    }
     public void paint(Graphics g){
            super.paint(g);

            g.fill3DRect(x, 30, 60, 60, true);


        }    


        }

Upvotes: 0

Views: 84

Answers (2)

Oliver Watkins
Oliver Watkins

Reputation: 13509

Firstly I would not have a subclass implement from you main program.

I am not exactly sure what you want to do in your program, but you probably just need to implement a key listener like this :

public class Test extends JFrame {

    static int x = 30;

    public static void main(String[] args) {


        final Test frame = new Test();
        frame.setSize(1275, 775);
        frame.setResizable(false);
        frame.setTitle("game");
        frame.setVisible(true);
        frame.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent arg0) {
                if (arg0.getKeyCode() == KeyEvent.VK_LEFT){
                    x = x - 5;
                    frame.repaint(); 
                    System.out.println( x);
                }

            }

            @Override
            public void keyReleased(KeyEvent arg0) {}

            @Override
            public void keyPressed(KeyEvent arg0) {}
        });
    }

    public void paint(Graphics g) {
        super.paint(g);

        g.fill3DRect(x, 30, 60, 60, true);
    }
}

Upvotes: 0

Jens
Jens

Reputation: 69450

Change your code this way:

package de.swisslife.muellerj.test;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

    public class runpaintgui  extends JFrame implements KeyListener{

      public runpaintgui(){

        this.setSize(1275, 775);
        this.setResizable(false);
        this.setTitle("game");
        this.setVisible(true);    
        this.addKeyListener(this);
        this.setVisible(true);;
      }
        int x = 30;
        public static void main(String[] args){
          runpaintgui runpaintgui = new runpaintgui();

        }
         public void paint(Graphics g){
                super.paint(g);

                g.fill3DRect(x, 30, 60, 60, true);


            }    

        public void keyTyped(KeyEvent e) {
          // TODO Auto-generated method stub

        }
        public void keyPressed(KeyEvent e) {
          if (e.getKeyCode() == KeyEvent.VK_LEFT){

            x = x - 5;
            repaint(); 
            System.out.println( x);
        }

        }
        public void keyReleased(KeyEvent e) {
          // TODO Auto-generated method stub

        }
    }

Upvotes: 1

Related Questions