Java JSlider 设置值

发布于 2024-11-24 02:09:14 字数 60 浏览 1 评论 0原文

如何设置 JSlider 的值?我想为它分配特定的值,间隔不是恒定的。 请帮忙

How can I set the values of a JSlider? I want to assign it specific values, with intervals that are not constant.
Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美男兮 2024-12-01 02:09:14

您可以使用构造函数设置滑块的最小值和最大值:

JSlider mySlider = new Slider(10, 30); // min value of slider, maxValue of slider

据我所知,滑块的范围是统一的并且无法更改。 (不过我不确定这一点。)您可以做的是使用统一值并将其映射到您想要的间隔。例如,假设您希望滑块从 10 变为 10000,但采用对数刻度。

将最小值设置为 1(以 10 为底的对数 10 = 1),将最大值设置为 4(以 10 为底的对数 10,000)= 4。使用 getValue() 方法并使用 Math.pow() 计算 10 的幂。

或者,如果无法计算,您可以将与滑块位置相对应的值与您想要的值存储在数组中。

您可以使用setLabelTable(Dictionary labels)来设置自定义标签。 此页面提供有关如何创建自定义标签的信息。正如此处所指出的,您实际上会使用HashTable 实现Dictionary 接口的类。

干杯!

You can set the min and max values of the slider with the constructor:

JSlider mySlider = new Slider(10, 30); // min value of slider, maxValue of slider

As far as I know the range of the slider is uniform and can't be changed. (I am not certain of this though.) What you could do is use the uniform value and map it to the intervals you want. For example, lets' say you want the slider to go from 10 to 10000, but at a logarithmic scale.

Set the min value to 1, (log base 10 of 10 = 1), the max value to 4 (log base 10 of 10,000) = 4. Get the current value of the slider using the getValue() method and raise 10 to that power with Math.pow().

Or you could store the values corresponding to the slider positions with the values you want in array if they cannot be computed.

You can use the setLabelTable(Dictionary labels) to set custom labels. This page has information on how to create custom labels. As pointed out here you would actually use a HashTable a class that implements theDictionary interface.

Cheers!

祁梦 2024-12-01 02:09:14

使用BoundedRangeModel
您可以修改此模型,这样您也可以同步 SJlider。

    BoundedRangeModel bRangeModel = 
      new DefaultBoundedRangeModel(initValue, extent, min, max);
    JSlider s = new JSlider(bRangeModel);

但步长与模型无关。您可以使用小刻度间距设置步骤(我认为这就是您想要的)

    slider.setMinorTickSpacing(5); // step / interavl
    slider.setSnapToTicks(true); // should be activated for custom tick space


Alternatively you can use a JSpinner:

    SpinnerModel spinnerModel = 
      new SpinnerNumberModel(value, minimum, maximum, stepSize);
    JSpinner spinner = new JSpinner(spinnerModel);

    // changing the stepSize at anytime
    sm.setStepSize(newValue);

Use a BoundedRangeModel
You can modify this model and so you synchronize the SJlider too.

    BoundedRangeModel bRangeModel = 
      new DefaultBoundedRangeModel(initValue, extent, min, max);
    JSlider s = new JSlider(bRangeModel);

But the step size isn't bound to the model. You can set the step with the minor tick spacing (I think that it's what you want)

    slider.setMinorTickSpacing(5); // step / interavl
    slider.setSnapToTicks(true); // should be activated for custom tick space


Alternatively you can use a JSpinner:

    SpinnerModel spinnerModel = 
      new SpinnerNumberModel(value, minimum, maximum, stepSize);
    JSpinner spinner = new JSpinner(spinnerModel);

    // changing the stepSize at anytime
    sm.setStepSize(newValue);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文