下面列出了怎么用javax.swing.JSpinner.DateEditor的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Creates new form ClockFrame
*/
ClockFrame() {
initComponents();
jSpinner1.setValue(new Date(System.currentTimeMillis()));
((DateEditor) jSpinner1.getEditor()).getFormat().applyPattern("dd.MM.yy HH:mm:ss.SSS");
tThread = new TimerThread(this);
tThread.start();
jCheckBox1.setSelected(GlobalOptions.getProperties().getBoolean("clock.alwaysOnTop"));
setAlwaysOnTop(jCheckBox1.isSelected());
cp = new ColoredProgressBar(0, 1000);
jPanel1.add(cp, BorderLayout.CENTER);
jComboBox1.setModel(new DefaultComboBoxModel(new String[]{"Alarm", "Homer", "LetsGo", "NHL", "Roadrunner", "Schwing", "Sirene", "StarTrek1", "StarTrek2"}));
// <editor-fold defaultstate="collapsed" desc=" Init HelpSystem ">
if (!Constants.DEBUG) {
GlobalOptions.getHelpBroker().enableHelpKey(getRootPane(), "pages.clock_tool", GlobalOptions.getHelpBroker().getHelpSet());
}
restoreTimers();
// </editor-fold>
}
@Override
public void stateChanged(ChangeEvent e) {
// for xAxisTime
if (useXAxisValue.isSelected()) {
annotation.setText(((DateEditor) xAxisTime.getEditor()).getFormat().format(xAxisTime.getValue()));
}
}
protected void addComponents(JPanel content, GridBagConstraints labelConstraints,
GridBagConstraints fieldConstraints) {
date = new JSpinner(new SpinnerDateModel(new Date(TimeHelper.today()), null, null, Calendar.DAY_OF_WEEK));
date.setEditor(new DateEditor(date, "MMM dd yyyy"));
JLabel dateLabel = new JLabel("Date:");
dateLabel.setFont(Styles.LABEL);
dateLabel.setHorizontalAlignment(SwingConstants.TRAILING);
content.add(dateLabel, labelConstraints);
content.add(date, fieldConstraints);
}
AbsoluteTimeIntervalPanel(NMONVisualizerGui gui) {
super(gui);
setLayout(new BorderLayout());
add.addActionListener(addInterval);
// start and end text boxes with labels, followed by a Add button
startLabel = new JLabel("Start:");
startLabel.setHorizontalAlignment(SwingConstants.TRAILING);
startLabel.setFont(Styles.LABEL);
start = new JSpinner(new SpinnerDateModel(new Date(getDefaultStartTime()), null, null, Calendar.MINUTE));
start.setEditor(new DateEditor(start, Styles.DATE_FORMAT_STRING_WITH_YEAR));
start.addChangeListener(intervalUpdater);
endLabel = new JLabel("End:");
endLabel.setHorizontalAlignment(SwingConstants.TRAILING);
endLabel.setFont(Styles.LABEL);
end = new JSpinner(new SpinnerDateModel(new Date(getDefaultEndTime()), null, null, Calendar.MINUTE));
end.setEditor(new DateEditor(end, Styles.DATE_FORMAT_STRING_WITH_YEAR));
end.addChangeListener(intervalUpdater);
JPanel namePanel = new JPanel();
namePanel.add(nameLabel);
namePanel.add(name);
JPanel startPanel = new JPanel();
startPanel.add(startLabel);
startPanel.add(start);
JPanel endPanel = new JPanel();
endPanel.add(endLabel);
endPanel.add(end);
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(add);
buttonsPanel.add(endToStart);
buttonsPanel.add(reset);
JPanel dataPanel = new JPanel();
dataPanel.setLayout(new GridBagLayout());
GridBagConstraints labelConstraints = new GridBagConstraints();
GridBagConstraints fieldConstraints = new GridBagConstraints();
labelConstraints.gridx = 0;
fieldConstraints.gridx = 1;
labelConstraints.gridy = 0;
fieldConstraints.gridy = 0;
labelConstraints.insets = new Insets(0, 0, 0, 5);
fieldConstraints.insets = new Insets(5, 0, 0, 5);
labelConstraints.fill = GridBagConstraints.HORIZONTAL;
fieldConstraints.fill = GridBagConstraints.HORIZONTAL;
dataPanel.add(startLabel, labelConstraints);
dataPanel.add(start, fieldConstraints);
++labelConstraints.gridy;
++fieldConstraints.gridy;
dataPanel.add(endLabel, labelConstraints);
dataPanel.add(end, fieldConstraints);
add(namePanel, BorderLayout.PAGE_START);
add(dataPanel, BorderLayout.CENTER);
add(buttonsPanel, BorderLayout.PAGE_END);
}
@Override
TimeZone getTimeZone() {
DateEditor de = (DateEditor) start.getEditor();
return de.getFormat().getTimeZone();
}
@Override
TimeZone getTimeZone() {
DateEditor de = (DateEditor) base.getEditor();
return de.getFormat().getTimeZone();
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
// all but timeZone fired by IntervalDocumentListeners
if ("interval".equals(evt.getPropertyName())) {
if (evt.getNewValue() == null) {
startLabel.setFont(Styles.LABEL_ERROR);
endLabel.setFont(Styles.LABEL_ERROR);
startLabel.setForeground(Styles.ERROR_COLOR);
endLabel.setForeground(Styles.ERROR_COLOR);
start.setForeground(Styles.ERROR_COLOR);
end.setForeground(Styles.ERROR_COLOR);
days.setForeground(Styles.ERROR_COLOR);
}
else {
startLabel.setFont(Styles.LABEL);
endLabel.setFont(Styles.LABEL);
startLabel.setForeground(Styles.DEFAULT_COLOR);
endLabel.setForeground(Styles.DEFAULT_COLOR);
start.setForeground(Styles.DEFAULT_COLOR);
end.setForeground(Styles.DEFAULT_COLOR);
days.setForeground(Styles.DEFAULT_COLOR);
}
}
else if ("start".equals(evt.getPropertyName())) {
startAbsolute.setText(FORMAT.format(new Date((Long) evt.getNewValue())));
}
else if ("end".equals(evt.getPropertyName())) {
endAbsolute.setText(FORMAT.format(new Date((Long) evt.getNewValue())));
}
else if ("timeZone".equals(evt.getPropertyName())) {
TimeZone timeZone = (TimeZone) evt.getNewValue();
DateEditor de = (DateEditor) base.getEditor();
de.getFormat().setTimeZone(timeZone);
// hack to get the spinner to fire a state change and update the displayed value
// toggle the calendar field back to its original value
((SpinnerDateModel) base.getModel()).setCalendarField(Calendar.MINUTE);
((SpinnerDateModel) base.getModel()).setCalendarField(Calendar.SECOND);
FORMAT.setTimeZone(timeZone);
startAbsolute.setText(FORMAT.format(new Date(getStartTime())));
endAbsolute.setText((FORMAT.format(new Date(getEndTime()))));
}
}
@Override
TimeZone getTimeZone() {
DateEditor de = (DateEditor) start.getEditor();
return de.getFormat().getTimeZone();
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("timeZone".equals(evt.getPropertyName())) {
TimeZone timeZone = (TimeZone) evt.getNewValue();
DateEditor de = (DateEditor) start.getEditor();
de.getFormat().setTimeZone(timeZone);
// hack to get the spinner to fire a state change and update the displayed value
// toggle the calendar field back to its original value
((SpinnerDateModel) start.getModel()).setCalendarField(Calendar.MINUTE);
((SpinnerDateModel) start.getModel()).setCalendarField(Calendar.SECOND);
FORMAT.setTimeZone(timeZone);
end.setText(FORMAT.format(getEndTime()));
}
else if ("values".equals(evt.getPropertyName())) {
long[] updatedValues = (long[]) evt.getNewValue();
boolean validDuration = true;
// 0 days, then duration must be valid & non-zero
if (updatedValues[1] == 0) {
// duration
if (updatedValues[0] < 1) {
validDuration = false;
}
}
if (validDuration) {
durationLabel.setFont(Styles.LABEL);
durationLabel.setForeground(Styles.DEFAULT_COLOR);
duration.setForeground(Styles.DEFAULT_COLOR);
days.setForeground(Styles.DEFAULT_COLOR);
}
else {
durationLabel.setFont(Styles.LABEL_ERROR);
durationLabel.setForeground(Styles.ERROR_COLOR);
duration.setForeground(Styles.ERROR_COLOR);
days.setForeground(Styles.ERROR_COLOR);
}
// repeat
if (updatedValues[2] == 0) {
repeatLabel.setFont(Styles.LABEL_ERROR);
repeatLabel.setForeground(Styles.ERROR_COLOR);
repeat.setForeground(Styles.ERROR_COLOR);
offset.setEnabled(false);
}
else {
repeatLabel.setFont(Styles.LABEL);
repeatLabel.setForeground(Styles.DEFAULT_COLOR);
repeat.setForeground(Styles.DEFAULT_COLOR);
if (updatedValues[2] == 1) {
// repeating once => no offset
offset.setEnabled(false);
}
else {
offset.setEnabled(true);
}
}
// offset: -1 => invalid
if (updatedValues[3] < 0) {
offsetLabel.setFont(Styles.LABEL_ERROR);
offsetLabel.setForeground(Styles.ERROR_COLOR);
offset.setForeground(Styles.ERROR_COLOR);
}
else {
offsetLabel.setFont(Styles.LABEL);
offsetLabel.setForeground(Styles.DEFAULT_COLOR);
offset.setForeground(Styles.DEFAULT_COLOR);
}
// end time
if (updatedValues[4] != -1) {
end.setText(FORMAT.format(updatedValues[4]));
}
}
}