如何从微调器中获取int值 [英] How to get int value from spinner

查看:57
本文介绍了如何从微调器中获取int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NetBeans 7.1在Java中进行编码.我有一个 JFrame ,上面有带整数值的微调器,我想知道如何在微调器中获得活动值,这是指用户在程序运行时选择的那个值.在其他方法上使用它.

I'm using NetBeans 7.1 to code in Java. I have a JFrame where I have spinner with integer values on it, I want to know how to get the active value in the spinner, I mean, the one that the user picks when the program is running; to use it on another methods.

推荐答案

spinner.getValue()应该可以解决问题.您可以将其转换为 Integer ,例如

spinner.getValue() should do the trick. You can cast it to Integer, like

int value = (Integer) spinner.getValue();

来自 reggoodwin 的注释:您还应该致电 spinner.commitEdit(),然后再调用 getValue()以确保使用编辑器手动输入的值会传播到模型中,否则只会得到旧值.

Note from reggoodwin: You should also call spinner.commitEdit() prior to calling getValue() to ensure manually typed values with the editor are propagated to the model, otherwise you will only get the old value.

因此,应该像下面这样

try {
    spinner.commitEdit();
} catch ( java.text.ParseException e ) { .. }
int value = (Integer) spinner.getValue();

这篇关于如何从微调器中获取int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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