没有负值的JSpinner [英] JSpinner without negative values

查看:153
本文介绍了没有负值的JSpinner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在netbeans中构建一个小型应用程序,我使用JSpinner组件来设置产品数量.如何将微调器设置为仅取正值?我可以在Netbeans中设置或设置一个选项吗? JSpinner的方法?

I'm building a small application in netbeans,I use a JSpinner component to set the quantity of a product.How can I set the spinner to take only positive values?Is there a choice inside Netbeans that I can set or a method for the JSpinner?

额外:

spinner.setModel(new SpinnerNumberModel(0, 0, 20, 1));

推荐答案

用于 JSpinner 您必须实现 SpinnerNumberModel

import javax.swing.*;

public class SpinnerModelTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new SpinnerModelTest().makeUI();
            }
        });
    }

    public void makeUI() {
        SpinnerModel modeltau = new SpinnerNumberModel(0.0002, 0.0001, 100.0000, 0.0001);
        JSpinner spinner = new JSpinner(modeltau);
        ((JSpinner.NumberEditor) spinner.getEditor()).getFormat().setMaximumFractionDigits(4);
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(spinner);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

这篇关于没有负值的JSpinner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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