📄 mycalender.java
字号:
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
public class MyCalender extends JPanel
implements ChangeListener , ItemListener{
DatePane dp = new DatePane();
JPanel settingPanel = new JPanel();
private static String monthNames[] = {
"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"
};
JComboBox monthBox = new JComboBox(monthNames);
SpinnerNumberModel model = new SpinnerNumberModel(2008, 0, 9999, 1);
JSpinner yearSpn = new JSpinner(model);
public MyCalender(){
JSpinner.NumberEditor yearEditor = new JSpinner.NumberEditor(yearSpn,"#### ");
yearEditor.getTextField().setEditable(false);
yearSpn.setEditor(yearEditor);
model.addChangeListener(this);
this.setBorder(new TitledBorder("日期"));
monthBox.addItemListener(this);
setLayout(new BorderLayout());
settingPanel.setLayout(new GridLayout(1,2,20,1));
monthBox.setSelectedIndex(dp.curDate.getMonth() - 1);
settingPanel.add(monthBox);
settingPanel.add(yearSpn);
add(settingPanel, BorderLayout.NORTH);
add(dp);
}
public static void main(String[] args){
JFrame JF=new JFrame("日历(苏静宇 制作)");
MyCalender t=new MyCalender();
JF.add(t);
JF.setSize(210,230);
JF.setLocation(250,100);
JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JF.setResizable(false);
JF.setVisible(true);
}
//监听年份的变化
public void stateChanged(ChangeEvent e) {
SpinnerModel source = (SpinnerModel)e.getSource();
dp.curDate.setYear((Integer)source.getValue());
dp.updateDay();
}
//监听月份的变化
public void itemStateChanged(ItemEvent event){
JComboBox sourceBox = (JComboBox)event.getSource();
dp.curDate.setMonth(sourceBox.getSelectedIndex() + 1);
dp.updateDay();
}
}
class DatePane extends JPanel
{
private JPanel JP1=new JPanel();
private JPanel JP2=new JPanel();
private JPanel JP3=new JPanel();
private JLabel center;
public MyDate curDate = new MyDate();
private static String[] weekDayNames = {"日","一","二","三","四","五","六"};
MyLabelListener listener = new MyLabelListener();
DatePane()
{
this.setLayout(null);
JP1.setLayout(new GridLayout(1,7));
for(int i = 0 ; i < weekDayNames.length; i++){
JLabel label = new JLabel(weekDayNames[i]);
JP1.add(label);
}
this.add(JP1);
JP1.setBounds(5,0,200,25);
updateDay();
}
//添加显示本月的月历
void put()
{
MyDate tmpDate = new MyDate(curDate.getYear() , curDate.getMonth() , 1);
int week = tmpDate.getWeekDay();
int dayNum = curDate.getDayNumOfMonth();
for(int i=1;i<week;i++)
{
JP2.add(new JLabel(""));
}
for(int i=1;i<=dayNum;i++)
{
JLabel j;
j=new JLabel(String.valueOf(i),JLabel.CENTER);
j.addMouseListener(listener);
if(i==curDate.getDay())
{
j.setForeground(Color.red);
}
JP2.add(j);
}
for(int i=dayNum+week;i<42;i++)
{
JP2.add(new JLabel(""));
}
}
//更新当前日期的显示
void updateDay()
{
JP2.removeAll();
JP2.setLayout(new GridLayout(6,7));
JP2.setBackground(Color.white);
this.add(JP2);
JP2.setBounds(0,25,195,100);
put();
JP3.removeAll();
JP3.setLayout(new BorderLayout());
center=new JLabel("今天:" + curDate ,JLabel.CENTER);
JP3.add(center,BorderLayout.CENTER);
this.add(JP3);
JP3.setBounds(2,124,185,35);
validate();
}
//标签的鼠标事件监听器
class MyLabelListener extends MouseAdapter{
public void mousePressed(MouseEvent e)
{
JLabel label = (JLabel) e.getSource();
label.setForeground(Color.white);
int day = Integer.parseInt(label.getText());
curDate.setDay(day);
updateDay();
}
public void mouseEntered(MouseEvent e)
{
JLabel label = (JLabel) e.getSource();
label.setForeground(Color.red);
}
public void mouseExited(MouseEvent e)
{
JLabel label = (JLabel) e.getSource();
label.setForeground(Color.black);
updateDay();
}
public void mouseReleased(MouseEvent e)
{
JLabel label = (JLabel) e.getSource();
label.setForeground(Color.blue);
updateDay();
}
}
}
class MyDate {
public final static int MAX_YEAR = 9999;
private int year; //年 0 - MAX_YEAR
private int month; //月 0-11
private int day; //日
Calendar C;
//初始化为系统时间
public MyDate(){
C=Calendar.getInstance();
day=C.get(Calendar.DAY_OF_MONTH);
month=C.get(Calendar.MONTH);
year=C.get(Calendar.YEAR);
}
//初始化为year,month,day
public MyDate(int year,int month , int day){
this.year=year;
this.month=month - 1;
this.day=day;
}
//得到今天是本周第几天
public int getWeekDay(){
GregorianCalendar G1=new GregorianCalendar(year,month,day);
int week=G1.get(Calendar.DAY_OF_WEEK);
return week;
}
//判断是否是闰年
public boolean isLeapYear(){
if((year%4==0&&year%100!=0)||(year%400==0))
return true;
else
return false;
}
//返回本月天数
public int getDayNumOfMonth(){
int lm[]= {31,29,31,30,31,30,31,31,30,31,30,31};
int m[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(month > 11|| month < 0) return -1;
int dayNum;
if (isLeapYear())
dayNum=lm[month];
else
dayNum=m[month];
return dayNum;
}
//得到日期
public int getDay(){
return day;
}
//设置日期
public void setDay(int day){
if (day<=getDayNumOfMonth()&&day>0)
this.day=day;
}
//得到月份
public int getMonth(){
return month +1;
}
//设置月份
public void setMonth(int month){
if (month> 0&&month<=12) {
this.month=month - 1;
day = Math.min(day, getDayNumOfMonth());
}
}
//得到年份
public int getYear(){
return year;
}
//设置年份
public void setYear(int year){
if (year<0||year>MAX_YEAR)
return ;
else
this.year=year;
}
//下一个月
public void nextMonth(){
if (month<11)
month++;
else
{
nextYear();
month=0;
}
day = Math.min(day, getDayNumOfMonth());
}
//下一年
public void nextYear(){
if (year>MAX_YEAR-1)
return ;
else
year++;
day = Math.min(day, getDayNumOfMonth());
}
//上一年
public void preYear(){
if (year>1)
year--;
else
return;
}
//重载 Object类的toString方法,将对象转换为字符串
public String toString(){
return "" + year + "年" + (month+1) + "月" + day + "日";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -