JSpinner采用双格式

时间:2013-03-02 16:24:07

标签: java swing jspinner jformattedtextfield

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

不仅仅是这个,但我希望步长应该是动态的,使用类似下面的公式(10%的大小):

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

有可能吗?或者我应该问,麻烦值得吗?

更新

通过调整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();
    }

}

1 个答案:

答案 0 :(得分:6)

是的,这是可能的。请参阅下面给出的示例:
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();
            }
        });
    }
}