当用户完成拖动时,我正在尝试使用自动将旋钮的位置移动到最近的刻度。用户单击时位置设置正确,但拖动旋钮时位置设置不正确。我该如何解决这个问题?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);
}
}
}