java - 我想弄清楚如何正确循环鼠标事件

标签 java

我正在做一项任务,其中图像四处移动,当用户单击面板时图像停止。当用户再次单击面板时,图像将启动。截至目前,我只能在图像继续运行之前启动和停止图像一次。我需要帮助来循环此过程,以便用户可以继续启动和停止图像。

这是我的代码

主要:

import javax.swing.*;

public class Rebound {
//-----------------------------------------------------------------
   //  Displays the main frame of the program.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      JFrame frame = new JFrame ("Rebound");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      frame.getContentPane().add(new ReboundPanel());
      frame.pack();
      frame.setVisible(true);
   }
}

面板:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ReboundPanel extends JPanel
{

   private final int DELAY = 10, IMAGE_SIZE = 35;

   private ImageIcon image;
   private Timer timer;
   private int x, y, moveX, moveY;

   public ReboundPanel()
   {
      timer = new Timer(DELAY, new ReboundListener());
      addMouseListener (new StopListener());
      image = new ImageIcon ("happyFace.gif");

      x = 0;
      y = 40;
      moveX = moveY = 3;

      setPreferredSize (new Dimension(1900, 1000));
      setBackground (Color.black);
      timer.start();
   }


   public void paintComponent (Graphics page)
   {
      super.paintComponent (page);
      image.paintIcon (this, page, x, y);
   }


   private class ReboundListener implements ActionListener
   {

      public void actionPerformed (ActionEvent event)
      {
         x += moveX;
         y += moveY;

         if (x <= 0 || x >= 1900-IMAGE_SIZE)
            moveX = moveX * -1;

         if (y <= 0 || y >= 1000-IMAGE_SIZE)
            moveY = moveY * -1;

         repaint();
      }
   }

   //  Represents the action listener for the timer.

   public class StopListener extends MouseAdapter
   {

       public void mouseClicked (MouseEvent event)
   {

            if (event.getButton() == MouseEvent.BUTTON1)
            {
                timer.stop();
            }

            addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent event) {
                    if(event.getButton() == MouseEvent.BUTTON1)
                    {
                        timer.start();
                        removeMouseListener(this);
                    }
                }
            });
    }

   }
}

最佳答案

您可以简单地重用相同的监听器,并在启动/停止之前检查计时器的状态:

public void mouseClicked(MouseEvent event)
{
    if (event.getButton() == MouseEvent.BUTTON1)
    {
        if (timer.isRunning()) {
            timer.stop();
        }
        else {
            timer.start();
        }
    }
}

关于java - 我想弄清楚如何正确循环鼠标事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20382724/

相关文章:

java - 使用扫描仪时出现意外的线条打印

java - 如何为 "drawer"菜单制作自动调整大小的边框 Pane

java - 使用 FTPS 将文件从 AS400 调度到大型机

java - spring mvc 在合并来自 jsp 的表单对象时错过依赖集合的 id

Java如何传递和返回字符串、方法?

java - 我应该在循环中内联长代码,还是将其移动到单独的方法中?

java - 如何将服务器证书添加到eclipse中的java应用程序中

java - 如何在 JFrame 中删除标题栏

eclipse - 如何在没有 sysouts 的情况下获取 JRE 中任何变量的当前值(如 eclipse 中的调试器模式)

java - 在 spring mvc 中更改 URL