3

我有这段代码,但没有任何反应。我不知道为什么degreesSlider.getValue() + 1不起作用。如果有任何建议,我将不胜感激。

degreesSlider.addMouseWheelListener(new MouseWheelListener() {
    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        int notches = e.getWheelRotation();
        if (notches < 0) {
            System.out.println("Mouse wheel moved UP " + -notches + " notch(es)");
            degreesSlider.setValue(degreesSlider.getValue() + 1);
        } else {
            System.out.println("Mouse wheel moved DOWN " + notches + " notch(es)");
            degreesSlider.setValue(degreesSlider.getValue() - 1);
        }
    }
});
4

2 回答 2

2

这是使用鼠标滚轮旋转移动 JSlider 的代码演示。我希望这可以帮助您更有效地处理 JSlider 的鼠标滚轮旋转。

import javax.swing.*;
import java.awt.event.*;
public class JSliderDemo extends JFrame implements MouseWheelListener
{
    JSlider degreesSlider;
    public void prepareAndShowGUI()
    {
        degreesSlider = new JSlider(0,100);
        degreesSlider.setMajorTickSpacing(2);
        degreesSlider.setMinorTickSpacing(1);
        degreesSlider.setPaintLabels(true);
        degreesSlider.setPaintTicks(true);
        degreesSlider.setPaintTrack(true);
        degreesSlider.addMouseWheelListener(this);
        getContentPane().add(degreesSlider);
        setSize(1000,100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    @Override
    public void mouseWheelMoved(MouseWheelEvent evt)
    {
        if (evt.getWheelRotation() < 0 )//mouse wheel was rotated up/away from the user
        {
            int iNewValue = degreesSlider.getValue() - degreesSlider.getMinorTickSpacing()  ;
            if (iNewValue >= degreesSlider.getMinimum())
            {
                degreesSlider.setValue(iNewValue);
            }
            else
            {
                degreesSlider.setValue(0);
            }
        }
        else
        {
            int iNewValue = degreesSlider.getValue() + degreesSlider.getMinorTickSpacing()  ;
            if (iNewValue <= degreesSlider.getMaximum())
            {
                degreesSlider.setValue(iNewValue);
            }
            else
            {
                degreesSlider.setValue(degreesSlider.getMaximum());
            }
        }
    }
    public static void main(String[] st)
    {
        SwingUtilities.invokeLater( new Runnable()
        {
            @Override
            public void run()
            {
                JSliderDemo jsd = new JSliderDemo();
                jsd.prepareAndShowGUI();
            }
        });
    }
}
于 2013-02-09T22:38:49.573 回答
2

尝试运行有关如何编写鼠标滚轮侦听器的 Swing 教程中的代码。当我运行该代码时,似乎“凹口”仅针对每 3 个车轮移动单位发生变化。因此,当您向上滚动时,实际上每次向上滚动都会获得 3 个向下滚动,并且滑块会慢慢向 0 移动。

作为一个快速修复,我刚刚做了:

    if (notches < 0) {
        System.out.println("Mouse wheel moved UP " + -notches + " notch(es)");
        slider.setValue(slider.getValue() + 1);
    } else
    if (notches > 0) {
        System.out.println("Mouse wheel moved DOWN " + notches + " notch(es)");
        slider.setValue(slider.getValue() - 1);
    }
于 2013-02-09T22:14:29.777 回答