Jump to content

Java KeyListener for JPane

Go to solution Solved by AngusBrown,

You have already implemented the KeyListener Interface, so the last thing to do is to assign it to the JPanel.

What you need to do in the constructor:

addKeyListener(this);

Documentation: https://docs.oracle.com/javase/8/docs/api/java/awt/Component.html#addKeyListener-java.awt.event.KeyListener-

Hi! I'm trying to make so that my JPane in Java can listen and act upon different key presses. I don't know what I'm missing in my code.

package main;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class Panel extends javax.swing.JPanel implements KeyListener {

    //Skapandet av spelare och object
    public Spelare p;
    public ArrayList<Vägg> väggar;
    
    public Panel() {
        initComponents();
        gameLoop.schedule(loop, 17, 17);
        p = new Spelare(20, 20, 50);
        väggar = new ArrayList<>(); 
        
    }
   
    
    
    //De olika KeyListenerna
    @Override
    public void keyTyped(KeyEvent e) {
        //Inget
    }
    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println(e.getKeyCode());
    }
    @Override
    public void keyReleased(KeyEvent e) {
        
    }
    
    
    //Main game loop
    Timer gameLoop = new Timer();
    TimerTask loop = new TimerTask(){
        public void run() {
            //Egentliga loop
            
            p.verkställFlyttning();
            
            
            
            repaint();
        }
    };
    
    
    
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        p.rita(g);
        
    }
    
    
    


    
    
    
    
    
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    // End of variables declaration                   
}

Thanks in advance!

 

Link to comment
https://linustechtips.com/topic/880388-java-keylistener-for-jpane/
Share on other sites

Link to post
Share on other sites

You have already implemented the KeyListener Interface, so the last thing to do is to assign it to the JPanel.

What you need to do in the constructor:

addKeyListener(this);

Documentation: https://docs.oracle.com/javase/8/docs/api/java/awt/Component.html#addKeyListener-java.awt.event.KeyListener-

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×