0

ユーザーがノブのドラッグを終了したときに、を使用してノブの位置を最も近い目盛りに自動的に移動しようとしています。ユーザーがクリックしたときの位置は正しく設定されていますが、ノブをドラッグしたときは正しく設定されていません。どうすればthixを修正できますか?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

非常に単純な解決策です。AndrewThompsonに感謝します。

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