java 双格式的 JSpinner

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15176695/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:43:21  来源:igfitidea点击:

JSpinner in Double format

javaswingjspinnerjformattedtextfield

提问by Chris Watts

I'd like to create JSpinners with support for non-integer values such as 2.01 and -3.456, so getValue() returns a Double.

我想创建支持非整数值(例如 2.01 和 -3.456)的 JSpinner,因此 getValue() 返回一个 Double。

Not only this, but I'd like the step size should be dynamic using something like following formula (10% of the magnitude):

不仅如此,我还希望步长应该是动态的,使用如下公式(幅度的 10%):

stepSize = 0.1 * pow(10, round( log(currentValue) ));

Is it possible? Or should I ask, is it worth the hassle?

是否可以?或者我应该问,值得麻烦吗?

Update:

更新:

With adaption of Vishal's answer, I've produced the following class to make nice double spinners. So far, they've shown to work really well in my program although I will abstract the adaptive step size into another, parent class so I can make AdaptiveDoubleSpinners and AdaptiveIntegerSpinners later.

随着 Vishal 的回答的改编,我制作了以下课程来制作漂亮的双旋转器。到目前为止,它们已经证明在我的程序中运行得非常好,尽管我会将自适应步长抽象到另一个父类中,以便稍后我可以制作 AdaptiveDoubleSpinners 和 AdaptiveIntegerSpinners。

import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class DoubleSpinner extends JSpinner {

    private static final long serialVersionUID = 1L;
    private static final double STEP_RATIO = 0.1;

    private SpinnerNumberModel model;

    public DoubleSpinner() {
        super();
        // Model setup
        model = new SpinnerNumberModel(0.0, -1000.0, 1000.0, 0.1);
        this.setModel(model);

        // Step recalculation
        this.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                Double value = getDouble();
                // Steps are sensitive to the current magnitude of the value
                long magnitude = Math.round(Math.log10(value));
                double stepSize = STEP_RATIO * Math.pow(10, magnitude);
                model.setStepSize(stepSize);
            }
        });
    }

    /**
     * Returns the current value as a Double
     */
    public Double getDouble() {
        return (Double)getValue();
    }

}

回答by Vishal K

Yes it is possible . See the example Given Below:
enter image description here

对的,这是可能的 。请参阅下面给出的示例:
在此处输入图片说明

import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;


public class SpinnerDemo extends JFrame 
{
    JSpinner s;
    SpinnerNumberModel model ;
    JSpinner.NumberEditor editor;
    JTextField stepText;
    JButton bStepSet;
    public void prepareAndShowGUI()
    {
        model = new SpinnerNumberModel(0.0,-1000.0 ,1000.0,0.1);
        s = new JSpinner(model);
        editor = new JSpinner.NumberEditor(s) ;
        s.setEditor(editor);
        stepText = new JTextField(10);
        bStepSet = new JButton("Set Step");
        bStepSet.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent evt)
            {
                try
                {
                    /* You can apply your stepSize deduction logic here*/
                    Double val = Double.parseDouble(stepText.getText().trim()); 
                    /*Setting the stepSize*/
                    model.setStepSize(val);
                }
                catch (Exception ex){}
            }
        });
        Container c = getContentPane();
        c.add(s);
        JPanel southPanel = new JPanel();
        southPanel.add(stepText);southPanel.add(bStepSet);
        c.add(southPanel,BorderLayout.SOUTH);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String args[]) 
    {
        SwingUtilities.invokeLater( new Runnable()
        {
            @Override
            public void run()
            {
                SpinnerDemo sd = new SpinnerDemo();
                sd.prepareAndShowGUI();
            }
        });
    }
}