1

怎么可能一步为 JSpinner 指定(例如 10 分钟而不是 1 分钟)

有一个模型允许为数字选择步长

SpinnerNumberModel(value, min, max, step); 

但是如何为日期设置呢?

这段代码

JSpinner timeSpinner = new JSpinner( new SpinnerDateModel() );
DateEditor timeEditor = new DateEditor(timeSpinner, "HH:mm:ss");
timeSpinner.setEditor(timeEditor);
timeSpinner.setValue(new Date()); // will only show the current time

(从这个答案中,Java Swing 是否有任何好的免费日期和时间选择器可用?

仅允许编辑时间,但步长始终为 1 小时、1 分钟或 1 秒,具体取决于是否指定了“HH”、“HH:mm”或“HH:mm:ss”。


有没有什么简单的方法可以让 min、 max 和 step 持续几分钟?

谢谢

4

3 回答 3

1

用于DateEditor.getModel()获取SpinnerDateModelwhich 提供setStart,setEndsetCalendarField.

于 2012-04-27T18:53:38.320 回答
1

正如您在以下代码中看到的SpinnerDateModel

public Object getNextValue() {
    Calendar cal = Calendar.getInstance(); //Current date.
    cal.setTime(value.getTime()); //Set the date to the current value of the model.
    cal.add(calendarField, 1); //Increment the date by 1 unit of the selected calendar field (e.g. 1 month).
    Date next = cal.getTime(); //Convert back to Date Object.
    return ((end == null) || (end.compareTo(next) >= 0)) ? next : null;
}

public Object getPreviousValue() {
    Calendar cal = Calendar.getInstance(); //Current date.
    cal.setTime(value.getTime()); //Set the date to the current value of the model.
    cal.add(calendarField, -1); //Decrement the date by 1 unit of the selected calendar field (e.g. 1 month).
    Date prev = cal.getTime(); //Convert back to Date Object.
    return ((start == null) || (start.compareTo(prev) <= 0)) ? prev : null;
}

它适用于添加或减去指定日历字段的 1 个单位。
您想将此数字 1 自定义为其他内容。
所以我在这里看到两个选项:

  1. 重新实现AbstractSpinnerModel. 只需复制粘贴SpinnerDateModel(它并不大),然后例如引入整数字段“step”,而不是数字 1,只需将“step”放入getNextand中getPrevious
  2. 实现 a SpinnerDateModel,它也可以在内部与 a 一起使用SpinnerDateModel。它会小很多,但我猜有点hackish。

遵循这样的代码SpinnerDateModel(案例2):

import java.awt.GridLayout;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;

public class MultiStepDateSpinner extends JPanel {
    private static class StepperSpinnerDateModel extends SpinnerDateModel {
        private final SpinnerDateModel internal; //We let the internal SpinnerDateModel do the work for us.
        private final int step; //The number of steps to increment and decrement per click.
        private Object currentValue; //Needed to get restored each time getPreviousValue and getNextValue is called.

        private StepperSpinnerDateModel(final Date value, final Comparable start, final Comparable end, final int calendarField, final int step) {
            internal = new SpinnerDateModel(value, start, end, calendarField);
            if (step <= 0)
                throw new IllegalArgumentException("Non positive step.");
            this.step = step;
            currentValue = internal.getValue();
        }

        private StepperSpinnerDateModel(final int step) {
            this(new Date(), null, null, Calendar.DAY_OF_MONTH, step);
        }

        @Override
        public Object getValue() {
            return currentValue;
        }

        @Override
        public void setValue(final Object value) {
            internal.setValue(value);
            currentValue = value;
            fireStateChanged(); //Important step for the spinner to get updated each time the model's value changes.
        }

        @Override
        public Object getNextValue() {
            Object next = null;
            for (int i=0; i<step; ++i) { //Calculate step next values:
                next = internal.getNextValue();
                internal.setValue(next); //We have to set the next value internally, in order to recalculate the next-next value in the next loop.
            }
            internal.setValue(currentValue); //Restore current value.
            return next;
        }

        @Override
        public Object getPreviousValue() {
            Object prev = null;
            for (int i=0; i<step; ++i) { //Calculate step previous values:
                prev = internal.getPreviousValue();
                internal.setValue(prev); //We have to set the previous value internally, in order to recalculate the previous-previous value in the next loop.
            }
            internal.setValue(currentValue); //Restore current value.
            return prev;
        }
    }

    private MultiStepDateSpinner() {
        super(new GridLayout(0, 1));

        //Increment and decrement by 4 minutes each step.
        //The null values indicate there shall be no minimum nor maximum date.
        //The current value is set to the current date.
        final JSpinner spinner = new JSpinner(new StepperSpinnerDateModel(new Date(), null, null, Calendar.MINUTE, 4));

        final JButton getValueButton = new JButton("Get value");
        getValueButton.addActionListener(e -> {
            JOptionPane.showMessageDialog(null, spinner.getValue(), "Got value", JOptionPane.PLAIN_MESSAGE);
        });

        add(spinner);
        add(getValueButton);
    }

    public static void main(final String[] args) {
        final JFrame frame = new JFrame("Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MultiStepDateSpinner());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

上面的示例代码是可运行的,因此您可以在实践中看到它。

为什么不让它成为一个AbstractSpinnerModel而不是SpinnerDateModel?因为我们需要将其标识为的实例,SpinnerDateModel所以JSpinner内部默认DateEditor为编辑器分配 a。
即使您扩展AbstractSpinnerModel并为微调器提供 a DateEditor,您也会收到一个异常,抱怨模型不是 a SpinnerDateModel

但在我看来,最干净的方法是重新实现AbstractSpinnerModel并复制粘贴SpinnerDateModel如果需要的代码(不是很大),并引入您认为的任何字段。

于 2018-04-04T21:14:01.090 回答
0
Incrementa JSpinnerDateEditor_15_+_15_minutos
    Calendar Horacalendario = Calendar.getInstance();
    Horacalendario.setTime(cita_hora.getDate());
    Date HoraParaCita = Horacalendario.getTime();//para asignar luego del incremento de 15 formato date()'
    int minutos = Horacalendario.get(Calendar.MINUTE);
    Horacalendario.set(Horacalendario.get(Calendar.YEAR), Horacalendario.get(Calendar.MONTH),
            Horacalendario.get(Calendar.DATE), Horacalendario.get(Calendar.HOUR), 0);//hago cero los minutos
    if (minutos >= 1 & minutos <= 14) {
        Horacalendario.add(Calendar.MINUTE, 15); //minutos A Sumar 
        HoraParaCita = Horacalendario.getTime();
        cita_hora.setDate(HoraParaCita);
    }
    if (minutos >= 16 & minutos <= 29) {
        Horacalendario.add(Calendar.MINUTE, 30);
        HoraParaCita = Horacalendario.getTime();
        cita_hora.setDate(HoraParaCita);
    }
    if (minutos >= 31 & minutos <= 44) {
        Horacalendario.add(Calendar.MINUTE, 45);
        HoraParaCita = Horacalendario.getTime();
        cita_hora.setDate(HoraParaCita);
    }
    if (minutos >= 46 & minutos <= 59) {
        Horacalendario.set(Horacalendario.get(Calendar.YEAR), Horacalendario.get(Calendar.MONTH),
                Horacalendario.get(Calendar.DATE), Horacalendario.get(Calendar.HOUR), 0);//hago cero los minutos
        HoraParaCita = Horacalendario.getTime();
        cita_hora.setDate(HoraParaCita);
    }
于 2020-08-22T02:23:18.270 回答