java 双打的 JSlider
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2172574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSlider for doubles
提问by Aly
I am making a GUI (using swing) for a poker framework and need some sort of slider to allow players to select a bet size. However the Swing JSlideronly works with int values whereas I need something that can support doubles for 1 decimal point. Are there any libraries I can use, or tricks with the JSlider?
我正在为扑克框架制作 GUI(使用 Swing),并且需要某种滑块来允许玩家选择下注大小。然而,SwingJSlider只适用于 int 值,而我需要一些可以支持 1 个小数点的双精度值。有没有我可以使用的库,或者有JSlider什么技巧?
采纳答案by Maurice Perry
You can multiply the value by 10
您可以将该值乘以 10
EDIT
编辑
You can change the labels displayed as follows:
您可以更改显示的标签,如下所示:
Hashtable labelTable = new Hashtable();
labelTable.put( new Integer( 0 ), new JLabel("0.0") );
labelTable.put( new Integer( 5 ), new JLabel("0.5") );
labelTable.put( new Integer( 10 ), new JLabel("1.0") );
framesPerSecond.setLabelTable( labelTable );
回答by Vincent Ramdhanie
One trickyou can use with JSlider is to use the units in cents. So if you want the user to select an amount between 1 and 10 dollars you actually set the JSlider to the range 100 to 1000. Then you simply convert the value to dollars and cents when the user selects a value.
您可以与 JSlider 一起使用的一个技巧是使用美分单位。因此,如果您希望用户选择 1 到 10 美元之间的金额,您实际上将 JSlider 设置为 100 到 1000 的范围。然后,当用户选择一个值时,您只需将该值转换为美元和美分。
You can then use a Dictionary of values to specify what label is displayed at any value on the slider. Use the setLabelTable()method for this.
然后,您可以使用值字典来指定在滑块上的任何值处显示什么标签。setLabelTable()为此使用该方法。

