java 如何从整数变量设置 JSpinner 的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31530164/
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
How to Set the Value of JSpinner from an Integer Variable?
提问by STEPHEN YAO
int age=12;
spinner.setValue(age);
That's my code, I can't set the spinner to 12.
那是我的代码,我无法将微调器设置为 12。
PS. How to set the spinner value from database?
附注。如何从数据库中设置微调器值?
回答by sakthisundar
setValue
method expects an object as it's arguments. You are trying to set an integer, a primitive type to the method. Use one of the below
setValue
方法需要一个对象作为它的参数。您正在尝试为该方法设置一个整数,一种原始类型。使用以下之一
spinner.setValue(Integer.valueOf(age))
or
或者
spinner.setValue(Integer.toString(age))
回答by MulleOne
This may be a late answer, however: getValue() method expects an Object, so I presume rather not use a primitive data type, therefore in your case, instead of:
然而,这可能是一个迟到的答案:getValue() 方法需要一个对象,所以我认为而不是使用原始数据类型,因此在您的情况下,而不是:
int age = 12
整数年龄 = 12
rather this:
而是这个:
Integer age = 12;
spinner.setValue(age);
整数年龄 = 12;
spinner.setValue(age);
Well as for string values this works just fine:
至于字符串值,这很好用:
spinner.setValue("12");
spinner.setValue("12");
or from a string value, this works just fine too:
或者从字符串值,这也很好用:
Integer age = Integer.parseInt(yourString);
整数年龄 = Integer.parseInt(yourString);
Open to corrections!
开放更正!
回答by Shinwar ismail
Try This:
试试这个:
jSpinner1.setValue(Integer.parseInt("3"));
jSpinner1.setValue(Integer.parseInt("3"));