尝试在检测到鼠标移动时添加计时器

时间:2017-01-12 13:43:15

标签: java eclipse timer

我正在尝试添加一些代码,当鼠标移动一定次数时会启动计时器,下面的代码用于鼠标移动。我希望计时器持续10秒,并提醒用户计时器已经启动并完成。

var zip = new JSZip();
zip.file("test1.xml", xmlcontent);
zip.generateAsync({
        type: "blob",
        compression: "DEFLATE"
    })
    .then(function (content) {
            ...
    });

}

1 个答案:

答案 0 :(得分:1)

也许我在这里错过了一些东西,但你要求的东西似乎是一项相当直接的任务。我猜你的问题在于设置"计时器"本身? java.util.Timer类可用于此目的。

因此,对于您的情况,像

这样的函数
private void startTimer()
{
    isTimerRunning = true;
    new java.util.Timer().schedule(new java.util.TimerTask()
    {
        @Override
        public void run()
        {
            isTimerRunning = false;
        }
    }, 10000);

}

您必须在mouseMoved函数中调用此函数,如下所示,

public void mouseMoved(MouseEvent e)
{
    eventOutput("Mouse moved", e);
    if (!isTimerRunning)
    {
        startTimer();
    }
}

您可以将警报代码与设置和重置isTimerRunning的代码一起放入。

修改 正如VGR所提到的,javax.swing.Timer更适合与其他swing组件一起使用,尤其是在执行与GUI相关的操作时。 来自文档,

  

通常,我们建议使用Swing计时器而不是通用计时器来处理与GUI相关的任务,因为Swing计时器都共享相同的预先存在的计时器线程,并且GUI相关任务会在事件派发线程上自动执行。但是,如果您不打算从计时器触摸GUI,或者需要执行冗长的处理,则可以使用通用计时器。

https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html

您的代码,已修改为使用,javax.swing.Timer,

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MouseMotionEvent extends JPanel implements MouseMotionListener
{
BlankArea blankArea;
JTextArea textArea;
private Timer timer;
boolean isTimerRunning = false;
static final String NEWLINE = System.getProperty("line.separator");

public static void main(String[] args)
{
    try
    {

        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    }
    catch (UnsupportedLookAndFeelException ex)
    {
        ex.printStackTrace();
    }
    catch (IllegalAccessException ex)
    {
        ex.printStackTrace();
    }
    catch (InstantiationException ex)
    {

        ex.printStackTrace();
    }
    catch (ClassNotFoundException ex)
    {
        ex.printStackTrace();
    }

    UIManager.put("swing.boldMetal", Boolean.FALSE);

    javax.swing.SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            createAndShowGUI();
        }
    });
}

private static void createAndShowGUI()
{

    JFrame frame = new JFrame("MouseMotionEventDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComponent newContentPane = new MouseMotionEvent();
    newContentPane.setOpaque(true);
    frame.setContentPane(newContentPane);

    frame.pack();
    frame.setVisible(true);
}

public MouseMotionEvent()
{
    super(new GridLayout(0, 1));
    blankArea = new BlankArea(Color.YELLOW);
    add(blankArea);

    textArea = new JTextArea();
    textArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollPane.setPreferredSize(new Dimension(200, 75));

    add(scrollPane);

    blankArea.addMouseMotionListener(this);
    addMouseMotionListener(this);

    setPreferredSize(new Dimension(450, 450));
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    ActionListener action = new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent event)
        {
            timer.stop();
        }
    };

    timer = new Timer(0, action);
    timer.setInitialDelay(10000);
}

void eventOutput(String eventDescription, MouseEvent e)
{
    textArea.append(eventDescription + " (" + e.getX() + "," + e.getY() + ")" + " detected on " + e.getComponent().getClass().getName() + NEWLINE);
    textArea.setCaretPosition(textArea.getDocument().getLength());
}

public void mouseMoved(MouseEvent e)
{
    eventOutput("Mouse moved", e);
    if (!timer.isRunning())
    {
        timer.start();
    }
}

public void mouseDragged(MouseEvent e)
{
    eventOutput("Mouse dragged", e);
}
}