java 中的鼠标输入问题

Mouse input problems in java

编辑: 我有一个应用程序使用 swing Timer 来控制动作侦听器界面何时触发。鼠标逻辑有效,但偶尔不会检测到点击。下面是我的注释代码。

public class Board extends JPanel implements MouseListener, MouseMotionListener, ActionListener
{
    private MainMenu mainMenu = new MainMenu();
    private static String State; /*Makes the control flow simpler, just checking 
    strings that describe the state. All the states are contained in GameState class.*/
    public Board()
    {

        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        setVisible(true);

        mainMenu.initLogIn(); /*This just loads the button images*/
        Timer timer = new Timer(12, this); /*(millisecond delay, tells this class 
        to update any actionlistener (mouselistener etc)*/
        timer.start();
    }


    public void paint(Graphics G)
    {
        super.paint(G);
        Graphics G2d = (Graphics2D)G; 
        /*Main menu paint logic*/

            //          This paints buttons from mainMenu class on screen
            G.drawImage(mainMenu.getTopic1().getspriteImage(),
                    mainMenu.getTopic1().getxCoord(), 
                    mainMenu.getTopic1().getyCoord(),this);
            G.drawImage(mainMenu.getTopic2().getspriteImage(),
                    mainMenu.getTopic2().getxCoord(),
                    mainMenu.getTopic2().getyCoord(), this);
            G.drawImage(mainMenu.getTopic3().getspriteImage(),
                    mainMenu.getTopic3().getxCoord(),
                    mainMenu.getTopic3().getyCoord(),this);
            /*Shows mouse input worked by changing the background color*/
            if (State == GameState.MAINMENU_TOPIC1)
            {
                setBackground(Color.BLACK);
            }
            if (State == GameState.MAINMENU_TOPIC2)
            {
                setBackground(Color.BLUE);
            }
            if (State == GameState.MAINMENU_TOPIC3)
            {
                setBackground(Color.GRAY);
            }
            repaint(); //tells paint to repaint, which allows gui to update

    }
        @Override
        public void mouseClicked(MouseEvent e) 
        {
            Point point = e.getPoint();
            /*This contains the logic to change State based on mouse clicks*/

                if(mainMenu.getTopic1().getRectangle().contains(point))
                {
                    State = GameState.MAINMENU_TOPIC1;
                }
                if(mainMenu.getTopic2().getRectangle().contains(point))
                {
                    State = GameState.MAINMENU_TOPIC2;
                }
                if(mainMenu.getTopic3().getRectangle().contains(point))
                {
                    State = GameState.MAINMENU_TOPIC3;
                }
        }

所以,我不确定为什么不能始终检测到鼠标点击。我知道分配给更新动作侦听器的时间可能太短了。但是,机器循环的代码并不多,所以我认为这不是问题所在。关于什么可能导致鼠标以这种方式运行的任何想法?

还有,我以后肯定会用JButtons来实现这个。我相信这将有助于清理我在更大项目上的代码

感谢您的评论,我希望这能解决大部分问题。

鼠标 "click" 本质上可能是双击或三次单击。您可以使用 evt.clickCount 来获取它。它将合并为一个事件。

如果您想获取每个 "press",请改用 mousePressed()