简体   繁体   English

JSlider - 单击使点 go 朝向该方向

[英]JSlider - clicking makes the dot go towards that direction

I have a JSlider with minimum value of 0 and max value of 100. When the JSlider first launches, the value is 0.我有一个 JSlider,最小值为 0,最大值为 100。当 JSlider 首次启动时,值为 0。

Now, when I click my mouse on the 50, or even the 20 or 100, the JSlider doesn't move to the location I click.现在,当我在 50,甚至 20 或 100 上单击鼠标时,JSlider 不会移动到我单击的位置。 It simply jumps a little bit towards the right.它只是向右跳了一下。

How do I make it so that the value will go to whatever I click?我如何使它的值将 go 到我点击的任何地方?

Here is the beginning of a class that will do what you need.这是 class 的开头,它将满足您的需求。

The general idea is to detect where the user clicks and calculate the value offset necessary in order to match that new slider location.总体思路是检测用户点击的位置并计算必要的偏移值,以匹配新的 slider 位置。

// 'E' stands for enhanced
public class EJSlider extends JSlider {

   public EJSlider() {
      super();
      addMouseListener(new MouseAdapter() {
         @Override
         public void mousePressed(MouseEvent e) {
            Point p = e.getPoint();
            double percent = p.x / ((double) getWidth());
            int range = getMaximum() - getMinimum();
            double newVal = range * percent;
            int result = (int)(getMinimum() + newVal);
            setValue(result);
         }
      });
   }

   public static void main(String[] args) {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.add(new EJSlider());
      f.pack();
      f.setVisible(true);
   }
}

Simply run the example.只需运行示例。 The slider will jump to wherever you click your mouse. slider 将跳转到您单击鼠标的任何位置。 This has not been thoroughly tested, and will not work with vertical sliders, yet.这尚未经过彻底测试,并且不适用于垂直滑块。

A similar solution to this one would be simply adding my custom MouseListener to any JSlider you would like that functionality on.与此类似的解决方案是将我的自定义MouseListener添加到您想要该功能的任何JSlider中。

(Note, I know it's not perfect yet, but it is a good start.) (注意,我知道它还不完美,但这是一个好的开始。)

BasicSliderUI provides a method called valueForXPosition : BasicSliderUI提供了一个名为valueForXPosition的方法:

Returns the value at the x position.返回 x position 处的值。 If xPos is beyond the track at the left or the right, this method sets the value to either the minimum or maximum value of the slider, depending on if the slider is inverted or not.如果 xPos 超出左侧或右侧的轨道,此方法将值设置为 slider 的最小值或最大值,具体取决于 slider 是否反转。

(For vertical sliders use valueForYPosition ) (对于垂直滑块使用valueForYPosition

Example adding a MouseListener to a horizontal JSlider using valueForXPosition:使用 valueForXPosition 将 MouseListener 添加到水平 JSlider 的示例:

JSlider slider = new JSlider();
slider.setOrientation(JSlider.HORIZONTAL);
slider.setMinimum(0);
slider.setMaximum(100); 
slider.addMouseListener(new MouseAdapter() {
    @Override
    public void mousePressed(MouseEvent e) {
       JSlider sourceSlider=(JSlider)e.getSource();
       BasicSliderUI ui = (BasicSliderUI)sourceSlider.getUI();
       int value = ui.valueForXPosition( e.getX() );
       slider.setValue(value);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM