在JSpinner上格式化日期

在JSpinner上格式化日期,第1张

在JSpinner上格式化日期

您已经为JSpinner定义了两件事(基本上是本教程的内容完全回答了您的问题)

  • SimpleDateFormat

  • SpinnerDateModel

例如

import java.awt.*;import java.text.SimpleDateFormat;import java.util.*;import javax.swing.*;import javax.swing.event.*;public class TimeZoneSpinners {    private final String[] zones = {"Asia/Tokyo", "Asia/Hong_Kong",        "Asia/Calcutta", "Europe/Paris", "Europe/London",        "America/New_York", "America/Los_Angeles"    };    private final JLabel[] labels = new JLabel[zones.length];    private final SimpleDateFormat[] formats = new SimpleDateFormat[zones.length];    private JSpinner spinner;    private SpinnerDateModel model;    private SimpleDateFormat format;    private JPanel panel;    private Jframe frame = new Jframe();    public void makeUI() {        Calendar cal = Calendar.getInstance();        Date date = cal.getTime();        model = new SpinnerDateModel();        model.setValue(date);        spinner = new JSpinner(model);        spinner.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) {     Date date = (Date) ((JSpinner) e.getSource()).getValue();     for (int i = 0; i < labels.length; i++) {         labels[i].setText(formats[i].format(date));     } }        });        format = ((JSpinner.DateEditor) spinner.getEditor()).getFormat();        format.setTimeZone(TimeZone.getTimeZone(zones[0]));        format.applyPattern("yyyy-MM-dd HH:mm:ss");        panel = new JPanel(new GridLayout(zones.length, 2, 10, 10));        for (int i = 0; i < zones.length; i++) { formats[i] = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss"); formats[i].setTimeZone(TimeZone.getTimeZone(zones[i])); JLabel label = new JLabel(zones[i]); labels[i] = new JLabel(formats[i].format(date)); panel.add(label); panel.add(labels[i]);        }        frame.setLayout(new BorderLayout(10, 10));        frame.add(spinner, BorderLayout.NORTH);        frame.add(panel, BorderLayout.CENTER);        frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);        frame.pack();        frame.setVisible(true);    }    public static void main(String[] args) {        SwingUtilities.invokeLater(new Runnable() { @Override public void run() {     new TimeZoneSpinners().makeUI(); }        });    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5506097.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存