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

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

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

可能吗?还是我应该问,值得麻烦吗?

更新:

适应了Vishal的回答,我制作了以下 class 来使双陀螺仪更好。到目前为止,尽管我将自适应步长抽象到另一个父类中,但它们在我的程序中确实表现良好,因此以后可以制作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();
    }

}

最佳答案

是的,有可能 。请参见下面给出的示例:

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();
            }
        });
    }
}

关于java - JSpinner双重格式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15176695/

10-08 23:56