mouseMoved事件不起作用

时间:2014-04-02 12:23:16

标签: java swing jpanel paint runnable

我一直在尝试使用此代码,就像当您将鼠标悬停在开始按钮上时,它应该将其颜色更改为灰色,但每当我将鼠标悬停在它上面时。没有任何反应,有人可以告诉我为什么吗?我没有得到任何错误,似乎我的鼠标移动听众没有被编译器识别,对不起我的英语。我还没有完成它,但这是代码:

class Contents extends JFrame implements Runnable {
    private Image dbi;
    private Graphics dbg;
    private boolean isStarted, isHovered;
    private int x,y,xDir,yDir,bx,by,timer,life,my,mx,mhx,mhy;
    private Rectangle startgame = new Rectangle(80,100,150,40);
    Contents()
    {
    super();
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    if(isStarted)
    setSize(600,600);
    else
    {    
    setSize(300,300);
    setBackground(Color.BLUE);
    }
    setLocationRelativeTo(null);
    isStarted = false;
    isHovered = false;
    addMouseListener(new MouseAdapter()
    {
        public void mousePressed(MouseEvent e)
        {
            mx = e.getX();
            my =  e.getY();
            if(mx > startgame.x && mx < startgame.x+startgame.width &&
               my > startgame.y && my < startgame.y+startgame.height)
            {
                isStarted = true;
            }
        }
        public void mouseMoved(MouseEvent e)
        {
            mhx = e.getX();
            mhy =  e.getY();
            if(mhx > startgame.x && mhx < startgame.x+startgame.width &&
               mhy > startgame.y && mhy < startgame.y+startgame.height)
               isHovered = true;
            else
               isHovered = false;

        }
    });
    }

    public void  paint(Graphics g)
    {
        dbi = createImage(getWidth(), getHeight());
        dbg = dbi.getGraphics();
        draw(dbg);
        g.drawImage(dbi,0,0,this);
        repaint();
    }
    public void draw(Graphics g)
    {
        if(!isStarted)
        {
                if(!isHovered)
                g.setColor(Color.GRAY);
                else
                g.setColor(Color.GREEN);
                    g.fillRect(startgame.x, startgame.y, startgame.width, startgame.height);
                    g.setFont(new Font("Serif",Font.BOLD,24));
                    g.setColor(Color.WHITE);
                    g.drawString("Start game", startgame.x+20, startgame.y+25);
                    g.drawString(String.format("hoverx: %d hovery: %d",mhx,mhy), 50,200);
        }
        else
        {
        }
    }
    public void run()
    {
    } }

public class Game  {
    public static void main(String[] args)
    {
        Contents c = new Contents();
    } }

1 个答案:

答案 0 :(得分:2)

只需使用Rectangle.contains(Point)检查MouseEvent中的点是否在Rectangle内。这是一个例子

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class PaintedButton extends JPanel {
    private static final Color HOVER_COLOR = Color.BLUE;
    private static final Color NON_HOVER_COLOR = Color.GREEN;
    private static final Rectangle2D RECTANGLE = new Rectangle2D.Double(50, 50,
            200, 100);

    private Color color = NON_HOVER_COLOR;

    public PaintedButton() {
        addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseMoved(MouseEvent e) {
                Point p = e.getPoint();
                if (RECTANGLE.contains(p)) {
                    color = HOVER_COLOR;
                } else {
                    color = NON_HOVER_COLOR;
                }
                repaint();
            }
        });
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setPaint(color);
        g2.fill(RECTANGLE);

    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 200);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new PaintedButton());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}