📄 jcalendar.java
字号:
/*
* Copyright (C) 2004 Kai Toedter
* kai@toedter.com
* www.toedter.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.toedter.calendar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* JCalendar is a bean for entering a date by choosing the year, month and day.
*
* @author Kai Toedter
* @version 1.2
*/
public class JCalendar extends JPanel implements PropertyChangeListener {
private Calendar calendar;
/** the day chooser */
protected JDayChooser dayChooser;
private boolean initialized = false;
/** indicates if weeks of year shall be visible */
protected boolean weekOfYearVisible = true;
/** the locale */
protected Locale locale;
/** the month chooser */
protected JMonthChooser monthChooser;
private JPanel monthYearPanel;
/** the year chhoser */
protected JYearChooser yearChooser;
/**
* Default JCalendar constructor.
*/
public JCalendar() {
this(null, null, true, true);
}
/**
* JCalendar constructor specifying the month spinner type.
*
* @param monthSpinner false, if no month spinner should be used
*/
public JCalendar(boolean monthSpinner) {
this(null, null, monthSpinner, true);
}
/**
* JCalendar constructor which allows the initial date to be set.
*
* @param date the date
*/
public JCalendar(Date date) {
this(date, null, true, true);
}
/**
* JCalendar constructor allowing the initial locale to be set.
*
* @param locale the new locale
*/
public JCalendar(Locale locale) {
this(null, locale, true, true);
}
/**
* JCalendar constructor specifying both the initial date and locale.
*
* @param date the date
* @param locale the new locale
*/
public JCalendar(Date date, Locale locale) {
this(date, locale, true, true);
}
/**
* JCalendar constructor specifying both the initial date and the month
* spinner type.
*
* @param date the date
* @param monthSpinner false, if no month spinner should be used
*/
public JCalendar(Date date, boolean monthSpinner) {
this(date, null, monthSpinner, true);
}
/**
* JCalendar constructor specifying both the locale and the month spinner.
*
* @param locale the locale
* @param monthSpinner false, if no month spinner should be used
*/
public JCalendar(Locale locale, boolean monthSpinner) {
this(null, locale, monthSpinner, true);
}
/**
* JCalendar constructor with month spinner parameter.
*
* @param date the date
* @param locale the locale
* @param monthSpinner false, if no month spinner should be used
* @param weekOfYearVisible true, if weeks of year shall be visible
*/
public JCalendar(Date date, Locale locale, boolean monthSpinner,
boolean weekOfYearVisible) {
// needed for setFont() etc.
dayChooser = null;
monthChooser = null;
yearChooser = null;
this.weekOfYearVisible = weekOfYearVisible;
this.locale = locale;
if (locale == null) {
this.locale = Locale.getDefault();
}
calendar = Calendar.getInstance();
setLayout(new BorderLayout());
monthYearPanel = new JPanel();
monthYearPanel.setLayout(new BorderLayout());
monthChooser = new JMonthChooser(monthSpinner);
yearChooser = new JYearChooser();
monthChooser.setYearChooser(yearChooser);
monthYearPanel.add(monthChooser, BorderLayout.WEST);
monthYearPanel.add(yearChooser, BorderLayout.CENTER);
monthYearPanel.setBorder(BorderFactory.createEmptyBorder());
dayChooser = new JDayChooser(weekOfYearVisible);
dayChooser.addPropertyChangeListener(this);
monthChooser.setDayChooser(dayChooser);
monthChooser.addPropertyChangeListener(this);
yearChooser.setDayChooser(dayChooser);
yearChooser.addPropertyChangeListener(this);
add(monthYearPanel, BorderLayout.NORTH);
add(dayChooser, BorderLayout.CENTER);
// Set the initialized flag before setting the calendar. This will
// cause the other components to be updated properly.
if (date != null) {
calendar.setTime(date);
}
initialized = true;
setCalendar(calendar);
}
/**
* Creates a JFrame with a JCalendar inside and can be used for testing.
*
* @param s The command line arguments
*/
public static void main(String[] s) {
JFrame frame = new JFrame("JCalendar");
frame.getContentPane().add(new JCalendar());
frame.pack();
frame.setVisible(true);
}
/**
* Returns the calendar property.
*
* @return the value of the calendar property.
*/
public Calendar getCalendar() {
return calendar;
}
/**
* Gets the dayChooser attribute of the JCalendar object
*
* @return the dayChooser value
*/
public JDayChooser getDayChooser() {
return dayChooser;
}
/**
* Returns the locale.
*
* @return the value of the locale property.
*
* @see #setLocale
*/
public Locale getLocale() {
return locale;
}
/**
* Gets the monthChooser attribute of the JCalendar object
*
* @return the monthChooser value
*/
public JMonthChooser getMonthChooser() {
return monthChooser;
}
/**
* Returns "JCalendar".
*
* @return "JCalendar"
*/
public String getName() {
return "JCalendar";
}
/**
* Gets the yearChooser attribute of the JCalendar object
*
* @return the yearChooser value
*/
public JYearChooser getYearChooser() {
return yearChooser;
}
/**
* Indicates if the weeks of year are visible..
*
* @return boolean true, if weeks of year are visible
*/
public boolean isWeekOfYearVisible() {
return dayChooser.isWeekOfYearVisible();
}
/**
* JCalendar is a PropertyChangeListener, for its day, month and year
* chooser.
*
* @param evt the property change event
*/
public void propertyChange(PropertyChangeEvent evt) {
if (calendar != null) {
Calendar c = (Calendar) calendar.clone();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -