【问题标题】:Detecting Mouse Wheel Stops检测鼠标滚轮停止
【发布时间】:2015-08-16 16:15:09
【问题描述】:

我正在构建一个简单的程序,当用户的鼠标滚轮在滚动后停止时,我需要获取布尔值false。下面是我的代码,其中包含更多信息。

public class WheelHandler extends MouseAdapter {

    public void mouseWheelMoved(MouseWheelEvent e) {
        moved = e.getWheelRotation();
        scrolling = true;
    }

    scrolling = false; // How do I make this run when the wheel becomes stopped?
}

【问题讨论】:

  • 滚动是一系列离散运动,因此您必须确定离散运动之间的时间间隔意味着“停止”,然后使用摆动计时器检查此时间增量。
  • 不,我想知道用户何时停止滚动鼠标滚轮很长一段时间。
  • 也许我没有说清楚,对此我深表歉意。但是想想一个滚动速度非常慢的用户。如果慢速滚轮每秒都在移动鼠标滚轮,那么他什么时候真正停止?同样,我认为您将不得不决定一些任意时间片,当用户移动鼠标滚轮时启动一些计时器,并在切片结束之前再次使用滚轮时取消相同的计时器。
  • 我完全理解你在说什么,我同意。

标签: java awt listener mousewheel


【解决方案1】:

滚动是一系列离散运动,因此您必须确定离散运动之间的时间间隔意味着“停止”,然后使用摆动计时器检查此时间增量。您将需要决定一些任意时间片(低于它的int TIMER_DELAY = 100 或 100 毫秒),当用户移动鼠标滚轮时启动一些计时器,并在切片结束之前再次使用滚轮时取消相同的计时器。

例如:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseWheelEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class NotifyWheelStopped extends JPanel {
    private static final int PREF_W = 400;
    private static final int PREF_H = PREF_W;
    private static final String WHEEL_STOPPED = "Wheel has stopped";
    private static final String WHEEL_MOVING = "Wheel is moving";
    public static final int TIMER_DELAY = 100;
    private JLabel notificationLabel = new JLabel(WHEEL_STOPPED, SwingConstants.CENTER);
    private WheelHandler wheelHandler = new WheelHandler();
    private Timer wheelMovementTimer;

    public NotifyWheelStopped() {
        add(notificationLabel);
        addMouseWheelListener(wheelHandler);
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private class WheelHandler extends MouseAdapter {
        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            notificationLabel.setText(WHEEL_MOVING);
            if (wheelMovementTimer != null && wheelMovementTimer.isRunning()) {
                wheelMovementTimer.stop();
            }
            wheelMovementTimer = new Timer(TIMER_DELAY, new WheelMovementTimerActionListener());
            wheelMovementTimer.setRepeats(false);
            wheelMovementTimer.start();
        }
    }

    private class WheelMovementTimerActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            notificationLabel.setText(WHEEL_STOPPED);
        }
    }

    private static void createAndShowGui() {
        NotifyWheelStopped mainPanel = new NotifyWheelStopped();

        JFrame frame = new JFrame("NotifyWheelStopped");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

即使有 100 毫秒的延迟,当车轮缓慢转动时,通知也会变得跳跃。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多