2

我正在编写我的项目结构如下的游戏:

我们有一个抽象类,所有其他类都继承自它。

    public abstract class Bird extends JLabel implements MouseListener {
        private static final long serialVersionUID = 1L;

        private int M_weight;
        private int M_radius;
        private long M_bornTime;
        private int M_maxBirdPower;
        private BirdState M_birdState;
        private boolean B_prepareToShoot;

        public Bird(int weight, int radius, long bornTime, int maxBirdPower,
                BirdState birdState) {
            this.M_weight = weight;
            this.M_radius = radius;
            this.M_bornTime = bornTime;
            this.M_maxBirdPower = maxBirdPower;
            this.M_birdState = birdState;
            this.B_prepareToShoot = false;
            this.addMouseListener(this);
        }

        public BirdState getBirdState() {
            return M_birdState;
        }
    }

还有一些继承自Bird类并实现抽象函数的类,如RedBird。

    public class RedBird extends Bird {
        private static final long serialVersionUID = 1L;

        public RedBird(int weight, int radius, long bornTime, int maxBirdPower,
                BirdState birdState) {
            super(weight, radius, bornTime, maxBirdPower, birdState);
        }

        @Override
        public void mouseClicked(MouseEvent arg0) {
                System.out.println("Clicked");
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
        }

        @Override
        public void mouseExited(MouseEvent arg0) {
        }

        @Override
        public void mousePressed(MouseEvent arg0) {
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
        }
    }

我从 JPanel 中的 RedBird 类中添加了一个对象,但是当我单击该对象时,MouseListener它不起作用。问题是什么 ?任何人都可以给我一个解决方案。

提前致谢 :)

4

1 回答 1

5

您还没有将鼠标侦听器“添加”到任何 JComponent。这就是为什么它不起作用。在您的鸟类的构造函数中添加以下代码:

this.addMousListener(this);
于 2012-06-24T14:03:21.130 回答