我想实现一个面板,该面板绘制两只眼睛,它们的移动方向是向上,向中心还是向下,这取决于鼠标光标是在眼睛上方,内部还是下方。我首先使用此代码来吸引人:

public class EyesPanel extends JPanel implements ActionListener {
    // images
    public void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.drawOval(130, 100, 120, 120);
        g.drawOval(250, 100, 120, 120);
        g.fillOval(175, y, 30, 30);   // x: 175  y: 145
        g.fillOval(295, y, 30, 30);   // x: 295  y: 145
    }

java - 一位Swing新手,想做一点动人的Java GUI练习但卡住了-LMLPHP

然后是添加事件侦听器以使此类工作的时候了,但这是我坚持的部分。我知道如何移动图形(ActionListener),也知道如何实现MouseInputListener(扩展了MouseInputListener)。但是,将两者结合在一起使我感到沮丧。有人可以告诉我该怎么做吗,给我一个示例代码会很有帮助。

以下是我到目前为止的代码,而不是功能完整的代码:
public class EyesPanel extends JPanel implements ActionListener {
    private JPanel panel;
    private int y;
    private int dy;
    private Timer t;
    private Mouse move;

    public EyesPanel() {
        dy = 5;
        y = 145;

        // mouse detector
        this.addMouseListener(new Mouse());
        this.addMouseMotionListener(new Mouse());

        // Timer
        t = new Timer(100, this);
    }

    // images
    public void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.drawOval(130, 100, 120, 120);
        g.drawOval(250, 100, 120, 120);
        g.fillOval(175, y, 30, 30);   // x: 175  y: 145
        g.fillOval(295, y, 30, 30);   // x: 295  y: 145
    }

    public void actionPerformed(ActionEvent event) {
        moveDown();     //➜ not complete, don't know how to implement
    }

    // move up
    private void moveUp() {
        if (move.move() == 1) {
            t.start();
            y = y + dy;
            repaint();
        } else {
            t.stop();
        }
    }

    // move down
    private void moveDown() {
        if (move.move() == -1) {
            t.start();
            y = y - dy;
            repaint();
        } else {
            t.stop();
        }
    }
    // ➜ not complete, trying, but no clue
}

我的鼠标事件类:
public class Mouse extends MouseInputAdapter {
    private int y;

    public void mouseEntered(MouseEvent event) {
        JPanel pane =  (JPanel) event.getSource();
        y = pane.getHeight();      // ➜ not complete
    }
}

最佳答案

使您的类像这样,然后您就可以将super.paintComponents(g)取消。

    import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionListener;
import java.util.Timer;

import javax.swing.JPanel;


public class EyesPanel extends JPanel implements ActionListener,MouseMotionListener{
    private JPanel panel;
    private int y;
    private int dy;
    private Timer t;


    public EyesPanel() {
        dy = 5;
        y = 145;

        // mouse detector
        this.addMouseMotionListener(this);


        // Timer

    }

    // images
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawOval(130, 100, 120, 120);
        g.drawOval(250, 100, 120, 120);
        g.fillOval(175, y, 30, 30);   // x: 175  y: 145
        g.fillOval(295, y, 30, 30);   // x: 295  y: 145
    }



    // move up

    // ➜ not complete, trying, but no clue




@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}

    @Override
    public void mouseDragged(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println(arg0.getY());
        if(arg0.getY() > 101 && arg0.getY() < 187)
        y = arg0.getY();
        repaint();

    }


}

10-05 22:37