JSlider for doubles

2020-08-10 07:35发布

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 JSlider only 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?

2条回答
Viruses.
2楼-- · 2020-08-10 07:58

One trick you 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.

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.

查看更多
相关推荐>>
3楼-- · 2020-08-10 08:09

You can multiply the value by 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 );
查看更多
登录 后发表回答