首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试在检测到鼠标移动时添加计时器

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

Stack Overflow用户
提问于 2017-01-12 21:43:15
回答 1查看 236关注 0票数 0

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

代码语言:javascript
复制
public class MouseMotionEvent extends JPanel
    implements MouseMotionListener {
BlankArea blankArea;
JTextArea textArea;
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));
}

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);
}

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

}

EN

回答 1

Stack Overflow用户

发布于 2017-01-12 22:21:20

也许我在这里遗漏了一些东西,但你所要求的似乎是一个相当直接的任务。我猜你的问题是设置“定时器”本身吧?java.util.Timer类可用于此目的。

因此,对于您的情况,如下所示的函数

代码语言:javascript
复制
private void startTimer()
{
    isTimerRunning = true;
    new java.util.Timer().schedule(new java.util.TimerTask()
    {
        @Override
        public void run()
        {
            isTimerRunning = false;
        }
    }, 10000);

}

您必须从mouseMoved函数内部调用此函数,如下所示,

代码语言:javascript
复制
public void mouseMoved(MouseEvent e)
{
    eventOutput("Mouse moved", e);
    if (!isTimerRunning)
    {
        startTimer();
    }
}

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

编辑:正如VGR提到的,javax.swing.Timer更适合与其他swing组件一起使用,特别是在做一些与图形用户界面相关的事情时。从文件上看,

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

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

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

代码语言:javascript
复制
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);
}
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41614766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档