Java 我的钥匙扣没有';不要在JPanel中执行其操作

Java 我的钥匙扣没有';不要在JPanel中执行其操作,java,swing,jframe,jpanel,key-bindings,Java,Swing,Jframe,Jpanel,Key Bindings,我在一个JPanel上添加了键绑定。问题是这个keybinding没有执行它的“actionPerformed”。尽管我在actionPerformed中输入了一个sysout,但控制台上没有输出任何内容。有人能帮我解决这个问题吗?我已经尝试禁用我的按钮,但我的键绑定仍然不起作用 package project.fin; import java.awt.*; import java.io.*; import java.util.List; import java.awt.event.*; im

我在一个JPanel上添加了键绑定。问题是这个keybinding没有执行它的“actionPerformed”。尽管我在actionPerformed中输入了一个sysout,但控制台上没有输出任何内容。有人能帮我解决这个问题吗?我已经尝试禁用我的按钮,但我的键绑定仍然不起作用

package project.fin;

import java.awt.*;
import java.io.*;
import java.util.List;
import java.awt.event.*;
import javax.swing.*;

//Panel for my game
public class GamePlayPanel extends JPanel{
    private Image current;
    private Baby bayi;
    public GamePlayPanel(String img) {
        Dimension size = new Dimension(1200, 500);
        this.setPreferredSize(size);
        this.setMaximumSize(size);
        this.setMinimumSize(size);
        this.setSize(size);
        this.setLayout(null);
        
        //An baby object
        bayi = new Baby(100, 410, 5);
        
        //this is where my keyBinding initialized
        bayi.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0), "moveRight");
        bayi.getActionMap().put("moveRight", new Move_it(1));
        
    }
    
    //this is the action class that i want to put in my keybinding
    private class Move_it extends AbstractAction{
        int code;
        public Move_it(int code) {
            this.code=code;
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            System.out.println("test\n");
            if (this.code==1) {
                bayi.MoveRight();
            }
            repaint();
        }
    }
    
    //To draw my baby
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        bayi.draw(g);
    }

}
这是我的宝贝班:

package project.fin;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

import java.awt.event.*;
public class Baby extends JComponent{
    float x, y;
    float speed;
    Image current;
    private List <Image> ImgPool;
    private int Current;
    public Baby(float x, float y, float speed) {
        // TODO Auto-generated constructor stub
        this.x = x;
        this.y = y;
        this.speed = speed;
        ImgPool = new ArrayList<Image>();
        
        //These are just some images that i use to build my moving baby
        ImgPool.add(new ImageIcon("baby1_50.png").getImage());
        ImgPool.add(new ImageIcon("baby2_50.png").getImage());
        ImgPool.add(new ImageIcon("baby1_50.png").getImage());
        ImgPool.add(new ImageIcon("baby3_50.png").getImage());
        this.current = ImgPool.get(0);
        this.Current = 0;
    }
    
    //The action that i want my baby to do when a key is pressed
    public void MoveRight() {
            if (x>600) return;
            this.x+=speed;
            if (this.Current==3)this.Current=0;
            else
            this.Current++;
            this.current = this.ImgPool.get(Current);
    }
    

