基于java的桌面日历程序源码
用java实现简单的万年历输出的代码
![用java实现简单的万年历输出的代码](https://img.taocdn.com/s3/m/039b86ee4afe04a1b071dedf.png)
package clock;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class Lunar {private int year;private int month;private int day;private boolean leap;final static String chineseNumber[] = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"};static SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy年MM月dd 日");final static long[] lunarInfo = new long[]{0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977,0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970,0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950,0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557,0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0,0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6,0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570,0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0,0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5,0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520,0x0dd45,0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0};//====== 传回农历y年的总天数final private static int yearDays(int y) {int i, sum = 348;for (i = 0x8000; i > 0x8; i >>= 1) {if ((lunarInfo[y - 1900] & i) != 0) sum += 1;}return (sum + leapDays(y));}//====== 传回农历y年闰月的天数final private static int leapDays(int y) {if (leapMonth(y) != 0) {if ((lunarInfo[y - 1900] & 0x10000) != 0)return 30;elsereturn 29;} elsereturn 0;}//====== 传回农历y年闰哪个月1-12 , 没闰传回0final private static int leapMonth(int y) {return (int) (lunarInfo[y - 1900] & 0xf);}//====== 传回农历y年m月的总天数final private static int monthDays(int y, int m) {if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0)return 29;elsereturn 30;}//====== 传回农历y年的生肖final public String animalsYear() {final String[] Animals = new String[]{"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};return Animals[(year - 4) % 12];}//====== 传入月日的offset 传回干支, 0=甲子final private static String cyclicalm(int num) {final String[] Gan = new String[]{"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};final String[] Zhi = new String[]{"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};return (Gan[num % 10] + Zhi[num % 12]);}//====== 传入offset 传回干支, 0=甲子final public String cyclical() {int num = year - 1900 + 36;return (cyclicalm(num));}/** *//*** 传出y年m月d日对应的农历.* yearCyl3:农历年与1864的相差数?* monCyl4:从1900年1月31日以来,闰月数* dayCyl5:与1900年1月31日相差的天数,再加40 ?* @param cal* @return*/public Lunar(Calendar cal) {@SuppressWarnings("unused") int yearCyl, monCyl, dayCyl;int leapMonth = 0;Date baseDate = null;try {baseDate = chineseDateFormat.parse("1900年1月31日");} catch (ParseException e) {e.printStackTrace(); //To change body of catch statement use Options | File Templates.}//求出和1900年1月31日相差的天数int offset = (int) ((cal.getTime().getTime() - baseDate.getTime()) / 86400000L);dayCyl = offset + 40;monCyl = 14;//用offset减去每农历年的天数// 计算当天是农历第几天//i最终结果是农历的年份//offset是当年的第几天In用java实现简单的万年历输出的代码(2009-12-14 19:08:55)转载标签:分类:计算机类itimport java.util.Scanner;public class Zuizhong{public static void main(String[] args){Scanner input=new Scanner(System.in);System.out.println("--------------------------欢迎使用万年历程序----------------------"); System.out.print("请输入年份:");int year=input.nextInt();System.out.print("\n请输入月份:");int month=input.nextInt();//打印换行符System.out.println();//计算1900年1月1日到指定年份前一年的天数int totalDays=0;//判断是否是1900后的年份if(year>=1900){for(int i=1900;i<year;i++){//判断是否闰年,闰年加366天,否则加365天if((i%4==0 && i%100!=0)||(i%400==0))totalDays+=366;else totalDays+=365;}//计算指定年份1月到指定月份1号之间的天数int daysOfMonth=0;int days;for(int i=1;i<month;i++){switch(i){case 2:if((year%4==0 && year%100!=0)|| year%400==0) days=29;else days=28;break;case 4:case 6:case 9:case 11:days=30;break;default:days=31;}daysOfMonth+=days;}//获得指定年月的天数switch(month){case 2:if((year%4==0 && year%100!=0)|| year%400==0) days=29;else days=28;break;case 4:case 6:case 9:case 11:days=30;break;default:days=31;}//1900.1.1到指定年月1号之间的总天数totalDays+=daysOfMonth;//计算指定年月1号的星期数int firstDay=(totalDays)%7+1;//上一行算出的星期数是1到7,因此要转换成0-6,即星期日=0if(firstDay==7)firstDay=0;//显示月历System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");//打印1号之前的空格for(int i=0;i<firstDay;i++)System.out.print("\t");//打印月历for(int i=1;i<=days;i++){System.out.print(i+"\t");//如果是星期六,换行if((i-1)%7+firstDay==6)System.out.println();}}else if(year>0&&year<1900){for(int i=1899;i>year;i--){//判断是否闰年,闰年加366天,否则加365天if((i%4==0 && i%100!=0)||(i%400==0))totalDays+=366;else totalDays+=365;}//计算指定年份12月到指定月份31号之后的天数int daysOfMonth=0;int days;for(int i=12;i>=month;i--){switch(i){case 2:if((year%4==0 && year%100!=0)|| year%400==0) days=29;else days=28;break;case 4:case 6:case 9:case 11:days=30;break;default:days=31;}daysOfMonth+=days;}//获得指定年月的天数switch(month){case 2:if((year%4==0 && year%100!=0)|| year%400==0) days=29;else days=28;break;case 4:case 6:case 9:case 11:days=30;break;default:days=31;}//1900.1.1到指定年月1号之间的总天数totalDays+=daysOfMonth;//计算指定年月1号的星期数int firstDay=8-(totalDays)%7;//上一行算出的星期数是1到7,因此要转换成0-6,即星期日=0if(firstDay==7)firstDay=0;if(firstDay==8)firstDay=1;//显示月历System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");//打印1号之前的空格for(int i=0;i<firstDay;i++)System.out.print("\t");//打印月历for(int i=1;i<=days;i++){System.out.print(i+"\t");//如果是星期六,换行if((i-1)%7+firstDay==6)System.out.println();}}System.out.println("\n程序结束");}}(完)在myeclipse运行效果图:--------------------------欢迎使用万年历程序---------------------- 请输入年份:2009请输入月份:12星期日星期一星期二星期三星期四星期五星期六1 2 3 4 56 7 8 9 10 11 1213 14 15 16 17 18 1920 21 22 23 24 25 2627 28 29 30 31程序结束。
JAVA课程设计 万年历 源代码
![JAVA课程设计 万年历 源代码](https://img.taocdn.com/s3/m/0f7dbef4f021dd36a32d7375a417866fb84ac0f4.png)
测试用例设计:根据 需求文档和功能描述, 设计出能够覆盖所有 功能的测试用例
测试工具:使用JUnit 等测试框架进行单元 测试,使用Selenium 等工具进行UI测试
测试结果分析:根 据测试结果,分析 代码存在的问题, 并进行修改和优化
集成测试:验证各个模块之间的接口是否正确,数据传输是否正常 性能测试:测试系统的响应时间、吞吐量、资源利用率等性能指标
提醒功能:用户可以设置提醒功能,在节日或假期到来之前,系统会自动提醒用户。
删除事件:用户可以删除不 再需要的事件
编辑事件:用户可以对已添加 的事件进行编辑,如修改事件 名称、时间等
添加事件:用户可以在万年历 中添加新的事件,如生日、纪 念日等
查询事件:用户可以查询特定 日期或时间段内的事件,如查
界面显示:万年历界面将显示年、 月、日、星期等信息,用户可以通 过点击相应的按钮来切换日期。
添加标题
添加标题
添加标题
添加标题
系统响应:当用户输入日期后,系统 将根据输入的日期显示相应的万年历 信息,包括年、月、日、星期等信息。
用户操作:用户可以通过点击相应 的按钮来切换日期,系统将根据用 户的操作显示相应的万年历信息。
添加标题
界面设计:简洁明了,易于阅读
添加标题
添加标题
交互性:用户可以选择查看不同日 期的日历信息
功能描述:在万年历中,用户可以选择标注节日和假期,以便于查看和提醒。
节日标注:用户可以在万年历中设置自己喜欢的节日,如春节、中秋节等,系统会自动 标注这些节日。
假期标注:用户可以在万年历中设置自己的假期,如年假、病假等,系统会自动标注这 些假期。
,a click to unlimited possibilities
万年历-Java实现
![万年历-Java实现](https://img.taocdn.com/s3/m/f4da96601eb91a37f1115ca1.png)
万年历2.40.1 源程序import java.util.Scanner;public class PrintCalendar7 {public static void main(String[] args) {System.out.println("******************欢迎使用万年历******************");Scanner input = new Scanner(System.in);System.out.print("\n请选择年份: ");int year = input.nextInt();System.out.print("\n请选择月份: ");int month = input.nextInt();System.out.println();int days = 0; // 存储当月的天数boolean isRn;/* 判断是否是闰年 */if (year % 4 == 0 && !(year % 100 == 0) || year % 400 == 0) { // 判断是否为闰年isRn = true; // 闰年} else {isRn = false;// 平年}if(isRn){System.out.println(year + "\t闰年");} else {System.out.println(year + "\t平年");}/* 计算该月的天数 */switch (month) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:days = 31;break;if (isRn) {days = 29;} else {days = 28;}break;default:days = 30;break;}System.out.println(month + "\t共" + days + "天");/* 计算输入的年份之前的天数 */int totalDays = 0;for (int i = 1900; i < year; i++) {/* 判断闰年或平年,并进行天数累加 */if (i % 4 == 0 && !(i % 100 == 0) || i % 400 == 0) { // 判断是否为闰年totalDays = totalDays + 366; // 闰年366天} else {totalDays = totalDays + 365; // 平年365天}}System.out.println("输入年份距离1900年1月1日的天数:"+ totalDays);/* 计算输入月份之前的天数 */int beforeDays = 0;for (int i = 1; i <= month; i++) {switch (i) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:days = 31;break;case 2:if (isRn) {days = 29;} else {days = 28;break;default:days = 30;break;}if (i < month) {beforeDays = beforeDays + days;}}totalDays = totalDays + beforeDays; // 距离1900年1月1日的天数System.out.println("输入月份距离1900年1月1日的天数:"+ totalDays);System.out.println("当前月份的天数:" + days);/* 计算星期几 */int firstDayOfMonth; // 存储当月第一天是星期几:星期日为0,星期一~星期六为1~6int temp = 1 + totalDays % 7; // 从1900年1月1日推算if (temp == 7) { // 求当月第一天firstDayOfMonth = 0; // 周日} else {firstDayOfMonth = temp;}System.out.println("该月第一天是: " + firstDayOfMonth);/* 输出日历 */System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t 星期六");for (int nullNo = 0; nullNo < firstDayOfMonth; nullNo++) { System.out.print("\t"); // 输出空格}for (int i = 1; i <= days; i++) {System.out.print(i + "\t");if ((totalDays + i - 1) % 7 == 5) { // 如果当天为周六,输出换行System.out.println();}}}}2.40.2 运行结果:******************欢迎使用万年历******************请选择年份: 2008请选择月份: 62008 闰年6 共30天输入月份距离1900年1月1日的天数:39598当前月份的天数:30该月第一天是: 0星期日星期一星期二星期三星期四星期五星期六1 2 3 4 5 6 78 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 302.40.3 源程序揭秘2.40.3.1 源程序介绍:该程序是一个和日期相关非常紧密的程序,主要判断某年是否是闰年,某月总共有多少天,该月距离公元纪年的天数,该月第一天是星期几,最后打印该月的日历。
java万年历源代码
![java万年历源代码](https://img.taocdn.com/s3/m/465f8b8688eb172ded630b1c59eef8c75fbf95e6.png)
java万年历源代码第一个类:chaxun.javapackage wannianli;import java.util.*;public class chaxun {public static void main(String[] args) {Scanner input =new Scanner(System.in);String answer="y";for(;answer.equals("y");){week cn=new week();cn.weekDay();//调用方法System.out.print("\是否继续?");answer=input.next();}}}第二个类:tianshu.javapackage wannianli;import java.util.*;public class tianshu {int totalDay;//总共的天数int yueTian;//每月的天数public void jsts(){int days=0;//输入月份到当年的天数System.out.println("*************************************欢迎使用万年历*************************************");Scanner input=new Scanner(System.in);System.out.print("请输入年份:");//从键盘输入年份int year=input.nextInt();System.out.print("请输入月份:");//从键盘输入月份int yue=input.nextInt();/**判断每月的天数*/for(int index=1;index<=yue;index++){if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==1 2){//满足闰年的条件yueTian=31;}elseif(yue==2&&((year%4==0)&&(!(year%100==0))||(year%400== 0))){yueTian=29;}elseif(yue==2&&(!((year%4==0)&&(!(year%100==0))||(year%400= =0)))){yueTian=28;}else if(yue==4||yue==6||yue==9||yue==11){yueTian=30;}else{System.out.print("输入的月份不正确");}if(index<=yue){days=days+yueTian;}}/**判断是否是闰年*/for(int i=100;i<year;i++){< bdsfid="116" p=""></year;i++){<>if ((year%4==0)&&(!(year%100==0))||(year%400==0)){//满足闰年的条件totalDay=totalDay+366;}else{totalDay=totalDay+365;}}totalDay=totalDay+days;//System.out.println(totalDay+"天");//return totalDay+yueTian;}}第三个类:week.jvapackage wannianli;public class week {public void weekDay() {int monDay;//星期几tianshu cn=new tianshu();cn.jsts();int week=1+cn.totalDay%7;//System.out.println(""+week);if (week==7){// 求当月第一天monDay=0;// 周日}else{monDay=week;}/* 输出日历*/System.out.println("星期日\星期一\星期二\星期三\星期四\星期五\星期六");for(int nullNo=0;nullNo<monday;nullno++){< bdsfid="145"p=""></monday;nullno++){<>System.out.print("\");// 输出空格}for(int i=1;i<=cn.yueTian;i++){System.out.print(i+"\");//输出每月的号数if((cn.totalDay + i - 1) % 7 == 5){// 如果当天为周六,输出换行System.out.println();}}} }。
JAVA----日历源代码
![JAVA----日历源代码](https://img.taocdn.com/s3/m/f675b611bfd5b9f3f90f76c66137ee06eff94ed1.png)
JAVA----⽇历源代码1:先创建⼀个CalendarBean类:代码:1. import java.util.Calendar;2. public class CalendarBean3. {4. String day[];5. int year=2005,month=0;6. public void setYear(int year)7. {8. this.year=year;9. }10. public int getYear()11. {12. return year;13. }14. public void setMonth(int month)15. {16. this.month=month;17. }18. public int getMonth()19. {20. return month;21. }22. public String[] getCalendar()23. {24. String a[]=new String[42];25. Calendar date=Calendar.getInstance();26. date.set(year,month-1,1);27. int week=date.get(Calendar.DAY_OF_WEEK)-1;28. int day=0;29. //判断⼤⽉份30. if(month==1||month==3||month==5||month==731. ||month==8||month==10||month==12)32. {33. day=31;34. }35. //判断⼩⽉36. if(month==4||month==6||month==9||month==11)37. {38. day=30;39. }40. //判断平年与闰年41. if(month==2)42. {43. if(((year%4==0)&&(year%100!=0))||(year%400==0))44. {45. day=29;46. }47. else48. {49. day=28;50. }51. }52. for(int i=week,n=1;i<week+day;i++)53. {54. a[i]=String.valueOf(n) ;55. n++;56. }57. return a;58. }59. }2:创建⼀个CalendarFrame类import javax.swing.*;public class CalendarFrame extends JFrame implements ActionListener {JLabel labelDay[]=new JLabel[42];JTextField text=new JTextField(10);JButton titleName[]=new JButton[7];JButton button=new JButton();String name[]={"⽇","⼀","⼆","三","四","五","六"};JButton nextMonth,previousMonth;int year=1996,month=1;CalendarBean calendar;JLabel showmessage=new JLabel("",JLabel.CENTER);JLabel lbl1=new JLabel("请输⼊年份:");JLabel lbl2=new JLabel(" ");public CalendarFrame(){JPanel pCenter=new JPanel();//将pCenter的布局设置为7⾏7列的GridLayout布局pCenter.setLayout(new GridLayout(7,7));//pCenter添加组件titleName[i]for(int i=0;i<7;i++){titleName[i]=new JButton(name[i]);pCenter.add(titleName[i]);}//pCenter添加组件LabelDay[i]for(int i=0;i<42;i++){labelDay[i]=new JLabel("",JLabel.CENTER);pCenter.add(labelDay[i]);}text.addActionListener(this);calendar=new CalendarBean();calendar.setYear(year);calendar.setMonth(month);String day[]=calendar.getCalendar();for(int i=0;i<42;i++){labelDay[i].setText(day[i]);}nextMonth=new JButton("下⽉");previousMonth=new JButton("上⽉");button=new JButton("确定");//注册监听器nextMonth.addActionListener(this);previousMonth.addActionListener(this);button.addActionListener(this);JPanel pNorth=new JPanel(),pSouth=new JPanel();pNorth.add(showmessage);pNorth.add(lbl2);pNorth.add(previousMonth);pNorth.add(nextMonth);pSouth.add(lbl1);pSouth.add(text);pSouth.add(button);showmessage.setText("⽇历"+calendar.getYear()+"年"+calendar.getMonth()+"⽉"); ScrollPane scrollPane=new ScrollPane();scrollPane.add(pCenter);add(scrollPane,BorderLayout.CENTER);//窗⼝添加到ScrollPane 中间位置add(pNorth,BorderLayout.NORTH); //窗⼝添加pNorth在窗⼝北⾯add(pSouth,BorderLayout.SOUTH); //窗⼝添加pSouth在窗⼝的南⾯}public void actionPerformed(ActionEvent e){if(e.getSource()==nextMonth){month+=1;if(month>12)month=1;calendar.setMonth(month);String day[]=calendar.getCalendar();for(int i=0;i<42;i++){labelDay[i].setText(day[i]);}}else if(e.getSource()==button){month+=1;if(month>12)month=1;calendar.setYear(Integer.parseInt(text.getText()));String day[]=calendar.getCalendar();for(int i=0;i<42;i++){labelDay[i].setText(day[i]);}showmessage.setText("⽇历"+calendar.getYear()+"年"+calendar.getMonth()+"⽉"); }}}3:在创建⼀个CalendarMainClass类代码:import javax.swing.JFrame;import javax.swing.UIManager;public class CalendarMainClass {public static void main(String[]args){try{UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); }catch(Exception e){e.printStackTrace();}CalendarFrame frame=new CalendarFrame();frame.setBounds(100,100,360,300);frame.setTitle("中华⽇历");frame.setLocationRelativeTo(null);//窗体居中显⽰frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}运⾏结果:。
JAVA实现的简单万年历代码
![JAVA实现的简单万年历代码](https://img.taocdn.com/s3/m/e915beb7f424ccbff121dd36a32d7375a417c64e.png)
JAVA实现的简单万年历代码本⽂实例讲述了JAVA实现的简单万年历。
分享给⼤家供⼤家参考,具体如下:import java.util.Scanner;public class PrintCalendar {public static void main(String[] args) {int years = 0;int month = 0;int days = 0;boolean isRun = false;//從控制台輸⼊年,⽉Scanner input = new Scanner(System.in);System.out.print("請輸⼊年份:");years = input.nextInt();System.out.print("請輸⼊⽉份:");month = input.nextInt();System.out.println("\n*********"+years+"年"+month+"⽉⽇曆表************");//判断是否是闰年if((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0)){isRun = true;}int totalDays = 0; //累計天數//計算距離1900年1⽉1⽇的天數for(int i = 1900; i < years; i++){if((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)){totalDays = totalDays + 366;}else{totalDays = totalDays + 365;}}int beforeDays = 0;//根據⽉份判斷天數for(int j = 1; j <= month; j++){switch(j){case 1:case 3:case 5:case 7:case 8:case 10:case 12:days = 31;break;case 4:case 6:case 9:case 11:days = 30;break;case 2:if(isRun){days = 29;}else{days = 28;}default:System.out.println("输⼊⽉份不正确!!");}if(j < month){beforeDays = beforeDays + days;}}totalDays = totalDays + beforeDays; //統計到⽬前總天數int firstDayOfMonth = 0;int temp = 1 + totalDays % 7 ;if(temp == 7){firstDayOfMonth = 0; //週⽇}else{firstDayOfMonth = temp;}/* 输出⽇历 */System.out.println("星期⽇\t星期⼀\t星期⼆\t星期三\t星期四\t星期五\t星期六");for(int k = 0; k < firstDayOfMonth; k++){System.out.print("\t");}for(int m = 1; m <= days; m++){System.out.print( m + "\t");if((totalDays + m) % 7 == 6){System.out.print("\n");}}}}关于万年历的制作感兴趣的朋友还可参考本站在线⼯具:希望本⽂所述对⼤家Java程序设计有所帮助。
java万年历程序
![java万年历程序](https://img.taocdn.com/s3/m/54b1ba2bbd64783e09122b10.png)
第一个程序import java.util.*;public class Calendar{public static void main(String[] args){Scanner input = new Scanner(System.in);int year,month; //声名变量年,月,日System.out.println("**********************************************************欢迎使用万年历*****************************************************************\n"); System.out.print("请选择年份:");year = input.nextInt();System.out.print("\n");System.out.print("请选择月份:");month = input.nextInt();System.out.print("\n");if((year%4 == 0 && year%100 != 0) ||year%400 == 0) //判断年份是润年还是平年开始{System.out.print(year + "是闰年\t");}else{System.out.print(year + "是平年\t");} //判断年份是润年还是平年结束switch(month) //判断输入的月份的天数开始{case 1:System.out.print(month + "月共31天");break;case 3:System.out.print(month + "月共31天");break;case 5:System.out.print(month + "月共31天");break;case 7:System.out.print(month + "月共31天");break;case 8:System.out.print(month + "月共31天");break;case 10:System.out.print(month + "月共31天");break;case 12:System.out.print(month + "月共31天");break;case 4:System.out.print(month + "月共30天");break;case 6:System.out.print(month + "月共30天");break;case 9:System.out.print(month + "月共30天");break;case 11:System.out.print(month + "月共30天");break;case 2:if((year%4 == 0 && year%100 != 0) ||year%400 == 0){System.out.print(month + "月共29天");}else{System.out.print(month + "月共28天");}break;} //判断输入的月份的天数结束int tianshuN = 0,tianshuY = 0,tianshu; //tianshuN表示输入年份到1900年经过的天数;tianshuY表示当年一月一号到输入月份的天数(不包括当月)for (int n=1900;n<year;n++) //输入年份到1900年经过的天数和计算开始{if((n%4 == 0 && n%100 != 0) ||n%400 == 0){tianshuN = tianshuN + 366;}else{tianshuN = tianshuN + 365;}} //输入年份到1900年经过的天数和计算结束for (int y=1;y<month;y++) //当年1月1号到输入月份经过的天数(不包含当月)和计算开始{switch(y){case 1:tianshuY = tianshuY +31;break;case 3:tianshuY = tianshuY +31;break;case 5:tianshuY = tianshuY +31;break;case 7:tianshuY = tianshuY +31;break;case 8:tianshuY = tianshuY +31;break;case 10:tianshuY = tianshuY +31;break;case 12:tianshuY = tianshuY +31;break;case 4:tianshuY = tianshuY +30;break;case 6:tianshuY = tianshuY +30;break;case 9:tianshuY = tianshuY +30;break;case 11:tianshuY = tianshuY +30;break;case 2:if((year%4 == 0 && year%100 != 0) ||year%400 == 0){tianshuY = tianshuY + 29;}else{tianshuY = tianshuY + 28;}break;}} //当年1月1号到输入月份经过的天数(不包含当月)和计算结束tianshu =tianshuN + tianshuY; //输入年份和月份到1900年1月1号经过的天数(不包含当月)和计算System.out.println("\n");System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六"); System.out.print("\n");int xingqiji = 1 + tianshu%7; //判断并在当月1号星期几if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) //判断输入的月份是不是大月{for(int j = 0 ;j < xingqiji;j++) //判断并在当月1号前输出空格开始{if(xingqiji==7) //如果当月1号是星期天就不在前面打空格{System.out.print("");}else{System.out.print("\t");}} //判断并在当月1号前输出空格结束for(int t=1;t<=31;t++) //输出当月的日历表开始{if((tianshu+t)%7==6) //判断那天是星期六,输出并换行{System.out.print(t + "\n");continue;}System.out.print(t + "\t");}}if(month==4||month==6||month==9||month==11) //判断输入的月份是不是小月(不包含2月){for(int j = 0 ;j < xingqiji;j++) //判断并在当月1号前输出空格开始{if(xingqiji==7) //如果当月1号是星期天就不在前面打空格{System.out.print("");}else{System.out.print("\t");}} //判断并在当月1号前输出空格结束for(int t=1;t<=30;t++) //输出当月的日历表开始{if((tianshu+t)%7==6) //判断那天是星期六,输出并换行{System.out.print(t + "\n");continue;}System.out.print(t + "\t");}}if(month==2){for(int j = 0 ;j < xingqiji;j++) //判断2月1号星期几,并在前面输出相应的空格{if(xingqiji==7){System.out.print("");}else{System.out.print("\t");}}if((year%4 == 0 && year%100 != 0) ||year%400 == 0) //判断2月份的天数,并输出当月的日历表开始{for(int t=1;t<=29;t++){if((tianshu+t)%7==6){System.out.print(t + "\n");continue;}System.out.print(t + "\t");}}else{for(int t=1;t<=28;t++){if((tianshu+t)%7==6){System.out.print(t + "\n");continue;}System.out.print(t + "\t");}}}}}第二个程序[code=Java][/code]package DK_Date;import java.util.Date;public class DK_Date {public int Year; // 年份public int Month; // 月份public int Day; // 日期public int Days; // 当月有几天public int Week; // 当月第一天为周几public void getCurDate() {Date date = new Date();this.Year = date.getYear() + 1900; this.Month = date.getMonth() + 1; this.Day = date.getDate();}/*** 判断是否为闰年** @param year* @return 如果为真则为闰年,反之为平年*/public boolean isRun(int year) {if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ) return true;return false;}/*** 计算当月天数** @param month* 月份* @param year* 年份*/public void getDays(int month, int year) {switch (month) {case 2:if (isRun(year))this.Days = 29;elsethis.Days = 28;break;case 4:case 6:case 9:case 11:this.Days = 30;break;default:this.Days = 31;break;}}/*** 计算当月第第一天是一年中的第几天** @param month* @param year* @return*/public int getYearDays(int month, int year) { int days = 1;for (int i = 1; i < month; i++) {switch (i) {case 2:if (isRun(year))days += 29;elsedays += 28;break;case 4:case 6:case 9:case 11:days += 30;break;default:days += 31;break;}}return days;}/*** 计算1900年到当年1月1日经过了多少天** @param year* 当前年份* @return*/public int getYearsDay(int year) {int days = 0;for (int i = 1900; i < year; i++) {if (isRun(i))days += 366;elsedays += 365;}return days;}/*** 计算当月第一天为周几* @param year* 年份* @param month* 月份*/public void getWeek(int year, int month) {int days = getYearsDay(year) + getYearDays(month, year);this.Week = days % 7 == 0 ? 7 : days % 7;}}package DK_Date;import java.util.Scanner;public class DateUI {private DK_Date date;Scanner scanner = new Scanner(System.in);public void initial() {date = new DK_Date();date.getCurDate();date.getWeek(date.Year, date.Month);date.getDays(date.Month, date.Year);}public void show() {System.out.println("◆◇◆◇◆◇DK 万年历◆◇◆◇◆◇\n"); System.out.println("北京时间:" + date.Year + "年" + date.Month + "月" + date.Day + "日\n");getShow(date.Week, date.Days);while (true) {System.out.println("\n请输入年份:");date.Year = scanner.nextInt();System.out.println("请输入月份:");date.Month = scanner.nextInt();date.getWeek(date.Year, date.Month);date.getDays(date.Month, date.Year);getShow(date.Week, date.Days);}public void getShow(int week, int days) {System.out.println("周日\t周一\t周二\t周三\t周四\t周五\t周六\t");if (week != 7) {for (int i = 0; i < week; i++) {System.out.print("\t");}}for (int i = 1; i <= days; i++) {System.out.print(i + "\t");if (i % 7 == 7 - week) {System.out.println();}}}}package DK_Date;public class Start {public static void main(String[] args){DateUI date = new DateUI();date.initial();date.show();}}第三个程序import java.util.*;public class Calendar{public static void main(String[] args){Scanner input = new Scanner(System.in);int year,month; //声名变量年,月,日System.out.println("**********************************************************欢迎使用万年历*****************************************************************\n");System.out.print("请选择年份:");year = input.nextInt();System.out.print("\n");System.out.print("请选择月份:");month = input.nextInt();System.out.print("\n");if((year%4 == 0 && year%100 != 0) ||year%400 == 0) //判断年份是润年还是平年开始{System.out.print(year + "是闰年\t");}else{System.out.print(year + "是平年\t");} //判断年份是润年还是平年结束switch(month) //判断输入的月份的天数开始{case 1:System.out.print(month + "月共31天");break;case 3:System.out.print(month + "月共31天");break;case 5:System.out.print(month + "月共31天");break;case 7:System.out.print(month + "月共31天");break;case 8:System.out.print(month + "月共31天");break;case 10:System.out.print(month + "月共31天");break;case 12:System.out.print(month + "月共31天");break;case 4:System.out.print(month + "月共30天");break;case 6:System.out.print(month + "月共30天");break;case 9:System.out.print(month + "月共30天");break;case 11:System.out.print(month + "月共30天");break;case 2:if((year%4 == 0 && year%100 != 0) ||year%400 == 0){System.out.print(month + "月共29天");}else{System.out.print(month + "月共28天");}break;} //判断输入的月份的天数结束int tianshuN = 0,tianshuY = 0,tianshu; //tianshuN表示输入年份到1900年经过的天数;tianshuY表示当年一月一号到输入月份的天数(不包括当月)for (int n=1900;n<year;n++) //输入年份到1900年经过的天数和计算开始{if((n%4 == 0 && n%100 != 0) ||n%400 == 0){tianshuN = tianshuN + 366;}else{tianshuN = tianshuN + 365;}} //输入年份到1900年经过的天数和计算结束for (int y=1;y<month;y++) //当年1月1号到输入月份经过的天数(不包含当月)和计算开始{switch(y){case 1:tianshuY = tianshuY +31;break;case 3:tianshuY = tianshuY +31;break;case 5:tianshuY = tianshuY +31;break;case 7:tianshuY = tianshuY +31;break;case 8:tianshuY = tianshuY +31;break;case 10:tianshuY = tianshuY +31;break;case 12:tianshuY = tianshuY +31;break;case 4:tianshuY = tianshuY +30;break;case 6:tianshuY = tianshuY +30;break;case 9:tianshuY = tianshuY +30;break;case 11:tianshuY = tianshuY +30;break;case 2:if((year%4 == 0 && year%100 != 0) ||year%400 == 0){tianshuY = tianshuY + 29;}else{tianshuY = tianshuY + 28;}break;}} //当年1月1号到输入月份经过的天数(不包含当月)和计算结束tianshu =tianshuN + tianshuY; //输入年份和月份到1900年1月1号经过的天数(不包含当月)和计算System.out.println("\n");System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");System.out.print("\n");int xingqiji = 1 + tianshu%7; //判断并在当月1号星期几if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) //判断输入的月份是不是大月{for(int j = 0 ;j < xingqiji;j++) //判断并在当月1号前输出空格开始{if(xingqiji==7) //如果当月1号是星期天就不在前面打空格{System.out.print("");}else{System.out.print("\t");}} //判断并在当月1号前输出空格结束for(int t=1;t<=31;t++) //输出当月的日历表开始{if((tianshu+t)%7==6) //判断那天是星期六,输出并换行{System.out.print(t + "\n");continue;}System.out.print(t + "\t");}}if(month==4||month==6||month==9||month==11) //判断输入的月份是不是小月(不包含2月){for(int j = 0 ;j < xingqiji;j++) //判断并在当月1号前输出空格开始{if(xingqiji==7) //如果当月1号是星期天就不在前面打空格{System.out.print("");}else{System.out.print("\t");}} //判断并在当月1号前输出空格结束for(int t=1;t<=30;t++) //输出当月的日历表开始{if((tianshu+t)%7==6) //判断那天是星期六,输出并换行{System.out.print(t + "\n");continue;}System.out.print(t + "\t");}}if(month==2){for(int j = 0 ;j < xingqiji;j++) //判断2月1号星期几,并在前面输出相应的空格{if(xingqiji==7){System.out.print("");}else{System.out.print("\t");}}if((year%4 == 0 && year%100 != 0) ||year%400 == 0) //判断2月份的天数,并输出当月的日历表开始{for(int t=1;t<=29;t++){if((tianshu+t)%7==6){System.out.print(t + "\n");continue;}System.out.print(t + "\t");}}else{for(int t=1;t<=28;t++){if((tianshu+t)%7==6){System.out.print(t + "\n");continue;}System.out.print(t + "\t");}}}}}第四个程序import java.awt.*;import java.util.*;import javax.swing.*;import java.awt.event.*;public class WanNianLi extends JFrame implements ActionListener { private static int year,month,days;private JButton[] btn=new JButton[days];WanNianLi() {super("万年历");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout bl=new GridLayout(5,7);JPanel pane=new JPanel();pane.setLayout(bl);for (int i=0;i<days;i++) {int temp=i+1;btn[i]=new JButton(""+temp);btn[i].addActionListener(this);pane.add(btn[i]);}setContentPane(pane);pack();setLookAndFeel();setVisible(true);}public static void main(String[] args) {if (args.length>0)year=Integer.parseInt(args[0]);elseyear=1982;if (args.length>1)month=Integer.parseInt(args[1]);elsemonth=1;GetDays gd=new GetDays(year,month);days=gd.getDays();new WanNianLi();}public void actionPerformed(ActionEvent evt) {Object src=evt.getSource();for (int i=0;i<days;i++)if (src==btn[i]) {int day=i+1;GetWeekday gw=new GetWeekday(year,month,day);String str="";switch (gw.getWeekday()) {case 1:str="天";break;case 2:str="一";break;case 3:str="二";break;case 4:str="三";break;case 5:str="四";break;case 6:str="五";break;case 7:str="六";break;}setTitle(year+"年"+month+"月"+day+"日"+"星期"+str);repaint();}}private void setLookAndFeel() {try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());SwingUtilities.updateComponentTreeUI(this); }catch(Exception e){System.out.print(e.toString());}}}////////////////获取星期几////////////////class GetWeekday {private Calendar cal=Calendar.getInstance(); private static int weekday;public int getWeekday() {return weekday;}GetWeekday(int y,int m,int d) {cal.clear();cal.set(Calendar.YEAR,y);cal.set(Calendar.MONTH,m-1);cal.set(Calendar.DAY_OF_MONTH,d); weekday=cal.get(Calendar.DAY_OF_WEEK); }}//////////////////////获取当前月的天数//////////////////////class GetDays {private static int days;public int getDays() {return days;}GetDays(int y,int m) {GregorianCalendar gc=new GregorianCalendar();switch (m) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:days=31;break;case 4:case 6:case 9:case 11:days=30;break;case 2:if (gc.isLeapYear(y))days=29;elsedays=28;break;}}}。
日历源代码——java
![日历源代码——java](https://img.taocdn.com/s3/m/568f36c56137ee06eff91886.png)
日历源代码——java//import java.sql.Date;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.Locale;public class GregorianCalendar日历 {public static void main(String[] args) { //设置不同地区Locale.setDefault();//创建当前日历对象GregorianCalendar now = new GregorianCalendar();//从当前时期对象是取出时间日期对象//编辑错误:Type mismatch: cannot convert from java.util.Date to java.sql.DateDate date = now.getTime();//将时间日期对象按字符形式打印System.out.println(date.toString());//重新将时间对象设置到日期对象中now.setTime(date);//从当前日期对象是取出当前月份、日期int today =now.get(Calendar.DAY_OF_MONTH);int month = now.get(Calendar.MONTH);//获取本月开始日期now.set(Calendar.DAY_OF_MONTH, 1);//获取本月开始日期在一周中的编号int week = now.get(Calendar.DAY_OF_WEEK);//打印日历头并换行设置当前月中第一天的开始位置System.out.println("星期日星期一星期二星期三星期四星期五星期六");//设置当前月中第一天的开始位置for( int i = Calendar.SUNDAY; i < week; i++){ System.out.print(" ");//按规格打印当前月的日期数字while(now.get(Calendar.MONTH) ==month){//取出当前日期int day =now.get(Calendar.DAY_OF_MONTH);//设置日期数字小于10与不小于10两种情况的打印规格if(day < 10){//设置当前日期的表现形式if(day == today)System.out.print(" <" + day + "> ");elseSystem.out.print(" " + day + " ");}else{//设置当前日期的表现形式if(day == today)System.out.print(" <" + day + "> ");elseSystem.out.print(" " + day + " ");}//设置什么时候换行if(week == Calendar.SATURDAY)System.out.println();//设置日期与星期几为下一天now.add(Calendar.DAY_OF_MONTH, 1);week = now.get(Calendar.DAY_OF_WEEK);}}}}。
JAVA控制台万年历代码
![JAVA控制台万年历代码](https://img.taocdn.com/s3/m/6046a66c58fafab069dc025f.png)
days += 31;
yearDay = 31;
} else if (month == 2) {
if (years % 4 == 0 && years % 100 != 0
+ work[No] + "】\t编号:No." + No);
No++;
continue;
} else {
No++;
continue;
}
}
No = 0; //输入菜单
System.out.print("\n\n当月日期:" + years + "年" + monthsDay + "月");
prox(); //上月
}else if(answer.equals("d") || answer.equals("D")|| answer.equals("2")){
ultimo(); //下月
}else if(answer.equals("w") || answer.equals("W")|| answer.equals("3")) {
|| month == 11) {
days += 30;
yearDay = 30;
}
}
if (monthsDay == 1 || monthsDay == 3 || monthsDay == 5
java万年历源代码(可运行)
![java万年历源代码(可运行)](https://img.taocdn.com/s3/m/99610e3783c4bb4cf7ecd1d1.png)
private JScrollPane sp = new JScrollPane(table);
private JButton bLastYear = new JButton("上一年");
p3.add(jsp, BorderLayout.CENTER);
p3.add(p2, BorderLayout.SOUTH);
p3.add(ld, BorderLayout.NORTH);
private JTextArea jta = new JTextArea(); //jta--JTextArea
private JScrollPane jsp = new JScrollPane(jta);
private JLabel l = new JLabel("年份文本框中可直接键入要查找的年份,以提高查询效率");
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
//import java.sql.Date;
cld.set(Integer.parseInt(strDate[0]), Integer.parseInt(strDate[1])-1, 0);
showCalendar(Integer.parseInt(strDate[0]), Integer.parseInt(strDate[1]), cld);
日历源代码
![日历源代码](https://img.taocdn.com/s3/m/55ab63140740be1e650e9ac5.png)
import java.awt.BorderLayout;import java.awt.Color;import java.awt.Graphics;import java.util.Date;import java.awt.event.*;import javax.swing.*;import javax.swing.border.TitledBorder;import java.util.*;import javax.swing.JPanel;import javax.swing.JTextField;import java.awt.GridLayout;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.SwingUtilities;import javax.swing.UIManager;public class Test extends JFrame{private static final long serialVersionUID = 1L;//测试public Test(){Clock clock =new Clock();Calender cal = new Calender();@SuppressWarnings("unused")JPanel jp2 = new JPanel();setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(560,300);setVisible(true);this.setContentPane(clock);this.getContentPane().add(cal,BorderLayout.WEST); setResizable(false);}//画时钟public class DrawClock extends JPanel implements Runnable {private static final long serialVersionUID = 1L;Thread newThread; //线程public int RADIUS = 80; //时钟的半径//设置时钟位置public int centerX = 150; //设置时钟x轴public int centerY = 120; //设置时钟y轴public int hr, min, sec; //小时,分钟,秒public int[] xPoint = new int[4]; //指针的4个坐标public int[] yPoint = new int[4];public double hrAlpha, minAlpha, secAlpha, theta;private JTextField timeZone;//启动时钟public void start(){newThread = new Thread(this);newThread.start(); //启动线程}//终止线程public void stop(){newThread = null;}@SuppressWarnings("deprecation")public void paint(Graphics g){super.paint(g);//画出时钟刻度double minuteAlpha = Math.PI/30.0;int count = 0;for(double alpha=0; alpha<2.0*Math.PI; alpha+=minuteAlpha){int tX = (int)(centerX+RADIUS*0.9*Math.sin(alpha));int tY = (int)(centerY-RADIUS*0.9*Math.cos(alpha));if(count%5 == 0){g.setColor(Color.CYAN);g.fill3DRect(tX, tY, 3, 3, false);if(count%3==0){int m = count /15;switch(m){case 1: g.drawString("3", centerX+RADIUS-18, centerY+5);break;case 2: g.drawString("6", centerX-3, centerY+RADIUS-10);break;case 3: g.drawString("9", centerX-RADIUS+11,centerY+6);break;default: g.drawString("12", centerX-5, centerY-RADIUS+22);}}}else{g.setColor(Color.DARK_GRAY);g.fill3DRect(tX, tY, 2, 2, false);}count++;}//画出时钟时针g.setColor(Color.gray); // 定义颜色drawPointer(g, centerX+2, centerY+2, (int)(RADIUS*0.75), hrAlpha);g.setColor(Color.CYAN); // 定义颜色drawPointer(g, centerX, centerY, (int)(RADIUS*0.75), hrAlpha);//画出分针g.setColor(Color.gray); // 定义颜色drawPointer(g, centerX+2, centerY+2, (int)(RADIUS*0.83), minAlpha);g.setColor(Color.CYAN); // 定义颜色drawPointer(g, centerX, centerY, (int)(RADIUS*0.83), minAlpha);//画出秒针g.setColor(Color.DARK_GRAY); //定义颜色g.drawLine( centerX,centerY,(int)(centerX+(int)(RADIUS*0.79)*Math.sin(secAlpha)),(int)(centerY-(int)(RADIUS*0.79)*Math.cos(secAlpha)) );setBorder(new TitledBorder("时间"));setBackground(Color.white); // 定义颜色g.drawRect(85, 210, 130, 20);g.setColor(Color.WHITE);g.setColor(Color.DARK_GRAY);Date timeNow = new Date();g.drawString(timeNow.toLocaleString(), 100,225);}public Date getDate(){Date timeNow = new Date();return timeNow;}// 刷新图层public void update(Graphics g){paint(g);}// 画出一个帧的图像public void run() {while(newThread != null){repaint();try{Thread.sleep(800);} catch(InterruptedException E) {}Date timeNow = new Date();@SuppressWarnings("deprecation")int hours = timeNow.getHours(); //这里不知道为什么会画横线的@SuppressWarnings("deprecation")int minutes = timeNow.getMinutes(); //这里不知道为什么会画横线的@SuppressWarnings("deprecation")int seconds = timeNow.getSeconds(); //这里不知道为什么会画横线的hr = hours;min = minutes;sec = seconds;theta = Math.PI/6.0/20.0;hrAlpha = (double)(hr*3600 + min*60 + sec) /(12.0*3600.0)*2.0*Math.PI;minAlpha = (double)(min*60 + sec)/3600.0*2.0*Math.PI;secAlpha = (double)sec/60.0 * 2.0*Math.PI;}}private void drawPointer(Graphics g, int x, int y,int len, double theta){xPoint[0] = (int)(x+len*0.3*Math.sin(theta-Math.PI));yPoint[0] = (int)(y-len*0.3*Math.cos(theta-Math.PI));xPoint[1] = (int)(xPoint[0]+len*0.3*Math.sin(theta-(double)(10.0/180)*Math.PI));yPoint[1] = (int)(yPoint[0]-len*0.3*Math.cos(theta-(double)(10.0/180)*Math.PI));xPoint[2] = (int)(xPoint[0]+len * Math.sin(theta));yPoint[2] = (int)(yPoint[0]-len * Math.cos(theta));xPoint[3] = (int)(xPoint[0]+len*0.3*Math.sin(theta+(double)(10.0/180)*Math.PI));yPoint[3] = (int)(yPoint[0]-len*0.3*Math.cos(theta+(double)(10.0/180)*Math.PI));g.fillPolygon(xPoint, yPoint, 4);}public JTextField getTimeZone() {return timeZone;}public void setTimeZone(JTextField timeZone) {this.timeZone = timeZone;}}//时钟public class Clock extends JPanel{private static final long serialVersionUID = 1L;private UIManager.LookAndFeelInfo looks[];private DrawClock clock ;@SuppressWarnings("unused")private JPanel pane_clock ;JPanel pane_cal;public Clock(){super();looks = UIManager.getInstalledLookAndFeels();changeTheLookAndFeel(2);clock = new DrawClock();clock.start();this.setBackground(Color.GRAY);this.setLayout(new BorderLayout());this.setOpaque(false);this.add(clock);this.setBorder(new TitledBorder("时间日期"));setSize( 300, 300 );setVisible( true );}private void changeTheLookAndFeel(int i){try{UIManager.setLookAndFeel(looks[i].getClassName());SwingUtilities.updateComponentTreeUI(this);}catch(Exception exception){exception.printStackTrace();}}} //设计日历public class Calender extends JPanel implements ActionListener {private static final long serialVersionUID = 1L;public final String HOUR_OF_DAY = null;//定义@SuppressWarnings("rawtypes")JComboBox Month = new JComboBox();@SuppressWarnings("rawtypes")JComboBox Year = new JComboBox();JLabel Year_l = new JLabel("年");JLabel Month_l = new JLabel("月");Date now_date = new Date();JLabel[] Label_day = new JLabel[49];@SuppressWarnings("deprecation")int now_year = now_date.getYear() + 1900;@SuppressWarnings("deprecation")int now_month = now_date.getMonth(); boolean bool = false;String year_int = null;int month_int;JPanel pane_ym = new JPanel();JPanel pane_day = new JPanel();@SuppressWarnings("unchecked")public Calender(){super();//设定年月for (int i = now_year - 10; i <= now_year + 20; i++) {Year.addItem(i + "");}for (int i = 1; i < 13; i++){Month.addItem(i + "");}Year.setSelectedIndex(10);pane_ym.add(new JLabel(""));pane_ym.add(Year);pane_ym.add(Year_l);Month.setSelectedIndex(now_month);pane_ym.add(Month);pane_ym.add(Month_l);pane_ym.add(new JLabel(""));Month.addActionListener(this);Year.addActionListener(this);//初始化日期并绘制pane_day.setLayout(new GridLayout(7, 7, 10, 10)); for (int i = 0; i < 49; i++) {Label_day[i] = new JLabel("");pane_day.add(Label_day[i]);this.setDay();this.setLayout(new BorderLayout());this.add(pane_day, BorderLayout.CENTER);this.add(pane_ym, BorderLayout.NORTH);this.setSize(100,200);this.setBorder(new TitledBorder("吴佳宸的日历"));setSize(300,300);}@SuppressWarnings("deprecation")void setDay(){if (bool){year_int = now_year + "";month_int = now_month;}else{year_int = Year.getSelectedItem().toString();month_int = Month.getSelectedIndex();}int year_sel = Integer.parseInt(year_int) - 1900; //获得年份值//@SuppressWarnings("deprecation")Date dt = new Date(year_sel, month_int, 1); //构造一个日期GregorianCalendar cal = new GregorianCalendar(); //创建一个Calendar实例cal.setTime(dt);String week[] = { "日", "一","二", "三", "四", "五", "六" };int day = 0;int day_week = 0;for (int i = 0; i < 7; i++) {Label_day[i].setText(week[i]);}//月份if (month_int == 0||month_int == 2 ||month_int == 4 ||month_int == 6 ||month_int == 9 ||month_int == 11){day = 31;}else if (month_int == 3 ||month_int == 5 || month_int == 7||month_int == 8 ||month_int == 10|| month_int == 1){day = 30;else{if (cal.isLeapYear(year_sel)){day = 29;}else{day = 28;}}day_week = 7 + dt.getDay();int count = 1;for (int i = day_week; i < day_week + day; count++, i++) {if (i % 7 == 0 ||i == 13||i == 20||i == 27||i == 48 ||i == 34 ||i == 41){if (i == day_week + now_date.getDate() - 1){Label_day[i].setForeground(Color.blue);Label_day[i].setText(count + "");}else{Label_day[i].setForeground(Color.red);Label_day[i].setText(count + "");}}else{if (i == day_week + now_date.getDate() - 1){Label_day[i].setForeground(Color.blue);Label_day[i].setText(count + "");}else{Label_day[i].setForeground(Color.black);Label_day[i].setText(count + "");}}if (day_week == 0){for (int i = day; i < 49; i++){Label_day[i].setText("");}}else{for (int i = 7; i < day_week; i++){Label_day[i].setText("");}for (int i = day_week + day; i < 49; i++){Label_day[i].setText("");}}}public void actionPerformed(ActionEvent e) {if (e.getSource() == Year || e.getSource() == Month) { bool = false;this.setDay();}}}public static void main(String[] args){try{Test frame = new Test();frame.setTitle("吴佳宸的日历");}catch (Exception e){System.out.print("run error!");}}}。
java万年历界面版
![java万年历界面版](https://img.taocdn.com/s3/m/c2fbab13fad6195f312ba618.png)
import java.awt.BorderLayout;import java.awt.Color;import java.awt.GridLayout;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.Date;import java.util.Calendar;import javax.swing.*;public class calendertest {public static void main(String[] args) {JFrame.setDefaultLookAndFeelDecorated(true);new MainFrame();}}class MainFrame extends JFrame {JPanel panel = new JPanel(new BorderLayout());JPanel panel1 = new JPanel();JPanel panel2 = new JPanel(new GridLayout(7, 7));JLabel[] label = new JLabel[49];JLabel y_label = new JLabel("年份");JLabel m_label = new JLabel("月份");JTextField T1=new JTextField(6);JTextField T2=new JTextField(6);JButton JB1=new JButton("转到");int re_year, re_month;int x_size, y_size;String year_num;Calendar now = Calendar.getInstance(); // 实例化CalendarMainFrame() {super("万年历");setSize(300, 350);x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());setLocation((x_size - 300) / 2, (y_size - 350) / 2);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);panel1.add(y_label);panel1.add(T1);panel1.add(m_label);panel1.add(T2);panel1.add(JB1);for (int i = 0; i < 49; i++) {label[i] = new JLabel("", JLabel.CENTER);// 将显示的字符设置为居中panel2.add(label[i]);}panel.add(panel1, BorderLayout.NORTH);panel.add(panel2, BorderLayout.CENTER);panel.setBackground(Color.white);panel1.setBackground(Color.white);panel2.setBackground(Color.white);Init();JB1.addActionListener(new ClockAction());setContentPane(panel);setVisible(true);setResizable(false);}public void Init() {int year, month_num, first_day_num;String log[] = { "日", "一", "二", "三", "四", "五", "六" };for (int i = 0; i < 7; i++) {label[i].setText(log[i]);}for (int i = 0; i < 49; i = i + 7) {label[i].setForeground(Color.red); // 将星期日的日期设置为红色}for (int i = 6; i < 49; i = i + 7) {label[i].setForeground(Color.green);// 将星期六的日期设置为绿色}month_num = (int) (now.get(Calendar.MONTH)); // 得到当前时间的月份year = (int) (now.get(Calendar.YEAR)); // 得到当前时间的年份first_day_num = use(year, month_num);Resetday(first_day_num, year, month_num);}public int use(int reyear, int remonth) {int week_num;now.set(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期return week_num;}class ClockAction implements ActionListener {public void actionPerformed(ActionEvent arg0) {int c_year, c_month, c_week;c_year = Integer.parseInt(T1.getText().toString()); // 得到当前所选年份c_month = Integer.parseInt(T2.getText().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11c_week = use(c_year, c_month); // 调用函数use,得到星期几Resetday(c_week, c_year, c_month); // 调用函数Resetday}}public void Resetday(int week_log, int year_log, int month_log) {int month_day_score; // 存储月份的天数int count;month_day_score = 0;count = 1;Date date = new Date(year_log, month_log + 1, 1); // nowCalendar cal = Calendar.getInstance();cal.setTime(date);cal.add(Calendar.MONTH, -1); // 前个月month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天for (int i = 7; i < 49; i++) { // 初始化标签label[i].setText("");}week_log = week_log + 6; // 将星期数加6,使显示正确month_day_score = month_day_score + week_log;for (int i = week_log; i < month_day_score; i++, count++) {label[i].setText(count + "");}}}/*class MainFrame extends JFrame {private static final long serialVersionUID = 1L;JPanel panel = new JPanel(new BorderLayout());JPanel panel1 = new JPanel();JPanel panel2 = new JPanel(new GridLayout(7, 7));JPanel panel3 = new JPanel();JLabel[] label = new JLabel[49];JLabel y_label = new JLabel("年份");JLabel m_label = new JLabel("月份");JComboBox com1 = new JComboBox();JComboBox com2 = new JComboBox();int re_year, re_month;int x_size, y_size;String year_num;Calendar now = Calendar.getInstance(); // 实例化CalendarMainFrame() {super("万年历");setSize(300, 350);x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());setLocation((x_size - 300) / 2, (y_size - 350) / 2);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);panel1.add(y_label);panel1.add(com1);panel1.add(m_label);panel1.add(com2);for (int i = 0; i < 49; i++) {label[i] = new JLabel("", JLabel.CENTER);// 将显示的字符设置为居中panel2.add(label[i]);}// panel3.add(new Clock(this));panel.add(panel1, BorderLayout.NORTH);panel.add(panel2, BorderLayout.CENTER);//panel.add(panel3, BorderLayout.SOUTH);panel.setBackground(Color.white);panel1.setBackground(Color.white);panel2.setBackground(Color.white);panel3.setBackground(Color.white);Init();com1.addActionListener(new ClockAction());com2.addActionListener(new ClockAction());setContentPane(panel);setVisible(true);setResizable(false);}class ClockAction implements ActionListener {public void actionPerformed(ActionEvent arg0) {int c_year, c_month, c_week;c_year = Integer.parseInt(com1.getSelectedItem().toString()); // 得到当前所选年份c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11c_week = use(c_year, c_month); // 调用函数use,得到星期几Resetday(c_week, c_year, c_month); // 调用函数Resetday}}public void Init() {int year, month_num, first_day_num;String log[] = { "日", "一", "二", "三", "四", "五", "六" };for (int i = 0; i < 7; i++) {label[i].setText(log[i]);}for (int i = 0; i < 49; i = i + 7) {label[i].setForeground(Color.red); // 将星期日的日期设置为红色}for (int i = 6; i < 49; i = i + 7) {label[i].setForeground(Color.green);// 将星期六的日期设置为绿色}for (int i = 1900; i < 2099; i++) {com1.addItem("" + i);}for (int i = 1; i < 13; i++) {com2.addItem("" + i);}month_num = (int) (now.get(Calendar.MONTH)); // 得到当前时间的月份year = (int) (now.get(Calendar.YEAR)); // 得到当前时间的年份com1.setSelectedIndex(year - 1900); // 设置下拉列表显示为当前年com2.setSelectedIndex(month_num); // 设置下拉列表显示为当前月first_day_num = use(year, month_num);Resetday(first_day_num, year, month_num);}public int use(int reyear, int remonth) {int week_num;now.set(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期return week_num;}@SuppressWarnings("deprecation")public void Resetday(int week_log, int year_log, int month_log) {int month_day_score; // 存储月份的天数int count;month_day_score = 0;count = 1;Date date = new Date(year_log, month_log + 1, 1); // nowCalendar cal = Calendar.getInstance();cal.setTime(date);cal.add(Calendar.MONTH, -1); // 前个月month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天for (int i = 7; i < 49; i++) { // 初始化标签label[i].setText("");}week_log = week_log + 6; // 将星期数加6,使显示正确month_day_score = month_day_score + week_log;for (int i = week_log; i < month_day_score; i++, count++) {label[i].setText(count + "");}}}*/。
使用Java创建简单的日历应用程序
![使用Java创建简单的日历应用程序](https://img.taocdn.com/s3/m/cac3250beffdc8d376eeaeaad1f34693dbef1075.png)
使用Java创建简单的日历应用程序-一个实战教程日历应用程序是一个有用的工具,它允许用户记录和管理事件、约会和提醒。
在这个实战博客中,我们将创建一个Java日历应用程序,演示如何使用Java编程语言和图形用户界面(GUI)库来实现事件管理和日期选择功能。
以下是本实战博客的主要内容:项目概述准备工作创建Java项目设计GUI界面实现日历视图添加事件管理功能总结让我们开始吧!1. 项目概述在本项目中,我们将创建一个简单的Java日历应用程序,它包括以下主要功能:显示日历界面,允许用户选择日期。
在日历中标记已经安排的事件和约会。
允许用户添加、编辑和删除事件。
提供事件的日期和时间提醒功能。
我们将使用Java编程语言和Swing GUI库来构建这个日历应用程序。
2. 准备工作在开始之前,确保您的开发环境已设置好。
我们将使用Java编程语言和Swing库来构建日历应用程序,不需要额外的工具或库。
3. 创建Java项目首先,创建一个新的Java项目,您可以使用任何Java集成开发环境(IDE)来完成此操作。
在项目中,我们将创建Java类来实现日历应用程序。
4. 设计GUI界面我们将创建一个简单的Swing GUI界面,用于显示日历界面和事件列表。
创建一个Java类,例如CalendarApp,并在其中创建GUI界面。
javaCopy codeimport javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Calendar;import java.util.Date;public class CalendarApp {private JFrame frame;private JPanel calendarPanel;private JPanel eventPanel;private JTextArea eventList;private JButton addButton;private JButton editButton;private JButton deleteButton;public CalendarApp() {frame = new JFrame("日历应用程序");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(800, 600);frame.setLayout(new BorderLayout());calendarPanel = new JPanel(new BorderLayout());eventPanel = new JPanel(new BorderLayout());eventList = new JTextArea();eventList.setEditable(false);JScrollPane eventScrollPane = new JScrollPane(eventList);addButton = new JButton("添加事件");editButton = new JButton("编辑事件");deleteButton = new JButton("删除事件");eventPanel.add(eventScrollPane, BorderLayout.CENTER);eventPanel.add(addButton, BorderLayout.NORTH);eventPanel.add(editButton, BorderLayout.WEST);eventPanel.add(deleteButton, BorderLayout.EAST);frame.add(calendarPanel, BorderLayout.WEST);frame.add(eventPanel, BorderLayout.CENTER);addButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {addEvent();}});editButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {editEvent();}});deleteButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {deleteEvent();}});frame.setVisible(true);}public static void main(String[] args) {SwingUtilities.invokeLater(() -> new CalendarApp());}private void addEvent() {// 实现添加事件的代码,略...}private void editEvent() {// 实现编辑事件的代码,略...}private void deleteEvent() {// 实现删除事件的代码,略...}}在上述代码中,我们创建了一个CalendarApp类,包括一个Swing窗口、日历界面和事件列表。
用Java实现日历记事本源代码
![用Java实现日历记事本源代码](https://img.taocdn.com/s3/m/06cd623c0722192e4536f6f3.png)
CalendarPad类import java.util.Calendar;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.Hashtable;public class CalendarPad extends JFrame implements MouseListener{int year,month,day;Hashtable hashtable;File file;JTextField showDay[];JLabel title[];Calendar 日历;int 星期几;NotePad notepad=null;Month 负责改变月;Year 负责改变年;String 星期[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; JPanel leftPanel,rightPanel;public CalendarPad(int year,int month,int day){leftPanel=new JPanel();JPanel leftCenter=new JPanel();JPanel leftNorth=new JPanel();leftCenter.setLayout(new GridLayout(7,7));rightPanel=new JPanel();this.year=year;this.month=month;this.day=day;负责改变年=new Year(this);负责改变年.setYear(year);负责改变月=new Month(this);负责改变月.setMonth(month);title=new JLabel[7];showDay=new JTextField[42];for(int j=0;j<7;j++){title[j]=new JLabel();title[j].setText(星期[j]);title[j].setBorder(BorderFactory.createRaisedBevelBorder());leftCenter.add(title[j]);}title[0].setForeground(Color.red);title[6].setForeground(Color.blue);for(int i=0;i<42;i++){showDay[i]=new JTextField();showDay[i].addMouseListener(this);showDay[i].setEditable(false);leftCenter.add(showDay[i]);}日历=Calendar.getInstance();Box box=Box.createHorizontalBox();box.add(负责改变年);box.add(负责改变月);leftNorth.add(box);leftPanel.setLayout(new BorderLayout());leftPanel.add(leftNorth,BorderLayout.NORTH);leftPanel.add(leftCenter,BorderLayout.CENTER);leftPanel.add(new Label("请在年份输入框输入所查年份(负数表示公元前),并回车确定"), ~ 6 / 25 ~BorderLayout.SOUTH) ;leftPanel.validate();Container con=getContentPane();JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftPanel,rightPanel);con.add(split,BorderLayout.CENTER);con.validate();hashtable=new Hashtable();file=new File("日历记事本.txt");if(!file.exists()){try{FileOutputStream out=new FileOutputStream(file);ObjectOutputStream objectOut=new ObjectOutputStream(out);objectOut.writeObject(hashtable);objectOut.close();out.close();}catch(IOException e){}}notepad=new NotePad(this);rightPanel.add(notepad);设置日历牌(year,month);~ 7 / 25 ~addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){System.exit(0);}});setVisible(true);setBounds(100,50,524,285);validate();}public void 设置日历牌(int year,int month){日历.set(year,month-1,1);星期几=日历.get(Calendar.DAY_OF_WEEK)-1;if(month==1||month==2||month==3||month==5||month==7||month==8||month==10||month==12) {排列号码(星期几,31);}else if(month==4||month==6||month==9||month==11){排列号码(星期几,30);}else if(month==2){if((year%4==0&&year%100!=0)||(year%400==0)){排列号码(星期几,29);}~ 8 / 25 ~else{排列号码(星期几,28);}}public void 排列号码(int 星期几,int 月天数){for(int i=星期几,n=1;i<星期几+月天数;i++){showDay[i].setText(""+n);if(n==day){showDay[i].setForeground(Color.green);showDay[i].setFont(new Font("TimesRoman",Font.BOLD,20));}else{showDay[i].setFont(new Font("TimesRoman",Font.BOLD,12));showDay[i].setForeground(Color.black);}if(i%7==6){showDay[i].setForeground(Color.blue);}if(i%7==0){showDay[i].setForeground(Color.red);~ 9 / 25 ~}n++;}for(int i=0;i<星期几;i++){showDay[i].setText("");}for(int i=星期几+月天数;i<42;i++){showDay[i].setText("");}}public int getYear(){return year;}public void setYear(int y){year=y;notepad.setYear(year);public int getMonth(){return month;}public void setMonth(int m){month=m;notepad.setMonth(month);}public int getDay(){return day;}public void setDay(int d){day=d;notepad.setDay(day);}public Hashtable getHashtable(){return hashtable;}public File getFile(){return file;}public void mousePressed(MouseEvent e){JTextField source=(JTextField)e.getSource(); try{day=Integer.parseInt(source.getText());notepad.setDay(day);notepad.设置信息条(year,month,day);notepad.设置文本区(null);notepad.获取日志内容(year,month,day);}catch(Exception ee){}}public void mouseClicked(MouseEvent e){}public void mouseReleased(MouseEvent e){}public void mouseEntered(MouseEvent e){}public void mouseExited(MouseEvent e){}public static void main(String args[]){Calendar calendar=Calendar.getInstance();int y=calendar.get(Calendar.YEAR);int m=calendar.get(Calendar.MONTH)+1;int d=calendar.get(Calendar.DAY_OF_MONTH);new CalendarPad(y,m,d);}}2、)Month类import javax.swing.*;import java.awt.*;import java.awt.event.*;~ 11 / 25 ~public class Month extends Box implements ActionListener{int month;JTextField showMonth=null;JButton 下月,上月;CalendarPad 日历;public Month(CalendarPad 日历){super(BoxLayout.X_AXIS);this.日历=日历;showMonth=new JTextField(2);month=日历.getMonth();showMonth.setEditable(false);showMonth.setForeground(Color.blue);showMonth.setFont(new Font("TimesRomn",Font.BOLD,16));下月=new JButton("下月");上月=new JButton("上月");add(上月);add(showMonth);add(下月);上月.addActionListener(this);下月.addActionListener(this);showMonth.setText(""+month);}public void setMonth(int month){if(month<=12&&month>=1){this.month=month;}else{this.month=1;}showMonth.setText(""+month);~ 12 / 25 ~}public int getMonth(){return month;}public void actionPerformed(ActionEvent e){if(e.getSource()==上月){if(month>=2){month=month-1;日历.setMonth(month);日历.设置日历牌(日历.getYear(),month);}else if(month==1){month=12;日历.setMonth(month);日历.设置日历牌(日历.getYear(),month);}showMonth.setText(""+month);}else if(e.getSource()==下月){if(month<12){month=month+1;日历.setMonth(month);日历.设置日历牌(日历.getYear(),month);}else if(month==12){month=1;日历.setMonth(month);日历.设置日历牌(日历.getYear(),month);}showMonth.setText(""+month);}~ 13 / 25 ~}}3、)NotePad类import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import javax.swing.event.*;import java.io.*;public class NotePad extends JPanel implements ActionListener{JTextArea text;JButton 保存日志,删除日志;Hashtable table;JLabel 信息条;int year,month,day;File file;CalendarPad calendar;public NotePad(CalendarPad calendar){this.calendar=calendar;year=calendar.getYear();month=calendar.getMonth();day=calendar.getDay();;table=calendar.getHashtable();file=calendar.getFile();信息条=new JLabel(""+year+"年"+month+"月"+day+"日",JLabel.CENTER);信息条.setFont(new Font("TimesRoman",Font.BOLD,16));信息条.setForeground(Color.blue);text=new JTextArea(10,10);~ 14 / 25 ~保存日志=new JButton("保存日志") ;删除日志=new JButton("删除日志") ;保存日志.addActionListener(this);删除日志.addActionListener(this);setLayout(new BorderLayout());JPanel pSouth=new JPanel();add(信息条,BorderLayout.NORTH);pSouth.add(保存日志);pSouth.add(删除日志);add(pSouth,BorderLayout.SOUTH);add(new JScrollPane(text),BorderLayout.CENTER); }public void actionPerformed(ActionEvent e){if(e.getSource()==保存日志){保存日志(year,month,day);}else if(e.getSource()==删除日志){删除日志(year,month,day);}}public void setYear(int year){this.year=year;}public int getYear(){return year;}public void setMonth(int month){this.month=month;}public int getMonth(){return month;}public void setDay(int day){this.day=day;}public int getDay(){return day;}public void 设置信息条(int year,int month,int day){信息条.setText(""+year+"年"+month+"月"+day+"日");}public void 设置文本区(String s){text.setText(s);}public void 获取日志内容(int year,int month,int day){String key=""+year+""+month+""+day;try{FileInputStream inOne=new FileInputStream(file);ObjectInputStream inTwo=new ObjectInputStream(inOne);table=(Hashtable)inTwo.readObject();inOne.close();inTwo.close();}catch(Exception ee){}if(table.containsKey(key)){String m=""+year+"年"+month+"月"+day+"这一天有日志记载,想看吗?";int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,~ 16 / 25 ~JOptionPane.QUESTION_MESSAGE);if(ok==JOptionPane.YES_OPTION){text.setText((String)table.get(key));}else{text.setText("");}}else{text.setText("无记录");}}public void 保存日志(int year,int month,int day){String 日志内容=text.getText();String key=""+year+""+month+""+day;String m=""+year+"年"+month+"月"+day+"保存日志吗?";int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);if(ok==JOptionPane.YES_OPTION){try~ 17 / 25 ~{FileInputStream inOne=new FileInputStream(file);ObjectInputStream inTwo=new ObjectInputStream(inOne);table=(Hashtable)inTwo.readObject();inOne.close();inTwo.close();table.put(key,日志内容);FileOutputStream out=new FileOutputStream(file);ObjectOutputStream objectOut=new ObjectOutputStream(out);objectOut.writeObject(table);objectOut.close();out.close();}catch(Exception ee){}}}public void 删除日志(int year,int month,int day){String key=""+year+""+month+""+day;if(table.containsKey(key)){String m="删除"+year+"年"+month+"月"+day+"日的日志吗?";~ 18 / 25 ~int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);if(ok==JOptionPane.YES_OPTION){try{FileInputStream inOne=new FileInputStream(file);ObjectInputStream inTwo=new ObjectInputStream(inOne);table=(Hashtable)inTwo.readObject();inOne.close();inTwo.close();table.remove(key);FileOutputStream out=new FileOutputStream(file);ObjectOutputStream objectOut=new ObjectOutputStream(out);objectOut.writeObject(table);objectOut.close();~ 19 / 25 ~out.close();text.setText(null);}catch(Exception ee){}}}else{String m=""+year+"年"+month+"月"+day+"无日志记录";JOptionPane.showMessageDialog(this,m,"提示",JOptionPane.WARNING_MESSAGE);}}}4、)Year 类import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Year extends Box implements ActionListener{int year;JTextField showYear=null;JButton 明年,去年;CalendarPad 日历;public Year(CalendarPad 日历){super(BoxLayout.X_AXIS);showYear=new JTextField(4);showYear.setForeground(Color.blue);showYear.setFont(new Font("TimesRomn",Font.BOLD,14));this.日历=日历;year=日历.getYear();明年=new JButton("下年");去年=new JButton("上年");add(去年);add(showYear);add(明年);showYear.addActionListener(this);去年.addActionListener(this);明年.addActionListener(this);}public void setYear(int year){this.year=year;showYear.setText(""+year);}public int getYear(){return year;}public void actionPerformed(ActionEvent e){if(e.getSource()==去年){year=year-1;showYear.setText(""+year);日历.setYear(year);日历.设置日历牌(year,日历.getMonth());}else if(e.getSource()==明年){year=year+1;showYear.setText(""+year);日历.setYear(year);日历.设置日历牌(year,日历.getMonth());}else if(e.getSource()==showYear){try{year=Integer.parseInt(showYear.getText()); ~ 21 / 25 ~showYear.setText(""+year);日历.setYear(year);日历.设置日历牌(year,日历.getMonth());}catch(NumberFormatException ee){showYear.setText(""+year);日历.setYear(year);日历.设置日历牌(year,日历.getMonth());}}}}。
用java做一个简单的日历
![用java做一个简单的日历](https://img.taocdn.com/s3/m/69505b0d0a4c2e3f5727a5e9856a561252d321da.png)
用java做一个简单的日历简单日历1:第一步先求每个月的第一天是星期几-----1900年1月1日--2014年9月1日是星期几呢?就是把这之间的天数加起来在对7求余,结果是多少就是星期几2:求当月有多少天3:在对日历进行排位一:效果图如下所示:二:具体实现代码如下:import java.util.Scanner;public class Calendar {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubScanner input=new Scanner(System.in);System.out.println("----万年历-----");System.out.println("请输入年份:");int year=input.nextInt();System.out.println("请输入月份:");int month=input.nextInt();int sumDay=0;int monthDay=0;//输入的月份天数;for(int oldYear=1900;oldYear<year;oldyear++)< bdsfid="90" p=""></year;oldyear++)<>{if(oldYear%4==0&&oldYear%100!=0||oldYear%400= =0) {sumDay+=366;if(month==2){monthDay=29;}else{if(month==4||month==6||month==9||month==11) {monthDay=30;}else{monthDay=31;}}}else{sumDay+=365;if(month==2){monthDay=28;}else{if(month==4||month==6||month==9||month==11) { monthDay=30;else{monthDay=31;}}}}for(int i=1;i<month;i++)< bdsfid="131" p=""></month;i++)<>{if(i==2){if(year%4==0&&year%100!=0||year%400==0) {sumDay+=29;}else{sumDay+=28;}}else{if(i==4||i==6||i==9||i==11){sumDay+=30;}else{sumDay+=31;}}sumDay+=1;int ofWeek=0;//星期几ofWeek=sumDay%7;//System.out.println("一共多少天:"+sumDay);//System.out.println("今天是星期几:"+ofWeek);//System.out.println("这个月一共多少天:"+monthDay); System.out.println("一\二\三\四\五\六\日");for(int i=1;i<=7;i++){if(ofWeek==0){ofWeek=7;}if(ofWeek==i){for(int j=1;j<=monthDay;j++){System.out.print(j+"\");if((ofWeek+j-1)%7==0){System.out.print("\");}}}else{System.out.print("\");}} }。
java课程设计简单日历程序
![java课程设计简单日历程序](https://img.taocdn.com/s3/m/f52e3f49240c844769eaeee0.png)
课程设计题目2. 题目说明通过编写一个基于JAVA的应用系统综合实例,自定义一个日历组件显示日期和时间并进行适当的功能扩充,实践Java语言编程技术。
3. 系统设计设计目标一个完整的程序应具有以下功能:1)显示当月日历、当前日期、当前时间;2)可查寻任意月以及任意年的日历;3)使用图形化界面能够弹出对话框;5)正常退出程序。
设计思想设计一个类用来构成日历系统的主窗口,然后编写一个框架类显示时间和提示信息。
在设计中应用了多种容器和控件。
系统模块划分图1:简易日历的程序结构图2.3.1初始化:public void init()完成界面初始化,形成一个以挂历形式显示当前日期的窗口。
2.3.2 日历描述:?? (1)public void updateView()改变日期后完成更新界面;? (2)获取系统日期并传递日期数据而且在人工改变日期后得出当天是周几;? (3)public static void main(String[] args)主函数完成系统各算法的调用并对主窗口的一些属性进行设置;2.3.3 滚动时间:? ? 将时间以文本的形式在文本框中滚动播出,并能改变滚动的速度。
4. 使用类及接口仅仅简单说明类的功能,详细资料请参看《JavaTM?2?Platform Standard?Ed. 6》的电子文档,常规的接口与包则省略不屑。
;import .*; ;运行结果与分析图2:初始界面显示日历。
图3:点击查看时间按钮,弹出时间消息对话框。
图4:滚动显示当前时间。
6. 程序源代码/*** @(#)* @author fancy*/oString()+"按钮");}});yearsLabel = new JLabel("Year: ");yearsSpinner = new JSpinner();(new (yearsSpinner, "0000"));(new Integer));(new ChangeListener() {public void stateChanged(ChangeEvent changeEvent) {int day = ;, 1);, ((Integer) ()).intValue());int maxDay = ;, day > maxDay maxDay : day);updateView();}});JPanel yearMonthPanel = new JPanel();(yearMonthPanel, ;(new BorderLayout());(new JPanel(), ;JPanel yearPanel = new JPanel();(yearPanel, ;(new BorderLayout());(yearsLabel, ;(yearsSpinner, ;monthsLabel = new JLabel("Month: ");monthsComboBox = new JComboBox();for (int i = 1; i <= 12; i++) {(new Integer(i));});(new ActionListener() {public void actionPerformed(ActionEvent actionEvent) {int day = ;, 1);, ());int maxDay = ;, day > maxDay maxDay : day);updateView();}});JPanel monthPanel = new JPanel();(monthPanel, ;(new BorderLayout());(monthsLabel, ;(monthsComboBox, ;daysModel = new AbstractTableModel() {public int getRowCount() {return 9;}public int getColumnCount() {return 7;}public Object getValueAt(int row, int column) { if (row == 0) {return getHeader(column);}row--;, 1);int dayCount = ;int moreDayCount = - 1;int index = row * 7 + column;int dayIndex = index - moreDayCount + 1;if (index < moreDayCount || dayIndex > dayCount) {return null;} else {return new Integer(dayIndex);}}};daysTable = new CalendarTable(daysModel, calendar);(true);;(0), new TableCellRenderer() {public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,boolean hasFocus, int row, int column) {String text = (value == null) "" : ();JLabel cell = new JLabel(text);(true);if (row == 0) {(headerForeground);(headerBackground);} else {if (isSelected) {(selectedForeground);(selectedBackground);} else {(foreground);(background);}}return cell;}});updateView();(daysTable, ;}public static String getHeader(int index) {switch (index) {case 0:return WEEK_SUN;case 1:return WEEK_MON;case 2:return WEEK_TUE;case 3:return WEEK_WED;case 4:return WEEK_THU;case 5:return WEEK_FRI;case 6:return WEEK_SAT;default:return null;}}public void updateView() {();,);- 1,- 1);}public static class CalendarTable extends JTable { private Calendar calendar;public CalendarTable(TableModel model, Calendar calendar) {super(model);= calendar;}public void changeSelection(int row, int column, boolean toggle, boolean extend) {(row, column, toggle, extend);if (row == 0) {return;}Object obj = getValueAt(row, column);if (obj != null) {, ((Integer)obj).intValue());}}}public static void main(String[] args) {JFrame frame = new JFrame("简易时间日历");;MyCalendar myCalendar = new MyCalendar();();().add(myCalendar);(330,80);(360, 212);(true);}//滚动字public static class RollbyJFrame extends JFrame{private JTextField text;private JSpinner spinner;private Timer timer;private JButton button;public RollbyJFrame(){super("滚动时间");(360,100);(700,120);Container c=getContentPane();JButton button=new JButton("修改速度");(button,"East");(this);Calendar now = ();int hour=;int minute=;int year=;int month=;int day=;text = new JTextField(" Hello 当前时间是:"+hour+":"+minute+" "+year+"/"+month+"/"+day);(text,"Center");(this); //注册焦点事件监听器timer = new Timer(136,this);();JPanel panel = new JPanel(new FlowLayout);(panel,"South");spinner = new JSpinner();());(spinner);(this);(true);}public void focusGained(FocusEvent e) //获得焦点时{if ()==text){();}}public void focusLost(FocusEvent e) //失去焦点时{if ()==text){();}}public void stateChanged(ChangeEvent e){if ()==spinner){(new Integer(""+())); //设置延时的时间间隔}}public void actionPerformed(ActionEvent e) //定时器定时执行事件{if ()==button);else{String temp = ();temp = (1) + (0,1);(temp);}}public void buttondown(ActionEvent e) //单击事件 {if ()==button){} ;}}}。
简单的java万年历代码
![简单的java万年历代码](https://img.taocdn.com/s3/m/08fe6fcc5022aaea998f0f5b.png)
private Calendar a; //日历功能
private GridLayout MyGridLayout = new GridLayout(2, 1, 0, 5); //设计容器
private JPanel row1 = new JPanel();
private JPanel row2 = new JPanel();
private JTextField JTMonth=new JTextField(10); //月份显示文本框
private JTextField JTYear=new JTextField(10); //年份显示文本框
private JButton JBAddYear=new JButton(">>"); //增加年份按钮
private JLabel[] numbers = new JLabel[42];
//private JLabel MyJLabel1=new JLabel("设计:王 时间:2014年12月");
private JLabel MyJLabel2=new JLabel("日 一 二 三 四 五 六");
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
s = Integer.toString(MyYear); // 将年份转换为字符串类型
java日历源代码
![java日历源代码](https://img.taocdn.com/s3/m/d7f7772acfc789eb172dc8fd.png)
Main.setLayout(new BorderLayout()); Main.setBackground(); Main.setBorder(null);
Out.setBackground(Color.lightGray); Out.setHorizontalAlignment(SwingConstants.CENTER); Out.setMaximumSize(new Dimension(100, 19)); Out.setMinimumSize(new Dimension(100, 19)); Out.setPreferredSize(new Dimension(100, 19));
private Locale l=Locale.CHINESE; //主日历
private GregorianCalendar cal=new GregorianCalendar(l);
第1页
Generated by Foxit PDF Creator © Foxit Software For evaluation only.
TestJCalendar.java //星期面板
private JPanel weekPanel=new JPanel(); //天按钮组
private JToggleButton[] days=new JToggleButton[42]; //天面板
private JPanel Days = new JPanel(); //标示
cal.setTime(date); try {
jbInit(); } catch (Exception e) {
e.printStackTrace(); } } //初始化组件 private void jbInit() throws Exception {
java_(Java)万年历源代码参考
![java_(Java)万年历源代码参考](https://img.taocdn.com/s3/m/f6471712cc7931b765ce1544.png)
this.getContentPane().setLayout(new BorderLayout(10, 0));
jta.setLineWrap(true); table.setGridColor(Color.GRAY); //星期之间的网格线是灰 色的
private JPanel p2 = new JPanel(); private JPanel p3 = new JPanel(new BorderLayout()); private JPanel p4 = new JPanel(new GridLayout(2,1)); private JPanel p5 = new JPanel(new BorderLayout()); private JButton bAdd = new JButton("保存日志"); private JButton bDel = new JButton("删除日志"); private JTextArea jta = new JTextArea(); //jta--JTextArea private JScrollPane jsp = new JScrollPane(jta); private JLabel l = new JLabel("您可以向年份文本框中键入 您要查找的年份,以提高查询效率"); private JLabel lt = new JLabel(); private JLabel ld = new JLabel(); private int lastTime; public MyCalendar() { super("MyCalendar"); //框架命名
日历源码
![日历源码](https://img.taocdn.com/s3/m/630becbd960590c69ec37623.png)
import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;class DateWin extends JFrame implements ItemListener{Calendar cd = null;int years;int months;int dates;JTable table;Object date[][];Object name[]={"日","一","二","三","四","五","六"}; JComboBox year,month;JLabel label1,label2;JPanel panel;DateWin(String s){super(s);setSize(400,300);setLocation(120,120);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);year=new JComboBox();//year.addItem("1990");for (int i=0;i<30;i++){int years=1990+i;year.addItem(years);}month=new JComboBox();for (int i=1;i<13;i++){month.addItem(i);}label1=new JLabel("请选择年份:");label2=new JLabel("请选择月份:");panel=new JPanel();panel.add(label1);panel.add(year);panel.add(label2);panel.add(month);Container con=getContentPane();con.setLayout(new BorderLayout());con.add(panel,BorderLayout.NORTH);cd=new GregorianCalendar();//years=Integer.parseInt(year.getSelectedItem().toString());//months=Integer.parseInt(month.getSelectedItem().toString())-1; //cd=new GregorianCalendar(years,months,1);years=cd.get(Calendar.YEAR);year.setSelectedItem(years);months=cd.get(Calendar.MONTH)+1;month.setSelectedItem(months);cd.set(Calendar.DATE, 1);int dateNumber=cd.getActualMaximum(Calendar.DATE);int firstDay=cd.get(Calendar.DAY_OF_WEEK)-1;int count=1;date=new Object[6][7];for(int i=0;i<6;i++){for(int j=0;j<7;j++){if(count>dateNumber)break;else {if ((i*7+j)<firstDay)continue;else{date[i][j]=count;count++;}}}}table=new JTable(date,name);table.setRowHeight(15);con.add(new JScrollPane(table),BorderLayout.CENTER);con.validate();validate();year.addItemListener(this);month.addItemListener(this);}public void itemStateChanged(ItemEvent e) {// TODO Auto-generated method stubyears=Integer.parseInt(year.getSelectedItem().toString());months=Integer.parseInt(month.getSelectedItem().toString())-1; cd=new GregorianCalendar(years,months,1);int dateNumber=cd.getActualMaximum(Calendar.DATE);int firstDay=cd.get(Calendar.DAY_OF_WEEK)-1;int count=1;date=new Object[6][7];for(int i=0;i<6;i++){for(int j=0;j<7;j++){if(count>dateNumber)break;else {if ((i*7+j)<firstDay)continue;else{date[i][j]=count;count++;}}}}table=new JTable(date,name);table.setRowHeight(15);getContentPane().removeAll();getContentPane().setLayout(new BorderLayout());getContentPane().add(panel,BorderLayout.NORTH);getContentPane().add(new JScrollPane(table),BorderLayout.CENTER); validate();}}public class HomeWork3_1 {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubDateWin dateWin=new DateWin("Java日历");}}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
for(int i=0;i<42;i++)
{
labelDay[i].setText(day[i]);
}
}
public void setMonth(int month)
{
this.month=month;
}
public int getMonth()
{
JButton titleName[]=new JButton[7];
JButton button = new JButton();
String name[]={"日","一","二","三", "四","五","六"};
JButton nextMonth,previousMonth;
import java.util.Calendar;
public class CalendarBean
{
String day[];
int year=2005,month=0;
public void setYear(int year)
if(month==1||month==3||month==5||month==7
||month==8||month==10||month==12)
{
day=31;
}
titleName[i]=new JButton(name[i]);
pCenter.add(titleName[i]);
}
//pCenter添加组件labelDay[i]
JLabel lbl2=new JLabel(" ");
public CalendarFrame()
{
JPanel pCenter=new JPanel();
//将pCenter的布局设置为7行7列的GridLayout 布局。
//判断平年与闰年
if(month==2)
{
if(((year%4==0)&&(year%100!=0))||(year%400==0))
{
{
this.year=year;
}
public int getYear()
{
return year;
}
}
for(int i=week,n=1;i<week+day;i++)
{
a[i]=String.valueOf(n) ;
n++;
calendar.setMonth(month);
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++)
{
labelDay[i].setText(day[i]);
//注册监听器
nextMonth.addActionListener(this);
previousMonth.addActionListener(this);
button.addActionListener(this);
month=month+1;
if(month>12)
month=1;
calendar.setMonth(month);
String day[]=calendar.getCalendar();
date.set(year,month-1,1);
int week=date.get(Calendar.DAY_OF_WEEK)-1;
int day=0;
//判断大月份
pCenter.setLayout(new GridLayout(7,7));
//pCenter添加组件titleName[i]
for(int i=0;i<7;i++)
{
add(scrollPane,BorderLayout.CENTER);// 窗口添加scrollPane在中心区域
add(pNorth,BorderLayout.NORTH);// 窗口添加pNortBorderLayout.SOUTH);// 窗口添加pSouth 在南区域。
JPanel pNorth=new JPanel(),
pSouth=new JPanel();
pNorth.add(showMessage);
pNorth.add(lbl2);
pNorth.add(previousMonth);
pNorth.add(nextMonth);
pSouth.add(lbl1);
pSouth.add(text);
pSouth.add(button);
int year=1996,month=1; //启动程序显示的日期信息
CalendarBean calendar;
JLabel showMessage=new JLabel("",JLabel.CENTER);
JLabel lbl1 = new JLabel("请输入年份:");
day=29;
}
else
{
day=28;
}
}
text.addActionListener(this);
calendar=new CalendarBean();
calendar.setYear(year);
public class CalendarFrame extends JFrame implements ActionListener
{
JLabel labelDay[]=new JLabel[42];
JTextField text=new JTextField(10);
else if(e.getSource()==previousMonth)
{
month=month-1;
for(int i=0;i<42;i++)
{
labelDay[i]=new JLabel("",JLabel.CENTER);
pCenter.add(labelDay[i]);
showMessage.setText("日历:"+calendar.getYear()+"年"+ calendar.getMonth()+"月" );
ScrollPane scrollPane=new ScrollPane();
scrollPane.add(pCenter);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==nextMonth)
{
}
return a;
}
}
CalendarFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//判断小月
if(month==4||month==6||month==9||month==11)
{
day=30;
}
}
nextMonth=new JButton("下月");
previousMonth=new JButton("上月");
button=new JButton("确定");
return month;
}
public String[] getCalendar()
{
String a[]=new String[42];
Calendar date=Calendar.getInstance();