Java 按键不工作

Java 按键不工作,java,swing,paintcomponent,keylistener,Java,Swing,Paintcomponent,Keylistener,我正在尝试使用箭头键上下移动图像。我使用ASCII值检查按下了哪个键,但没有调用keyPressed的处理程序。我通过应用断点进行了检查,但什么也没发生 package com.google.play; import java.awt.BorderLayout; import java.awt.Color; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.event.ActionEvent; impor

我正在尝试使用箭头键上下移动图像。我使用ASCII值检查按下了哪个键,但没有调用
keyPressed
的处理程序。我通过应用断点进行了检查,但什么也没发生

package com.google.play;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;

public class ShootingBubble extends JPanel implements ActionListener,KeyListener {

    int y=250;
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.GRAY);
        g.drawRect(0, 200, 300, 400);
        g.fillRect(0, 200, 300, 400);

        g.setColor(Color.GRAY);
        g.drawRect(800, 200, 300, 400);
        g.fillRect(800, 200, 300, 400);

        g.setColor(Color.BLACK);
        g.drawLine(1000,200, 350, 200);

        g.setColor(Color.BLACK);
        g.drawOval(295, 190, 20, 20);
        g.fillOval(295, 190, 20, 20);

        g.setColor(Color.BLACK);
        g.drawLine(0,190, 300, 190);


        g.setColor(Color.BLACK);
        g.drawLine(310,190, 310, y);

        ImageIcon ic=new ImageIcon("C:\\Users\\acer\\Desktop\\mario.gif");
        ic.paintIcon(this, g, 295, y);//moving image with help of y variable
        ic.paintIcon(this, g, 0, 150);
        ic.paintIcon(this, g, 40, 150);
        ic.paintIcon(this, g, 80, 150);
        ic.paintIcon(this, g, 120, 150);

        ImageIcon ic1=new ImageIcon("C:\\Users\\acer\\Desktop\\index.jpg");
        ic1.paintIcon(this, g, 320, 130);





    }

    public ShootingBubble() {
        setBackground(Color.WHITE);
        setFocusTraversalKeysEnabled(false);


    }


    void init(){

    }

    @Override
    public void keyReleased(KeyEvent e) {
        System.out.println("c");

    }

    @Override
    public void keyTyped(KeyEvent e) {
        System.out.println("d");

    }

    public static void main(String[] args) {
        ShootingBubble st=new ShootingBubble();
        JFrame jf=new JFrame();
        jf.setSize(1000, 600);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.getContentPane().add(st);
    }
    @Override
    public void actionPerformed(ActionEvent arg0) {

        }





    @Override
    public void keyPressed(KeyEvent arg0) {

        if(arg0.getKeyCode()==38){

            y-=2;//updating image position in vertically upward direction
        }
        if(arg0.getKeyCode()==40)
        {
            y=y+2;//updating image position in vertically downward direction
        }


        System.out.println("Key Pressed "+arg0.getKeyCode()+ " "+arg0.getKeyChar());
        repaint();;

    }



}

您必须注册keylistener


添加
jf.addKeyListener(此)到您的主方法。

几个问题,并非都与KeyListener有关:

  • 不要在paintComponent()方法中执行I/O操作。绘画方法仅适用于绘画。Swing将确定何时需要重新绘制组件,并且您不希望反复阅读图像。阅读构造器中的图像

  • 在调用frame.setVisible(…)方法之前,应将所有组件添加到框架中。这将确保在所有组件上调用布局管理器

  • 不要使用“神奇数字”。我不知道“38”和“40”是什么。你怎么知道它们是什么?如果您从教程中复制了该代码,请转储该教程!相反,您应该使用
    KeyEvent.VK\ux?

  • 即使将KeyListener添加到框架中,代码也可能无法工作,因为KeyEvents只被分派到具有焦点的组件,我不知道框架将始终具有焦点。实际上,面板应该有焦点,因为它是进行绘制的组件

  • 不要使用KeyListener。Swing设计用于
    键绑定
    。有关更多信息和示例,请参阅


  • 无论何时提出问题,请简化你的问题。无论你想问什么,都会被提及。因为这是你的第一个问题。我将放弃投票给你。通过本教程,了解如何提问和保持站立状态