    public void draw(Graphics g) {
        g.drawImage(this.current, (int)this.x, (int)this.y, null);
    }
    
}
package project.fin;
导入java.awt.*;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.*;
导入java.awt.event.*;
公共类婴儿组件{
浮动x,y;
浮动速度;
图像电流;
私有列表ImgPool;
私有电流;
公共婴儿(浮动x、浮动y、浮动速度){
//TODO自动生成的构造函数存根
这个.x=x;
这个。y=y;
速度=速度;
ImgPool=newarraylist();
//这些只是我用来制作移动婴儿的一些图像
添加(新的ImageIcon(“baby1_50.png”).getImage();
添加(新的ImageIcon(“baby2_50.png”).getImage();
添加(新的ImageIcon(“baby1_50.png”).getImage();
添加(新的图像图标(“baby3_50.png”).getImage();
this.current=ImgPool.get(0);
这个。电流=0;
}
//我希望我的宝宝在按键时所做的动作
公权{
如果(x>600)返回;
这个.x+=速度;
如果(this.Current==3)this.Current=0;
其他的
这个.Current++;
this.current=this.ImgPool.get(当前);
}
公共空间绘制(图g){
g、 drawImage(this.current,(int)this.x,(int)this.y,null);
}
}

Baby
未附加到组件层次结构,因此不会接收任何关键事件。事实上,这种设计毫无意义。根本不需要从
JPanel
扩展
Bady

相反,直接使用
GamePlayPanel

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.*;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new GamePlayPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class GamePlayPanel extends JPanel {

        private Baby bayi;

        public GamePlayPanel() {
            //An baby object
            bayi = new Baby(100, 410, 5);

            //this is where my keyBinding initialized
            getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "moveRight");
            getActionMap().put("moveRight", new Move_it(1));

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(1200, 500);
        }

        //this is the action class that i want to put in my keybinding
        private class Move_it extends AbstractAction {

            int code;

            public Move_it(int code) {
                this.code = code;
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                System.out.println("test\n");
                if (this.code == 1) {
                    bayi.moveRight();
                }
                repaint();
            }
        }

        //To draw my baby
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            bayi.draw(g);
        }

    }

    public class Baby {

        float x, y;
        float speed;
        Image current;
        private List<Image> ImgPool;
        private int Current;

        public Baby(float x, float y, float speed) {
            // TODO Auto-generated constructor stub
            this.x = x;
            this.y = y;
            this.speed = speed;
            ImgPool = new ArrayList<Image>();

            //These are just some images that i use to build my moving baby
            ImgPool.add(new ImageIcon("baby1_50.png").getImage());
            ImgPool.add(new ImageIcon("baby2_50.png").getImage());
            ImgPool.add(new ImageIcon("baby1_50.png").getImage());
            ImgPool.add(new ImageIcon("baby3_50.png").getImage());
            this.current = ImgPool.get(0);
            this.Current = 0;
        }

        //The action that i want my baby to do when a key is pressed
        public void moveRight() {
            if (x > 600) {
                return;
            }
            this.x += speed;
            if (this.Current == 3) {
                this.Current = 0;
            } else {
                this.Current++;
            }
            this.current = this.ImgPool.get(Current);
        }

        public void draw(Graphics g) {
            g.drawImage(this.current, (int) this.x, (int) this.y, null);
        }

    }
}
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Graphics;
导入java.awt.Image;
导入java.awt.event.ActionEvent;
导入java.awt.event.KeyEvent;
导入java.util.*;
导入javax.swing.AbstractAction;
导入javax.swing.ImageIcon;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.KeyStroke;
公开课考试{
公共静态void main(字符串[]args){
新测试();
}
公开考试(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
JFrame=新JFrame();
frame.add(新的GamePlayPanel());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类GamePlayPanel扩展了JPanel{
私人婴儿八一;
公共游戏面板(){
//婴儿用品
八一=新生儿(100410,5);
//这就是我的keyBinding初始化的地方
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0),“moveRight”);
getActionMap().put(“moveRight”,新的Move_it(1));
}
@凌驾
公共维度getPreferredSize(){
返回新尺寸(1200500);
}
//这是我想放在keybinding中的action类
私有类移动\u它扩展了AbstractAction{
int代码;
公共移动信息技术(内部代码){
this.code=代码;
}
@凌驾
已执行的公共无效操作(操作事件e){
//TODO自动生成的方法存根
System.out.println(“test\n”);
if(this.code==1){
bayi.moveRight();
}
重新油漆();
}
}
//画我的孩子
@凌驾
公共组件(图形g){
超级组件(g);
八一图(g);
}
}
公务舱婴儿{
浮动x,y;
浮动速度;
图像电流;
私有列表ImgPool;
私有电流;
公共婴儿(浮动x、浮动y、浮动速度){
//TODO自动生成的构造函数存根
这个.x=x;
这个。y=y;
速度=速度;
ImgPool=newarraylist();
//这些只是我用来制作移动婴儿的一些图像
添加(新的ImageIcon(“baby1_50.png”).getImage();
添加(新的ImageIcon(“baby2_50.png”).getImage();
添加(新的ImageIcon(“baby1_50.png”).getImage();
添加(新的图像图标(“baby3_50.png”).getImage();
this.current=ImgPool.get(0);
这个。电流=0;
}
//我希望我的宝宝在按键时所做的动作
公权{
如果(x>600){
返回;
}
这个.x+=速度;
如果(this.Current==3){
这个。电流=0;
}否则{
这个.Current++;
}
this.current=this.ImgPool.get(当前);
}
公共空间绘制(图g){
g、 drawImage(this.current,(int)this.x,(int)this.y,null);
}
}
}

我建议您发布一篇文章。最小。
bayi
未附加到任何组件层次结构,因此它不会接收关键事件我的宝贝类扩展了JComponent,因此它必须附加到组件层次结构,对吗?非常感谢,它工作了!!