3

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

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

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

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

更新:

通过改编 Vishal 的回答,我制作了以下课程来制作漂亮的双旋转器。到目前为止,它们在我的程序中表现得非常好,尽管我会将自适应步长抽象到另一个父类中,以便稍后制作 AdaptiveDoubleSpinner 和 AdaptiveIntegerSpinner。

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

}
4

1 回答 1

6

是的,有可能 。请参见下面给出的示例:
在此处输入图像描述

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();
            }
        });
    }
}
于 2013-03-02T17:59:27.990 回答