简体   繁体   中英

Java JFrame - Key presses dont seem to be registering

I am using Java AWT Graphics and JFrame to make a gui application. I need to get key input, and the KeyListener implementation does not seem to be working, as my System.out.println("hello") Does not seem to be triggering. I cannot find an answer for this problem elsewhere on the internet so i have come here.

I am using eclipse and itdoes not seem to provide me with any insight, errors, warnings ect relating to the problem, I am also rather new to Java but i have experience with higher level languages including JavaScript.

Other than the forementioned problem, the application works as intended, moving the rectangle from the top left, to the top right of the screen and beyond.

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Main extends JPanel implements ActionListener, KeyListener{

    static int WIDTH;
    static int HEIGHT;

    int x = 0;
    int y = 0;

    int targetFPS = 60;
    int currentFPS = targetFPS;

    Timer timer = new Timer(1000/targetFPS, this);

    public Main(){
        timer.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.fillRect(x-25, y-25, 50, 50);
    }
    public static void main(String[] args){
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        WIDTH = (int) screenSize.getWidth();
        HEIGHT = (int) screenSize.getHeight();
        Main main = new Main();
        JFrame frame = new JFrame();
        frame.setTitle("Hello");
        frame.setSize(WIDTH, HEIGHT);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(main);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        x++;
        repaint();
    }
    @Override
    public void keyPressed(KeyEvent e) {
        int c = e.getKeyCode();
        System.out.println("hello");
    }
    @Override
    public void keyReleased(KeyEvent e) {

    }
    @Override
    public void keyTyped(KeyEvent e) {

    }
}

KeyListener suffers from focus related issues and in this respect, is unreliable, instead, you should be using the Key Bindings API which was designed to help solve this issue.

Take a look at How to Use Key Bindings for more details

Try:

public Main(){
    timer.start();
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
==> requestFocus();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM