java月历小插件.docx
- 文档编号:2431533
- 上传时间:2023-05-03
- 格式:DOCX
- 页数:18
- 大小:35.92KB
java月历小插件.docx
《java月历小插件.docx》由会员分享,可在线阅读,更多相关《java月历小插件.docx(18页珍藏版)》请在冰点文库上搜索。
java月历小插件
在这里我用java做了一个简单的月历小插件,
效果图如下:
此月历可现实基本的年份,月份,日期等信息,插件小亮点是显示实时时间。
下面给出相关完整java源文件
下面是月历的:
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importChineseDate.*;
importjava.util.Calendar;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.Timer;
importjava.util.TimerTask;
publicclassCalendarJFrameextendsJFrameimplementsActionListener
{
privateintN=6,M=42;//存放日期的面板数,存放月份的文本数
Calendarnow=Calendar.getInstance();//获得当前日历对象
privateJTextFieldtext_data;//年份月份文本行
privateJLabeljlabel_week;////星期文本行
privateJTextFieldtext_day[]=newJTextField[M];//日期文本行
privateJButtonbutton_a,button_b;//月份切换键
privateJButtonbutton_c;//回到当前日历
privateJTextFieldtext_time;//实时时间文本行
intyear=now.get(Calendar.YEAR);//当前年份
intmonth=now.get(Calendar.MONTH)+1;//当前月份
intdate=now.get(Calendar.DATE);//当前日期
intY=year,MO=month,D=date;//保存当前年份,月份,日期
intdays1=ChineseDate.DayOfMonth(year,month-1);//上月天数
intdays2=ChineseDate.DayOfMonth(year,month);//当月天数
intDAYS1=days1,DAYS2=days2;//保存天数
intw=now.get(Calendar.DAY_OF_WEEK)-1;//当前星期
intweek=(w-date%7+8)%7;//当月第一天星期
intWEEK=week;//保存week
privateColorbg1=Color.pink;//颜色格式
privateColorbg2=Color.cyan;
privateColorbg3=Color.WHITE;
privateColorbg4=Color.orange;
privateColorbg5=Color.RED;
privateJPaneljpanel[]=newJPanel[N];//六个面板放日期
privateJPaneljpanel_time=newJPanel();//放实时时间面板
publicCalendarJFrame()//构造函数
{
/*******设置框架属性*******/
super("月历插件");
this.setBounds(300,240,205,200);//框架大小和位置
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);//框架尺寸不可变
this.getContentPane().setLayout(newGridLayout(9,1));//设置框架为9行1列
/*******设置框架属性结束*******/
/*******添加年份月份组件*******/
button_a=newJButton("◀");
button_a.addActionListener(this);//注册监听器
button_b=newJButton("▶");
button_b.addActionListener(this);
text_data=newJTextField(year+"年"+month+"月");
text_data.setHorizontalAlignment(JTextField.CENTER);//设置文本行内文字格式为居中
text_data.setBackground(bg1);//设置文本行背景色
text_data.setEditable(false);//设置文本行不可编辑
JPanelpanel1=newJPanel();//新建面板,用来摆放月份切换键和年份月份文本
GridBagLayoutgr=newGridBagLayout();//GridBagLayout布局方式
GridBagConstraintsgc=newGridBagConstraints();
gc.fill=GridBagConstraints.BOTH;
panel1.setLayout(gr);//panel1使用GridBagLayout布局方式
this.getContentPane().add(panel1);//将panel1加入框架第一行
gc.weightx=1;
gc.weighty=1;
gr.setConstraints(button_a,gc);//button_a的长权重为1
panel1.add(button_a);
gc.weightx=7;
gc.weighty=1;
gr.setConstraints(text_data,gc);//button_a的长权重为7
panel1.add(text_data);
gc.weightx=1;
gc.weighty=1;
gr.setConstraints(button_b,gc);//button_a的长权重为1
panel1.add(button_b);
/*******添加年份月份组件结束*******/
/*******添加星期组件*******/
jlabel_week=newJLabel("日一二三四五六");
this.getContentPane().add(jlabel_week);//将星期标签加入框架第二行
/*******添加星期组件结束*******/
/*******添加日期组件*******/
inttemp1=days1-week+1;//当月一号前面的日期
inttemp2=1;
for(intn=0;n { jpanel[n]=newJPanel(); jpanel[n].setLayout(newGridLayout(1,7)); this.getContentPane().add(jpanel[n]); } for(intj=0;j { if(j { text_day[j]=newJTextField(); text_day[j].setHorizontalAlignment(JTextField.CENTER); //text_day[j].setText(""+temp1); text_day[j].setText(""); text_day[j].setEditable(false); jpanel[j/7].add(text_day[j]); temp1++; } if(j>=week&&j { text_day[j]=newJTextField(); text_day[j].setHorizontalAlignment(JTextField.CENTER); text_day[j].setText(""+(j-week+1)); text_day[j].setEditable(false); jpanel[j/7].add(text_day[j]); if(j%7==0||j%7==6) text_day[j].setForeground(bg4); if(year==Y&&month==MO&&(j-week+1)==date) text_day[j].setBackground(bg2); } if(j>=days2+week)//当月最后一天以后 { text_day[j]=newJTextField(); text_day[j].setHorizontalAlignment(JTextField.CENTER); //text_day[j].setText(""+temp2); text_day[j].setText(""); text_day[j].setEditable(false); jpanel[j/7].add(text_day[j]); temp2++; } } /*******添加日期组件结束*******/ /*******添加实时时间组件*******/ jpanel_time.setLayout(newGridLayout(1,2)); text_time=newJTextField(""); classMyTaskextendsTimerTask { @Override publicvoidrun() { SimpleDateFormatsdf=newSimpleDateFormat("时间: HH: mm: ss"); Strings=sdf.format(newDate()); text_time.setText(s); } } this.getContentPane().add(jpanel_time); jpanel_time.add(text_time); text_time.setBorder(BorderFactory.createLoweredBevelBorder()); text_time.setEditable(false); text_time.setBackground(bg3); text_time.setForeground(bg5); text_time.setHorizontalAlignment(JTextField.CENTER); Timert=newTimer(); t.schedule(newMyTask(),1000,1000); /*******添加实时时间组件结束*******/ /*******添加回到当前日历按钮*******/ button_c=newJButton("今天"); button_c.addActionListener(this); jpanel_time.add(button_c); /*******添加回到当前日历结束*******/ this.setVisible(true);//框架可见 } publicvoidactionPerformed(ActionEvente)//单击事件处理方法 { if(e.getSource()==button_a)//月份减1 { if(month>1) month--; else { month=12; year--; } text_data.setText(year+"年"+month+"月"); days1=ChineseDate.DayOfMonth(year,month-1);//上月天数 days2=ChineseDate.DayOfMonth(year,month);//当月天数 week=(week-days2%7+7)%7;//计算上个月第一天的星期 inttemp1=days1-week+1; inttemp2=1; for(intj=0;j { if(j { text_day[j].setBackground(null); //text_day[j].setText(""+temp1); text_day[j].setText(""); jpanel[j/7].add(text_day[j]); temp1++; } if(j>=week&&j<(days2+week)) { text_day[j].setBackground(null); text_day[j].setText(""+(j-week+1)); jpanel[j/7].add(text_day[j]); if(j%7==0||j%7==6) text_day[j].setForeground(bg4); if(year==Y&&month==MO&&(j-week+1)==date) text_day[j].setBackground(bg2); } if(j>=(days2+week)) { text_day[j].setBackground(null); //text_day[j].setText(""+temp2); text_day[j].setText(""); jpanel[j/7].add(text_day[j]); temp2++; } } } if(e.getSource()==button_b)//月份加1 { if(month<12) month++; else { month=1; year++; } text_data.setText(year+"年"+month+"月"); if(month==1) days1=ChineseDate.DayOfMonth(year,12);//上月天数 else days1=ChineseDate.DayOfMonth(year,month-1);//上月天数 days2=ChineseDate.DayOfMonth(year,month);//当月天数 week=(week+days1%7)%7;//计算下个月第一天的星期 inttemp1=days1-week+1; inttemp2=1; for(intj=0;j { if(j { text_day[j].setBackground(null); //text_day[j].setText(""+temp1); text_day[j].setText(""); jpanel[j/7].add(text_day[j]); temp1++; } if(j>=week&&j<(days2+week)) { text_day[j].setBackground(null); text_day[j].setText(""+(j-week+1)); jpanel[j/7].add(text_day[j]); if(j%7==0||j%7==6) text_day[j].setForeground(bg4); if(year==Y&&month==MO&&(j-week+1)==date) text_day[j].setBackground(bg2); } if(j>=(days2+week)) { text_day[j].setBackground(null); //text_day[j].setText(""+temp2); text_day[j].setText(""); jpanel[j/7].add(text_day[j]); temp2++; } } } if(e.getSource()==button_c)//重置日历,回到当前月份 { days1=DAYS1; days2=DAYS2; week=WEEK; year=Y; month=MO; text_data.setText(year+"年"+month+"月"); inttemp1=days1-week+1; inttemp2=1; for(intj=0;j { if(j { //text_day[j].setText(""+temp1); text_day[j].setText(""); text_day[j].setEditable(false); jpanel[j/7].add(text_day[j]); temp1++; } if(j>=week&&j { text_day[j].setText(""+(j-week+1)); text_day[j].setEditable(false); jpanel[j/7].add(text_day[j]); if(j%7==0||j%7==6) text_day[j].setForeground(bg4); if(year==Y&&month==MO&&(j-week+1)==date) text_day[j].setBackground(bg2); } if(j>=days2+week) { //text_day[j].setText(""+temp2); text_day[j].setText(""); text_day[j].setEditable(false); jpanel[j/7].add(text_day[j]); temp2++; } } } } publicstaticvoidmain(String[]args) { newCalendarJFrame(); } } 下面是上面月历类用到的ChineseDate类 packageChineseDate; importjava.util.Date; importjava.util.Calendar; importjava.text.SimpleDateFormat; publicclassChineseDate{ privateCalendaraday=Calendar.getInstance(); publicChineseDate(intyear,intmonth,intday)throwsDateException//构造方法,指定日期 { this.set(year,month,day); } publicChineseDate()//默认构造方法 { aday=Calendar.getInstance();//获得当天日期 } publicChineseDate(ChineseDated)throwsDateException//拷贝构造函数 { this.set(d); } publicvoidset(intyear,intmonth,intday)throwsDateException//设置正确日期 { if(year<=0||year>=2500) thrownewDateException("年份不适合,有效年份为0~2500."); if(month<1||month>12) thrownewDateException("月份错误"); if(day<1||day>31)//daysOfMonth(year,month)) thrownewDateException("日期错误"); this.aday.set(year,month,day);//调用Calendar类的set() } publicvoidset(ChineseDated)throwsDateException {
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 月历 插件