0

好的,所以这个问题有两个部分,第一部分是我如何才能从 JSpinner 中返回分秒毫秒 (mm:ss.SSS)。我现在设置了一个 JSpinner,它在 JSpinner 中使用 java 的 DateEditor 格式,如下所示:

JSpinner spinner = new JSpinner();
spinner.setModel(new javax.swing.SpinnerDateModel());
spinner.setEditor(new javax.swing.JSpinner.DateEditor(spinner, "mm:ss.S"));
spinner.setBounds(420, 140, 228, 20);
contentPane.add(spinner);

但问题是,当我只想返回“minute:second.millisecond”而不是日期、月份或任何其他信息时,它将返回一个字符串,例如“Wed Jun 06 00:26:12 MDT 2012”。

问题的第二部分是我怎样才能只显示毫秒并将其更改为第十位,所以不是显示为 (mm:ss.SSS) 它显示为 (mm:ss.S) 并且 S 值可以是更改为 0 和 9 之间的值而不是 0 和 999 值也需要以这种方式返回。

4

3 回答 3

1

关于问题的第一部分......我不久前遇到了类似的问题,我需要一个可以管理小时/分钟/秒的微调器。

我编写了 JSpinner 类的新实现,您可以将其用作基础并对其进行更新以获取 Minutes/Seconds/Milliseconds 微调器。我按“原样”与您分享,它符合我的个人需求,但我当然可以改进:

/**
 * TimeSpinner
 * A implementation of JSpinner that manages
 * only the hour/minute part of a date.
 * Careful, in consequence the getValue()
 * gives you a date based on the 01/01/1970
 * ('issue' known about JSpinner).
 * => This spinner implements its own model. It allows to fix
 * a pb about the tick (the regular model SpinnerDateModel
 * increases/decreases hours instead of minutes.
 * => It overrides setBackground and make it works (the regular
 * JSpinner.setBackground does not).
 *
 * User: Yannick DECOURTRAY
 * Date: 21/06/11
 */
public class TimeSpinner extends JSpinner {

    /**
     * Constructor
     */
    public TimeSpinner() {
        Date date = today();
        TimeSpinnerModel sm = new TimeSpinnerModel(date, null, null, Calendar.MINUTE);
        setModel(sm);
        JSpinner.DateEditor de = new JSpinner.DateEditor(this, "h:mm a");
        setEditor(de);
    }

    /**
     * Calls setBackground on Editor of the spinner
     * @see javax.swing.JComponent#setBackground(java.awt.Color)
     */
    @Override
    public void setBackground(Color bg) {
        JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) getEditor();
        editor.getTextField().setBackground(bg);
    }

    /**
     * Model class
     */
    private class TimeSpinnerModel extends SpinnerDateModel {

        /**
         * Constructor
         * @param value Current date
         * @param start Low limite date
         * @param end High limit date
         * @param calendarField Step of incrementation
         */
        public TimeSpinnerModel(Date value,
                                Comparable start,
                                Comparable end,
                                int calendarField) {
            super(value, start, end, calendarField);
        }

        /** @see javax.swing.SpinnerDateModel#getNextValue() */
        public Object getNextValue() {
            Date currentDate = getDate();
            Calendar currentCal = Calendar.getInstance();
            currentCal.setTime(currentDate);
            int calField = Calendar.MINUTE;//getCalendarField();

            // Add calField to currentDate
            currentCal.add(calField, 1);
            Date newDate = new Date(currentCal.getTimeInMillis());

            Date endDate = (Date) getEnd();
            if (endDate != null && newDate.after(endDate))
                return currentDate;
            else
                return newDate;
        }

         /** @see javax.swing.SpinnerDateModel#getPreviousValue() */
        public Object getPreviousValue() {
            Date currentDate = getDate();
            Calendar currentCal = Calendar.getInstance();
            currentCal.setTime(currentDate);
            int calField = Calendar.MINUTE;//getCalendarField();

            // Add calField to currentDate
            currentCal.add(calField, -1);
            Date newDate = new Date(currentCal.getTimeInMillis());

            Date startDate = (Date) getStart();
            if (startDate != null && newDate.before(startDate))
                return currentDate;
            else
                return newDate;
        }
    }

    /**
     * Gets the today date
     *
     * @return the today date
     */
    private static Date today() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
}
于 2012-06-06T07:31:41.387 回答
1

您可以使用日历来提取分钟、秒和毫秒。这是一个例子:

SpinnerDateModel model = (SpinnerDateModel) spinner.getModel();
Calendar time = Calendar.getInstance();
time.setTime(model.getDate());
// Access minute, second, millisecond
time.get(Calendar.MINUTE);
time.get(Calendar.SECOND);
time.get(Calendar.MILLISECOND);

设置默认值应该如下工作:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);

SpinnerDateModel model = new SpinnerDateModel(cal.getTime(), null,
            null, Calendar.SECOND);

JSpinner spinner = new JSpinner();
spinner.setModel(model);
于 2012-06-06T06:56:12.743 回答
0

您应该使用自定义的 JSpinner.Editor,而不是使用 DateEditor。

这是一个与您想要的非常相似的示例:http ://www.java2s.com/Code/Java/Swing-JFC/AnexampleofJSpinnerwithacustomeditor.htm

于 2012-06-06T06:50:11.903 回答