Java JSlider 设置值 [英] Java JSlider set values

查看:48
本文介绍了Java JSlider 设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置 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

推荐答案

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

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

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

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.

将最小值设置为 1,(以 10 为基数的对数 = 1),最大值为 4(以 10,000 为基数的对数)= 4.使用 getValue() 获取滑块的当前值 方法并使用 Math.pow() 将 10 提高到那个幂.

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.

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

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.

干杯!

这篇关于Java JSlider 设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