mykeybinding没有在jpanel中执行它的操作

fhg3lkii  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(300)

我在一个jpanel上添加了keybinding。问题是这个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);
    }

}
yzckvree

yzckvree1#

Baby 未附加到组件层次结构,因此不会接收任何键事件。事实上,这种设计毫无意义。没有必要 Bady 延伸 JPanel 完全。
相反,利用 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);
        }

    }
}

相关问题