如何在 java swing 中创建一个数字文本框,它有两个按钮(向上和向下),分别增加和减少文本框中的值。此外,此文本框必须可使用仅数字值进行编辑。像这样
我尝试将两个按钮放在文本框附近,然后手动执行按钮单击操作。
有没有其他方法可以以更好的方式做到这一点并获得与第一张图片。
谢谢 :)
How can I create a numeric text box in java swing , which has two buttons (up and down) which increments and decrements the value in the text box respectively. Also this text box must be editable with ONLY NUMERIC VALUES. Something like this
I tried by placing two buttons near a text box and manually doing the operation on button click.
Is there any other method in which I can do this in a better way and achieve a similar result as the first image.
Thanks :)
发布评论
评论(2)
使用 JSpinner
如何在 java 中使用 Spinners
根据您的评论:
Use JSpinner
How to use Spinners in java
Based on your comment:
JSpinner 需要仅允许数字输入,需要一些技巧在它的模型中,但你的第二个。图片看起来像两个 JButtons (使用 JButton#setFocucPainted(false )) 和一个 JFormattedTextField 具有数字格式,具有 min/maxDecimalPaces,具有舍入模式,
然后 JButton 的 rel="nofollow">Action 将是
JSpinner need for allows numeric input only, required some hack for that in its Model, but your 2nd. picture looks like as two JButtons (with JButton#setFocucPainted(false)), and one JFormattedTextField with number Format, with min/maxDecimalPaces, with roundingMode
then Action from
JButton
will be