0

当用户完成拖动时,我正在尝试使用自动将旋钮的位置移动到最近的刻度。用户单击时位置设置正确,但拖动旋钮时位置设置不正确。我该如何解决这个问题?JSlider setValue(int)

    /**
     * Called when the slider is moved.
     * 
     * @access  public
     * @param   ChangeEvent
     * @return  void
     */
    public void stateChanged(ChangeEvent e) {
        JSlider source = (JSlider) e.getSource();

        if ( ! source.getValueIsAdjusting() ) {
            //let's only allow users to increment by TIMER_SPACING
            int fps = (int) source.getValue();
            int temp = fps % TIMER_SPACING;
            if ( temp > TIMER_SPACING / 2 ) {
                fps += TIMER_SPACING - temp;
            } else {
                fps -= temp;
            }

            source.setValue(fps);

            if ( fps == 0 ) {
                timer.stop();
            } else {
                if ( ! timer.isRunning() ) {
                    timer.start();
                }
                timer.setDelay(1000 / fps);
            }
        }
    }
4

1 回答 1

2

非常简单的解决方案,谢谢Andrew Thompson

slider.setSnapToTicks(true);
于 2012-04-10T21:01:59.980 回答