简体   繁体   English

Java MouseMotionListener

[英]Java MouseMotionListener

I am working on a Reversi game in java and one thing I am doing is making it so the background color of a space turns green if the move is valid. 我正在研究java中的Reversi游戏,我正在做的一件事就是如果移动有效,空间的背景颜色会变成绿色。 I want to do this by having it turn green when the player puts his mouse over the space, but I am having trouble figuring out how to make the color go back to the default when the mouse is removed from the space. 我想通过让玩家将鼠标放在空间上时变为绿色来做到这一点,但是当我从空间中移除鼠标时,我无法弄清楚如何使颜色恢复到默认状态。 Here is my code, it changes the color to red for now: 这是我的代码,它现在将颜色更改为红色:

gameSpacePanel.addMouseMotionListener(new MouseAdapter() {
            public void mouseMoved(MouseEvent e) {
                gameSpacePanel.setBackground(Color.RED);
            }
            public void mouseExited(MouseEvent e) {
                gameSpacePanel.setBackground(Color.GRAY);
            }
        });

I tried the mouseExited method, but apparently that doesn't do what I thought it would do. 我尝试了mouseExited方法,但显然这不符合我的想法。 Any suggestons? 有什么建议吗? the mouseMoved method works fine, I just don't know how to make the color go back to normal when the mouse is removed. mouseMoved方法工作正常,我只是不知道如何在删除鼠标时使颜色恢复正常。 Thanks! 谢谢!

A MouseMove event is being fired on every mouse movement. 每次鼠标移动都会触发MouseMove事件。 Correct me if I am wrong, you want the mouse to change color on entering and change back to default color on exit? 纠正我,如果我错了,你想让鼠标在输入时改变颜色并在退出时改回默认颜色? First MouseMotionListener does not have mouseExited method, instead use MouseListener , then replace 第一个MouseMotionListener没有mouseExited方法,而是使用MouseListener ,然后替换

void mouseMoved(MouseEvent e) 

with

void mouseEntered(MouseEvent e) 

It should look something like this: 它应该看起来像这样:

gameSpacePanel.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                gameSpacePanel.setBackground(Color.RED);
            }
            public void mouseExited(MouseEvent e) {
                gameSpacePanel.setBackground(Color.GRAY);
            }
        });

I just tried this and got the same result as you. 我刚试过这个并得到了和你一样的结果。 But then I realised you only added the MouseAdapter as a MouseMotionListener . 但后来我意识到你只将MouseAdapter添加为MouseMotionListener You also have to add it as a MouseListener too, because mouseExited() is part of that interface, while mouseMoved() is part of MouseMotionListener . 您还必须将其添加为MouseListener ,因为mouseExited()是该接口的一部分,而mouseMoved()MouseMotionListener一部分。

Here is a short program which works: 这是一个有效的简短程序:

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

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

public class Test {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                final JPanel panel = new JPanel();
                MouseAdapter mouseAdapter = new MouseAdapter() {
                    public void mouseMoved(MouseEvent e) {
                        panel.setBackground(Color.RED);
                    }
                    public void mouseExited(MouseEvent e) {
                        panel.setBackground(Color.GRAY);
                        panel.repaint();
                    }
                };
                panel.addMouseListener(mouseAdapter);
                panel.addMouseMotionListener(mouseAdapter);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(panel);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM