2012-04-10 2 views
0

JSlidersetValue(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); 
      } 
     } 
    } 
+0

당신이 동점골을 코딩 할 ??? – mKorbel

+0

아니지만 비슷한 기능입니다. – Wex

+3

[tick to snap] (http://docs.oracle.com/javase/7/docs/api/javax/swing/JSlider.html#setSnapToTicks%28boolean%29)가 아닌가? –

답변

2

아주 간단한 해결책은 당신에게 Andrew Thompson 감사합니다 :

slider.setSnapToTicks(true); 
관련 문제