📄 jcalendarpanel.java
字号:
buttonpanel.add(yearRight, c);
return buttonpanel;
}
/**
* Creates a JComboBox filled with year values (1900-2100)
* @return JComboBox with Years
*/
private JComboBox createYear() {
JComboBox year = new JComboBox();
for (int i = 1900; i <= 2100; i++) {
year.addItem("" + i); //$NON-NLS-1$
}
year.setSelectedIndex(_cal.get(Calendar.YEAR) - 1900);
return year;
}
/**
* Creates a JComboBox filled with Months.
* The name for the Month is created using the locale given
* in the constructor.
*
* @return JComboBox filled with Months
*/
private JComboBox createMonth() {
JComboBox month = new JComboBox();
SimpleDateFormat format = new SimpleDateFormat("MMMMM", _locale);
// Probably should be using the same locale as the rest of the widget.
Calendar currentCal = Calendar.getInstance(_locale);
// Setting the day to 1 will avoids problems with leap years.
// If the calendar is created on March 30, 2004 (like I just did)
// then the day defaults to 30. When the month is set to 1 (February)
// the currentCal will *adjust* it to become March 1. This results
// in TWO entries with the value of \"March\" in the month combo.
currentCal.set(Calendar.DAY_OF_MONTH, 1);
for (int i = 0; i < 12; i++) {
currentCal.set(Calendar.MONTH, i);
currentCal.set(Calendar.YEAR, _cal.get(Calendar.YEAR));
String myString = format.format(currentCal.getTime());
month.addItem(myString);
}
month.setSelectedIndex(_cal.get(Calendar.MONTH));
return month;
}
/**
* Updates the Calendar
*/
private void updateCalendar() {
if (!_updating) {
_updating = true;
_cal.set(Calendar.MONTH, _month.getSelectedIndex());
_cal.set(Calendar.YEAR, _year.getSelectedIndex() + 1900);
_cal.set(Calendar.DAY_OF_MONTH, _monthPanel.getSelectedDayOfMonth());
_monthPanel.setCalendar(_cal);
_monthPanel.grabFocus();
_updating = false;
}
}
/**
* Returns the current selected Date as Calender-Object
*
* @return current selected Date
*/
public Calendar getCalendar() {
updateCalendar();
return _cal;
}
/**
* Sets the current selected Date
*
* @param cal the Date to select
*/
public void setCalendar(Calendar cal) {
_updating = true;
_cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH));
_cal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
_cal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
_monthPanel.setCalendar(_cal);
_year.setSelectedIndex(_cal.get(Calendar.YEAR) - 1900);
_month.setSelectedIndex(_cal.get(Calendar.MONTH));
_monthPanel.grabFocus();
_updating = false;
}
/**
* Returns a String-Representation of this Calendar using
* the DateFormat given in the Constructor
*
* @return String-Representation of this Calendar
*/
public String toString() {
updateCalendar();
return _format.format(_cal.getTime());
}
/**
* Returns a String-Representation of this Calendar
* using the given DateFormat
*
* @param format DateFormat to use
* @return String-Representation of this Calendar
*/
public String toString(DateFormat format) {
updateCalendar();
return format.format(_cal.getTime());
}
/**
* Recieves StateChanges from the ComboBoxes for Month/Year
* and updates the Calendar
*
* @param e ItemEvent
* @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
*/
public void itemStateChanged(ItemEvent e) {
updateCalendar();
if (_listenermode == FIRE_EVERYTIME) {
fireChangeEvent();
}
}
/**
* Recieves StateChanges from the MonthPanel and
* updates the Calendar
*
* @param e ChangeEvent
*
* @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
*/
public void stateChanged(ChangeEvent e) {
updateCalendar();
fireChangeEvent();
}
/**
* Adds a Changelistener to this JCalendarPanel.
*
* It will be called every time the selected Date
* changes.
*
* @param listener ChangeListener
*/
public void addChangeListener(ChangeListener listener) {
_changeListener.add(listener);
}
/**
* Removes a ChangeListener from this JCalendarPanel
*
* @param listener listener to remove
*/
public void removeChangeListener(ChangeListener listener) {
_changeListener.remove(listener);
}
/**
* Gets all ChangeListeners
* @return all ChangeListeners
*/
public ChangeListener[] getChangeListener() {
return (ChangeListener[]) _changeListener.toArray();
}
/**
* Fires the ChangeEvent
*/
protected void fireChangeEvent() {
if (!_fireingChangeEvent) {
_fireingChangeEvent = true;
ChangeEvent event = new ChangeEvent(this);
for (int i = 0; i < _changeListener.size(); i++) {
((ChangeListener) _changeListener.get(i)).stateChanged(event);
}
_fireingChangeEvent = false;
}
}
/**
* Sets the Mode when the FireChangeEvent is called.
* Use FIRE_EVERYTIME or FIRE_DAYCHANGES as parameter.
*
* @param mode The Mode of the Listener
*/
public void setListenerModus(int mode) {
_listenermode = mode;
}
/**
* Enables/Disables the Panel
* @param enabled Enabled ?
*/
public void setEnabled(boolean enabled) {
_month.setEnabled(enabled);
_year.setEnabled(enabled);
_monthPanel.setEnabled(enabled);
}
/**
* Is the Panel enabled ?
* @return enabled ?
*/
public boolean isEnabled() {
return _month.isEnabled();
}
/**
* Returns the Dateformat the Panel is using
* @return DateFormat
*/
public DateFormat getDateFormat() {
return _format;
}
/**
* Fires everytime the Date changes
*/
public static final int FIRE_EVERYTIME = 1;
/**
* Fires only if the Day changes
*/
public static final int FIRE_DAYCHANGES = 2;
/**
* When does FireEvent() fire events?
* Every time there is an update or only
* if the Day was changed?
*/
private int _listenermode = FIRE_EVERYTIME;
/**
* Current chang in progress?
*/
private boolean _updating = false;
/**
* The current Date
*/
private Calendar _cal;
/**
* The DateFormat for Output
*/
private DateFormat _format;
/**
* The Locale to use
*/
private Locale _locale;
/**
* The JComboBox for Month-Selection
*/
private JComboBox _month;
/**
* The JComboBox for Year-Selection
*/
private JComboBox _year;
/**
* The JMonthPanel for Day-Selection
*/
private JMonthPanel _monthPanel;
/**
* The list of ChangeListeners
*/
private ArrayList _changeListener = new ArrayList();
/**
* Currently firing an ChangeEvent?
*/
private boolean _fireingChangeEvent = false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -