JAVA《万年历系统》课程设计报告附源码
- 格式:doc
- 大小:539.50 KB
- 文档页数:48
JA V A万年历设计报告专业:计算机科学与技术姓名:班级:JA V A万年历设计报告一课程实习目的:(1)为用户开发一个便捷的万年历;(2)对java技术的灵活应用;(3)掌握日期处理技术;(4)初步了解软件工程理论知识。
二课程实习内容与要求:(1)内容通过本课程设计教学所要求达到的目的是:实现一个简洁的万年历,通过日期的处理,能按照用户的需求查看各年份的日期。
(2)课程实习的基本要求(1)使用图形用户界面;(2)实现日期与星期的查询。
三系统功能分析:1 系统的主要功能:(1)可以查看当前的年份、月份和日期;(2)增加了时间栏,便于用户查看当前时间;(3)能够查找1至9999年的日期与星期的关系。
2 系统功能结构图(1)Clock类:实现了Runnable接口,运用线程,实现时钟的功能,包含了paint方法。
Paint方法:从系统获取当前日期,以设置时钟的初始值。
(2)MainFrame类:作为主类实现了JFrame接口,调用Clock类,包含了MainFrame方法,Init方法,use方法,Resetday方法。
MainFrame方法:定义各个参数,应用AWT的绘图功能,构建总体框架;获取所要查找的年份和月份,通过调用use方法和Resetday方法,实现“查看”按钮的功能,并得到所需结果。
Init方法:调用系统的年份和月份,以设置查看面板和日期面板的初始值。
use方法:确定所要查询的年月的第一天的是星期几。
Resetday方法:进一步处理所查询的年份月份,确定查询月份的天数;图1 系统功能结构四系统运行1.运行界面如下:用户可根据需要在下拉式菜单中选择所要查询的年份和月份,然后点击“查看”按钮:图2 运行界面2.流程图:图3五课程设计体会本次课程设计为课本后设计题目任选一题进行设计。
思考一段时间后,我决定做一个万年历的设计。
因为通过设计这样的一个日历小组件,能使自己对JA V A 的一些基础知识掌握的更加牢固。
河北科技大学课程设计报告学生姓名:学号:专业班级:课程名称:Java程序设计课程设计学年学期:2 014 —2 015 学年第2 学期指导教师:刘伟2 0 年月课程设计成绩评定表学生姓名学号成绩专业班级起止时间设计题目指导教师评语学习态度:端正□较端正□一般□较差□学习纪律:好□较好□一般□较差□出勤情况:好□较好□一般□较差□程序设计:好□较好□一般□较差□动手能力:强□较强□一般□较差□团队精神:好□较好□一般□较差□创新意识:强□较强□一般□较差□课程设计完成情况:优秀□好□一般□较差□课程设计报告完成情况:优秀□好□一般□较差□指导教师:年月日目录1.课程设计目的 (1)2.需求分析 (1)3.总体设计 (1)4.详细设计 (1)5.软件测试 (10)6.课程设计总结 (11)一、课程设计目的(1)熟练使用java语言编写程序,解决实际问题。
(2)初步掌握软件开发过程的问题分析、系统设计、程序编码、测试等基本方法和技能;(3)学会自己调试程序的方法并掌握一定的技巧。
二、需求分析本程序的要求为:1.使用图形用户界面;2.本程序能够实现日期与星期的查询。
三、总体设计(1)可以通过图形界面显示日历。
(2)能以月历形式显示日期与星期。
(3)支持用户自己输入年份,可以通过上一年,下一年等按钮来选择年份和月份。
四、详细设计1.总天数的算法:首先用if语句判断定义年到输入年之间每一年是否为闰年,是闰年,该年的总天数为366,否则,为365。
2.输出月份第一天为星期几的算法:使总天数除以7取余得几既为星期几,若是0,则为星期日。
3.算出输出月份第一天为星期几的算法:算出输出月份第一天为星期几后,把该日期以前的位置用空格补上,并总该日起一次输出天数直到月底,该月中的天数加上该月一日为星期几的数字再除以7得0换行,即可完整的输出该月的日历。
4.查询年份必须为四位有效数字,否则不予显示。
程序源代码:import java.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.sql.Date;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.awt.*;import java.awt.event.*;import java.text.*;import java.time.Clock;import java.util.*;import javax.swing.*;import javax.swing.table.*;import javax.swing.*;import javax.swing.table.DefaultTableModel;public class wannianli extends JFrame implements ActionListener{private Calendar cal= Calendar.getInstance();private String [] str = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};private DefaultTableModel dtm = new DefaultTableModel(null,str);private JTable table = new JTable(dtm); //装日期的表格private JScrollPane sp = new JScrollPane(table);private JButton bLastYear = new JButton("上一年");private JButton bNextYear = new JButton("下一年");private JButton bLastMonth = new JButton("上月");private JButton bNextMonth = new JButton("下月");private JTextField Year = new JTextField(4);//jtfYear年份显示和输入文本private JLabel jla=new JLabel("年");private JTextField month = new JTextField(4);private JLabel jla1=new JLabel("月");private JTextField jtfMonth = new JTextField(2);//jtfMonth月份显示文本框private JComboBox comb;private JPanel p1 = new JPanel();//装入控制日期按钮的模块private JPanel p2 = new JPanel();private JPanel p3 = new JPanel(new GridLayout(2,1));private JPanel p4 = new JPanel();private JPanel p5 = new JPanel();private JLabel l = new JLabel("文本框中可直接键入要查找的年份,以提高查询效率");private JLabel lt = new JLabel();private JLabel com = new JLabel("请选择月份:");private int lastTime;privatewannianli(){super("万年历"); //框架命名this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭函数table.setGridColor(Color.GRAY);//星期之间的网格线是灰色的table.setColumnSelectionAllowed(true);table.setSelectionBackground(Color.BLACK);//当选定某一天时这一天背景黑色table.setSelectionForeground(Color.GREEN);//选定的日期字体是绿色的table.setBackground(new Color(184,207,229));//日期显示表格颜色浅蓝色table.setFont(new Font("黑体",Font.BOLD,24));//日期数字字体格式table.setRowHeight(30);//表格的高度Year.addActionListener(this);//可输入年份的文本框//month.addActionListener(this);//可输入年份的文本框 //为各个按钮添加监听函数bLastYear.addActionListener(this);bNextYear.addActionListener(this);bLastMonth.addActionListener(this);bNextMonth.addActionListener(this); //将按钮添加到String[] str={"1","2","3","4","5","6","7","8","9","10","11","12"};comb = new JComboBox(str);p1.add(bLastYear);p1.add(Year);//年份输入文本框p1.add(jla);p1.add(month);//年份输入文本框p1.add(jla1);p1.add(bNextYear);p1.add(bLastMonth);p1.add(jtfMonth);p1.add(bNextMonth);p1.add(com);p1.add(comb);p2.setLayout(new FlowLayout());p2.add(p4,BorderLayout.EAST);p2.add(sp,BorderLayout.CENTER);p2.add(p5,BorderLayout.WEST);p3.add(l);p3.add(lt);this.add(p3, BorderLayout.SOUTH);this.add(p2, BorderLayout.CENTER);this.add(p1, BorderLayout.NORTH);String [] strDate = DateFormat.getDateInstance().format(newjava.util.Date()).split("-");cal.set(Integer.parseInt(strDate[0]), Integer.parseInt(strDate[1])-1,0);showCalendar(Integer.parseInt(strDate[0]),Integer.parseInt(strDate[1]), cal);jtfMonth.setEditable(false);//设置月份的文本框为不可编辑Year.setText(strDate[0]);month.setText(strDate[1]);comb.setToolTipText(strDate[1]);comb.addItemListener(new ItemListener() {public void itemStateChanged(ItemEvent e) {jtfMonth.setText((String) comb.getSelectedItem());month.setText((String) comb.getSelectedItem()) ;int m, y;try//控制输入的年份正确,异常控制{if (Year.getText().length() != 4){ throw new NumberFormatException();}y = Integer.parseInt(Year.getText());m = Integer.parseInt(jtfMonth.getText());}catch (NumberFormatException ex){// JOptionPane.showMessageDialog(this, "请输入4位0-9的数字!", "年份有误", JOptionPane.ERROR_MESSAGE);return;}for (int i = 0; i < lastTime+1; i++){dtm.removeRow(0);}cal.set(y, m-1, 0);showCalendar(y, m, cal);}});jtfMonth.setText("1");month.setText("1");this.setBounds(200,200,700,320);this.setResizable(false);this.setVisible(true);}public static void main(String[] args){new wannianli();}public void showCalendar(int localYear, int localMonth, Calendar cld) {int Days = getDaysOfMonth(localYear, localMonth) +cld.get(Calendar.DAY_OF_WEEK) - 2;Object [] ai = new Object[7];lastTime = 0;for (int i = cld.get(Calendar.DAY_OF_WEEK)-1; i <= Days; i++){ai[i%7] = String.valueOf(i-(cld.get(Calendar.DAY_OF_WEEK)-2));if (i%7 == 6){dtm.addRow(ai);ai = new Object[7];lastTime++;}}dtm.addRow(ai);}public int getDaysOfMonth(int year, int Month) //显示所选月份的天数{if(Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8|| Month == 10 || Month == 12){return 31;}if(Month == 4 || Month == 6 || Month == 9 || Month == 11){return 30;}if(year%4 == 0 && year%100 != 0 || year%400 == 0)//闰年{return 29;}else{return 28;}}public void actionPerformed(ActionEvent e){if(e.getSource() == Year || e.getSource() == bLastYear ||e.getSource() == bNextYear || e.getSource() == bLastMonth || e.getSource() == bNextMonth){int m, y;try//控制输入的年份正确,异常控制{if (Year.getText().length() != 4){ throw new NumberFormatException();}y = Integer.parseInt(Year.getText());m = Integer.parseInt(jtfMonth.getText());} catch (NumberFormatException ex){JOptionPane.showMessageDialog(this, "请输入4位0-9的数字!", "年份有误", JOptionPane.ERROR_MESSAGE);return;}for (int i = 0; i < lastTime+1; i++){dtm.removeRow(0);}if(e.getSource() == bLastYear){Year.setText(String.valueOf(--y));}if(e.getSource() == bNextYear){Year.setText(String.valueOf(++y));}if(e.getSource() == bLastMonth){if(m == 1){Year.setText(String.valueOf(--y));m = 12;jtfMonth.setText(String.valueOf(m)); }else{jtfMonth.setText(String.valueOf(--m));}}if(e.getSource() == bNextMonth){if(m == 12){Year.setText(String.valueOf(++y));m = 1;jtfMonth.setText(String.valueOf(m));}else{jtfMonth.setText(String.valueOf(++m)); }}cal.set(y, m-1, 0);showCalendar(y, m, cal);month.setText(String.valueOf(m));// comb.setToolTipText(String.valueOf(m));}}五、软件测试图1 界面图图2 界面图下拉选择2月份六、课程设计总结本次Java课程设计我们这组做的课题是万年历,刚开始拿到题目我们组员都不知如何下手,经过小组成员一起查找资料,并且开会讨论,我们确定了设计的设计目标以及具体实现方式,包括如何将Java的思想运用到实际系统的详细设计之中。
程序设计报告JA V A设计报告2:万年历姓名:王颖学号:001学习中心:延安富县学习中心专业:计算机科学与技术层次:专升本身份证号:联系电话:万年历程序设计一、设计分析万年历有很多作用,顾名思义它是一种很方便得计时计年工具,随着科技得发展,我们已经可以通过计算机制作出精美的万年历,通过计算机制作得万年历不但外观美观,而且功能强大。
通过万年历,我们可以实现很多功能,随意看某年得年份,某年得月份,随意调整某年得某天,并将其显示出来,而且有判断闰年闰月得功能,十分方便。
二、程序流程框图主程序流程图三、具体设计1、变量设计2、程序源代码import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.border.*;import java.util.Calendar;import java.util.*;import java.io.*;import java.io.IOException;public class calendar extends JFrame implements ActionListener,MouseListener {int year,month,day;int yearafterquery,monthafterquery;int startday;String SwitchMonth;String key;int changeyearmessage;int changemonthmessage;int priormonth;int prioryear;boolean ischange=false;boolean ischange_priornext=false; private JPanel LeftPane,RightPane;//Left subprivate JLabel YearLabel;private JComboBox MonthCombobox;private JTextField ShowDays[]= new JTextField[42];private JTextField YearText;private JLabel Ask;private JLabel ShowDate;private JLabel Blank;private JLabel TopBarTitle[]=new JLabel[7];private JButton ToToday;private JButton Query;privateString week[]={"SUN","MON","TUE","WED","THU","FRI","SAT"};//right subprivate JLabel NorthMonthDayYear;private JTextArea CenterText;private JButton SouthSave,SouthDelete;private JButton PriorMonth;private JButton NextMonth;public calendar(int year,int month,int day){setTitle("My Calendar&Textbook");//the layout about left of jpaneLeftPane = new JPanel();JPanel LeftCenter = new JPanel();JPanel LeftNorth = new JPanel();//JPanel LeftSouth = new JPanel();LeftPane.setLayout(new BorderLayout());LeftPane.add(LeftNorth,BorderLayout.NORTH);LeftPane.add(LeftCenter,BorderLayout.CENTER);LeftPane.add(ToToday = new JButton("Go to today",new ImageIcon("../images/Handle.gif")),BorderLayout.SOUTH);ToToday.setBackground(Color.cyan);ToToday.addActionListener(this);LeftPane.validate();//the layout of LeftPane//LeftPane_NorthLeftNorth.setLayout(new GridLayout(3,1,0,-2));LeftNorth.add(Ask = new JLabel(" Plese input the informations which you want query:"));JPanel North = new JPanel(new FlowLayout(0,8,0));LeftNorth.add(North);North.add(YearLabel=new JLabel("Year:"));North.add(YearText = new JTextField(4));YearText.setBackground(Color.getHSBColor(30,20,50));YearText.setForeground(Color.blue);YearText.setFont(new Font("TimesRoman",Font.BOLD,17));YearText.setFocusable(true);North.add(Blank=new JLabel(" "));North.add(MonthLabel = new JLabel("Month:"));North.add(MonthCombobox = new JComboBox());//add month to monthcomboboxfor(int i=1;i<=12;i++){MonthCombobox.addItem(new Integer(i));}//Switch the monthMonthCombobox.setForeground(Color.blue);MonthCombobox.setFont(new Font("TimesRoman",Font.BOLD,12));North.add(Blank=new JLabel(" "));North.add(Query=new JButton("Query"));Query.setForeground(Color.blue);Query.addActionListener(this);JPanel North2=new JPanel(new FlowLayout());LeftNorth.add(North2);North2.add(PriorMonth=new JButton(new ImageIcon("../images/prior.gif")));PriorMonth.addActionListener(this);PriorMonth.setActionCommand("prior");priormonth=month;prioryear=year;SwitchMonth(month);North2.add(ShowDate = new JLabel(SwitchMonth+" "+","+" "+String.valueOf(year),SwingConstants.CENTER));ShowDate.setForeground(Color.blue);ShowDate.setFont(new Font("TimesRoman",Font.BOLD,14));North2.add(NextMonth=new JButton(new ImageIcon("../images/next.gif")));NextMonth.addActionListener(this);NextMonth.setActionCommand("next");//LeftPane_CenterLeftCenter.setLayout(new GridLayout(7,7));//print titlefor(int i=0;i<7;i++){TopBarTitle[i]=new JLabel();TopBarTitle[i].setText(week[i]);TopBarTitle[i].setForeground(Color.darkGray);TopBarTitle[i].setHorizontalAlignment(0);TopBarTitle[i].setBackground(Color.MAGENTA );TopBarTitle[i].setBorder(BorderFactory.createRaisedBevelBorder());LeftCenter.add(TopBarTitle[i]);}for(int i=0;i<42;i++){ShowDays[i]=new JTextField();ShowDays[i].addMouseListener(this);ShowDays[i].setEditable(false);LeftCenter.add(ShowDays[i]);}//print the body of monthPrintMonth(year,month,day);//the layout about right of jpaneRightPane = new JPanel(new BorderLayout());JPanel RightCenter = new JPanel();JPanel RightNorth = new JPanel();JPanel RightSouth = new JPanel(new FlowLayout());RightPane.add(RightNorth,BorderLayout.NORTH);RightPane.add(RightCenter,BorderLayout.CENTER);RightPane.add(RightSouth,BorderLayout.SOUTH);RightNorth.add(NorthMonthDayYear=new JLabel(">>"+year+","+SwitchMonth+","+day+"<<")); key=year+"_"+SwitchMonth+"_"+day;NorthMonthDayYear.setForeground(Color.blue);NorthMonthDayYear.setFont(new Font("TimesRoman",Font.BOLD,17));RightCenter.add(CenterText=new JTextArea("please write today's things."));CenterText.setLineWrap(true);CenterText.setSelectedTextColor(Color.blue);//CenterText.addActionListener(this);RightSouth.add(SouthSave=new JButton(" Save "));SouthSave.setBackground(Color.cyan);SouthSave.addActionListener(this);SouthSave.setActionCommand("Save");RightSouth.add(SouthDelete=new JButton(" Delete "));SouthDelete.setBackground(Color.cyan);SouthDelete.addActionListener(this);SouthDelete.setActionCommand("Delete");this.year = year;this.month = month;this.day = day;///add container to put LeftPane and RightPaneContainer con=getContentPane();JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,RightPane,LeftPane);//gai bian con.add(split,BorderLayout.CENTER);con.validate();//add CenterPane to notepad//CenterPane initializesetFont(new Font("Times New Roman",Font.PLAIN,12));JScrollPane scrollpane = new JScrollPane(CenterText); scrollpane.setPreferredSize(new Dimension(220,250)); RightCenter.add(scrollpane);//init randomaccessfile}//switch the month in englishpublic void SwitchMonth(int month){switch(month){case 1:SwitchMonth="Jan";break;case 2:SwitchMonth="Feb";break;case 3:SwitchMonth="Mar";break;case 4:SwitchMonth="Apr";break;case 5:SwitchMonth="May";break;case 6:SwitchMonth="Jun";break;case 7:SwitchMonth="Jul";break;case 8:SwitchMonth="Aug";break;case 9:SwitchMonth="Sep";break;case 10:SwitchMonth="Qct";break;case 11:SwitchMonth="Nov";break;case 12:SwitchMonth="Dec";break;}}//print the body of the monthpublic void PrintMonth(int year,int month,int day){//Get start day of the week for the first date in the month int startday = GetStartDay(year,month);//Get number of days in the monthint dayinmonth = GetNumOfDaysInMonth(year,month); //Print header//Print bodyPrintMonthBody(startday,dayinmonth,day);}//PrintMonth(int year,int month,int day)'s burdenpublic void PrintMonth(int year,int month){//Get start day of the week for the first date in the monthint startday = GetStartDay(year,month);//Get number of days in the monthint dayinmonth = GetNumOfDaysInMonth(year,month);//Print header//Print bodyPrintMonthBody(startday,dayinmonth);}//PrintMonthBody(int startday,int dayinmonth,int day)'s burden public void PrintMonthBody(int startday,int dayinmonth){for(int i=startday,n=1;i<startday+dayinmonth;i++){ShowDays[i].setText(""+n);ShowDays[i].setHorizontalAlignment(0);//let centerif(n==day){ShowDays[i].setForeground(Color.green);ShowDays[i].setFont(new Font("TimesRoman",Font.BOLD,20)); ShowDays[i].setBackground(Color.DARK_GRAY);}else{ShowDays[i].setFont(new Font("TimesRoman",Font.BOLD,12)); ShowDays[i].setForeground(Color.white);ShowDays[i].setBackground(Color.DARK_GRAY);}n++;}for(int i=0;i<startday;i++){ShowDays[i].setText("");ShowDays[i].setBackground(Color.DARK_GRAY);}for(int i=startday+dayinmonth;i<42;i++){ShowDays[i].setText("");ShowDays[i].setBackground(Color.DARK_GRAY);}//judge leapyear is or notpublic boolean IsLeapYear(int year){if((year%400==0)||(year%4==0&&year%100!=0))return true;elsereturn false;}//judge the start day of a monthpublic int GetStartDay(int year,int month){//get total number of day since1/1/0000int startday0001=-32768;long totalnumofdays=GetTotalNumOfDays(year,month);//return the start dayreturn (int)((totalnumofdays+startday0001)%7);}//judge the days of a yearpublic long GetTotalNumOfDays(int year,int month){long total=0;//get the total days from -32767 to yearfor(int i=-32767;i<year;i++){if(IsLeapYear(i))total=total+366;elsetotal=total+365;}//Add days from jan to the month prior to the calendar monthfor(int i=1;i<month;i++)total=total+GetNumOfDaysInMonth(year,i);return total;}//judge the days of a monthpublic int GetNumOfDaysInMonth(int year,int month){if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 ||month==12) return 31;if(month==4 || month==6 || month==9 || month==11)return 30;if(month==2)if(IsLeapYear(year))return 29;elsereturn 28;}return 0;}public void WriteRecord(){String content;content=CenterText.getText();int n=content.length();char[] contentarr=new char[n];try{int i=0;for(i=0;i<n;i++){contentarr[i]=content.charAt(i);}File Diary = new File("Diary");Diary.mkdir();File myfile=new File("Diary\\"+key+".txt"); FileWriter Record=new FileWriter(myfile);for(i=0;i<contentarr.length;i++){Record.write(contentarr[i]);}Record.close();JOptionPane.showMessageDialog(this,"Save success!");}catch(IOException ex){}}public void ReadRecord(){try{String content="";File myfile=new File("Diary\\"+key+".txt"); FileReader Record=new FileReader(myfile);if(myfile.exists())long b=myfile.length();//char[] contentarr=new char[b];int n=JOptionPane.showConfirmDialog(this,"Today has logs,are you read?","Confirm",JOptionPane.YES_NO_CANCEL_OPTION);if(n==JOptionPane.YES_OPTION){while((b=Record.read())!=-1){content=content+(char)b;}CenterText.setText(content);}}Record.close();}catch(IOException ex){CenterText.setText("Today has not logs.");}}public void DeleteFile(){String filepath="Diary\\"+key+".txt";File myfile=new File(filepath);int n=JOptionPane.showConfirmDialog(this,"Are you sure delete the file?","Confirm",JOptionPane.YES_NO_CANCEL_OPTION);if(n==JOptionPane.YES_OPTION){if(myfile.exists()){Runtime rt = Runtime.getRuntime();try{rt.exec("cmd /c del "+filepath);}catch (IOException e){e.printStackTrace();}JOptionPane.showMessageDialog(this,"Delete successed!");CenterText.setText("Today has not logs.");}elseJOptionPane.showMessageDialog(this,"The file doesn't exist,delete failured!");}}}public void AboutActionListenerWay(){try{prioryear=Integer.parseInt(YearText.getText());priormonth=MonthCombobox.getSelectedIndex()+1;String StrYearText=YearText.getText();changeyearmessage=Integer.parseInt(StrYearText);changemonthmessage=MonthCombobox.getSelectedIndex()+1;monthafterquery=changemonthmessage;yearafterquery=changeyearmessage;SwitchMonth(changemonthmessage);ShowDate.setText(SwitchMonth+" "+","+" "+String.valueOf(changeyearmessage));PrintMonth(changeyearmessage,changemonthmessage);ischange=true;}catch(Exception ee){JOptionPane.showMessageDialog(this,"The input format doesn't match","Error",JOptionPane.ERROR_MESSAGE);}}//do actonlistener thingspublic void actionPerformed(ActionEvent eAction){String ActionCommand=eAction.getActionCommand();//Handle button eventsif(eAction.getSource() instanceof JButton){//Handle the queryif("Query".equals(ActionCommand)){try{AboutActionListenerWay();}catch(Exception ee){JOptionPane.showMessageDialog(this,"The input format doesn't}}//Handle prior monthif("prior".equals(ActionCommand)){if(priormonth>1){priormonth=priormonth-1;}else{priormonth=12;prioryear=prioryear-1;}PrintMonth(prioryear,priormonth,day);SwitchMonth(priormonth);ShowDate.setText(SwitchMonth+" , "+prioryear);NorthMonthDayYear.setText(">>"+SwitchMonth+","+day+","+prioryear+"<<"); key=prioryear+"_"+SwitchMonth+"_"+day;ischange_priornext=true;}//Handle next monthif("next".equals(ActionCommand)){if(priormonth<12){priormonth=priormonth+1;}else{priormonth=1;prioryear=prioryear+1;}PrintMonth(prioryear,priormonth,day);SwitchMonth(priormonth);ShowDate.setText(SwitchMonth+" , "+prioryear);NorthMonthDayYear.setText(">>"+SwitchMonth+","+day+","+prioryear+"<<"); key=prioryear+"_"+SwitchMonth+"_"+day;ischange_priornext=true;}//Handle the "Go to today"if("Go to today".equals(ActionCommand)){YearText.setText("");MonthCombobox.setSelectedIndex(0);SwitchMonth(month);ShowDate.setText(SwitchMonth+" "+","+" "+String.valueOf(year)); NorthMonthDayYear.setText(">>"+year+SwitchMonth+","+day+","+"<<"); key=year+"_"+SwitchMonth+"_"+day;priormonth=month;prioryear=year;ischange=false;}//Handle the "Save"if("Save".equals(ActionCommand)){WriteRecord();}if("Delete".equals(ActionCommand)){DeleteFile();}}//Handle JTextField eventsif(eAction.getSource() instanceof JTextField){//Handle the queryAboutActionListenerWay();}}public void mousePressed(MouseEvent eMouse){int day;try{//Handle ShowDays[] eventsif(ischange==false){JTextField source=(JTextField)eMouse.getSource();day=Integer.parseInt(source.getText());if(ischange_priornext==false){NorthMonthDayYear.setText(">>"+SwitchMonth+","+day+","+year+"<<"); key=year+"_"+SwitchMonth+"_"+day;}elseNorthMonthDayYear.setText(">>"+SwitchMonth+","+day+","+prioryear+"<<");key=prioryear+"_"+SwitchMonth+"_"+day;}}elseJTextField source=(JTextField)eMouse.getSource();day=Integer.parseInt(source.getText());if(ischange_priornext==false){SwitchMonth(changemonthmessage);NorthMonthDayYear.setText(">>"+SwitchMonth+","+day+","+changeyearmessage+"<<"); key=changeyearmessage+"_"+SwitchMonth+"_"+day;}else{SwitchMonth(priormonth);NorthMonthDayYear.setText(">>"+SwitchMonth+","+day+","+prioryear+"<<");key=prioryear+"_"+SwitchMonth+"_"+day;}}ReadRecord();}catch(Exception ee){}}public void mouseClicked(MouseEvent e){}public void mouseReleased(MouseEvent e){}public void mouseEntered(MouseEvent e){}public void mouseExited(MouseEvent e){}public void SaveLog(int year,int month,int day){}public static void main(String[] args) throws Exception{int y=calendar.get(Calendar.YEAR);int m=calendar.get(Calendar.MONTH)+1;int d=calendar.get(Calendar.DAY_OF_MONTH);calendar frame = new calendar(y,m,d);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(560,300);frame.setLocation(100,100);frame.setVisible(true);frame.setResizable(false);frame.ReadRecord();}}四、设计结果与分析4.1程序效果图程序打开时的效果,左边是记事本框,右边是万年历框。
目录前言 (2)1需求分析 (2)1.1需求分析 (2)1.2功能设计 (2)2.概要设计 (2)2.1程序设计思路 (2)2.2程序运行界面 (3)2.3流程图 (3)3.各模块的功能及程序说明 (4)3.1.初始化组件 (4)3.2.初始化数据 (4)3.3.绘制程序界面 (5)4.测试分析 (6)5.源程序清单 (8)6.课程设计体会 (16)参考文献 (17)前言Java的前身是Oak,它一开始只是被应用于消费性电子产品中。
后来它的开发者们发现、可移植和高效能等众多优点。
Java支持多线程编程,Java运行时系统在多线程同步方面具有成熟的解决方案。
Java的平台标准有Java ME,Java SE 和Java EE。
Java发展到今天,它的卓越成就及在业界的地位毋庸置疑。
目前在众多的支持Java的开发工具中主要的7有Java Development Kit,NetBeans,Jcreator,JBuilder,JDeveloper和Eclipse等。
其中Java Development Kit 简称JDK是大多开发工具的基础。
以上的每种开发工具都有优缺点,对于开发者来说,重要的是要根据自己的开发规模、开发内容和软硬件环境等因素来选择一种合适的开发工具。
1需求分析1.1需求分析本程序的要求为:1.使用图形用户界面;2.本程序能够实现日期与星期的查询。
1.2功能设计本程序要构建的万年历程序,其功能有以下几个方面:(1)通过网页形式运行,实现图形界面。
(2)能以月历形式显示日期与星期。
(3)支持用户自己输入年份,并提供月份的下拉形式菜单来选择月份。
(4)通过点击“更新”来刷新日历。
2.概要设计2.1程序设计思路1. 总天数的算法:首先用if语句判断定义年到输入年之间每一年是否为闰年,是闰年,该年的总天数为366,否则,为365。
然后判断输入的年是否为定义年,若是,令总天数S=1,否则,用累加法计算出定义年到输入年之间的总天数,再把输入年的一月到要输出的月份之间的天数累加起来,若该月是闰年中的月份并且该月还大于二月,再使总天数加1,否则,不加,既出输出月份第一天为星期几后,把该日期以前的位置用空格补上,并总该日起一次输出天数直到月底,该月中的天数加上该月一日为星期几的数字再除以7得0换行,即可完整的输出该月的日历。
2.2.2程序流程框图
开始
初始化
读、写日期、时间和温度
分离日期\时间\温度显示值
显示子程序
定时闹铃子程序
日期、时间修改子程序
农历自动更新子程序
闰月子程
返回
图1 主程序流程图
图2计算阳历程序流程图
图3时间调整程序流程图
课程设计说明书N O.19
课程设计说明书N O.20 3、设计结果与分析
3.1程序效果图
3.1.1程序打开时的效果,左边是记事本框,右边是万年历框。
图4程序效果图
3.1.2月份下拉列表的视图,选择某月很方便的。
图5程序效果图
3.1.3任选一个日期,在此日期中输入日记的信息。
效果如下图:
图6程序效果图
课程设计说明书N O.21 3.1.4保存日历成功对话框,选择“save”按键后自动弹出此对话框。
图7程序效果图
3.1.5当想删除所写日记时此确认对话框可有效防止误删。
图8程序效果图
3.1.6删除成功后会显示此对话框,提示删除成功。
图9程序效果图
3.2程序分析
该程序能够很好得实现万年历得功能,包括选择年份,月份,天份。
而且从外观上看不但简洁实用而且很美观。
程序打开的时候,能看到。
左边显示得是记事本框,右边显示是万年历框,简洁明了。
具有月份下拉功能,需要月份下拉得时候,可以随意调到自己需要得月份进行查看,十分方便。
可以任选一个日期,并在此日期中输入日记的信
课程设计说明书N O.22。
Java课程设计题目万年历班级计本0901班姓名秘密学号200908111053指导教师韩瑞峰要求:功能要求:可两人对下(用鼠标操作),判断胜负。
界面要求:美观简洁,有简单操作说明。
设计方案界面设计界面主要由两部分组成——查询输入部分和结果显示部分。
查询输入部分包括一个文本域,用于输入年份:一个选择下拉框,用于选择月份:一个选择按扭,用来出发查询事件。
具体使用的方法见注释流程图源代码import java.applet.Applet;import java.awt.*;import java.util.*;public class CalendarApplet extends Applet{static final int TOP = 80; //顶端距离static final int CELLWIDTH=50,CELLHEIGHT = 50; //单元格尺寸static final int MARGIN = 3; //边界距离static final int FEBRUARY = 1;TextField tfYear = new TextField("2011", 6); //显示年份的文本域Choice monthChoice = new Choice(); //月份选择下拉框Button btUpdate = new Button("选择"); //选择按钮GregorianCalendar calendar=new GregorianCalendar(); //日历对象Font smallFont = new Font("TimesRoman", Font.PLAIN, 15); //显示小字体Font bigFont = new Font("TimesRoman", Font.BOLD, 50); //显示大字体String days[] = {"星期日", "星期一", "星期二", "星期三","星期四", "星期五", "星期六"};String months[] = {"一月", "二月", "三月", "四月","五月", "六月", "七月", "八月", "九月","十月", "十一月", "十二月"};int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //每个月的天数int searchMonth,searchYear; //查询的年份及月份public void init(){setBackground(Color.white); //设置背景颜色searchMonth = calendar.get(Calendar.MONTH); //得到系统年份searchYear = calendar.get(Calendar.YEAR); //得到系统月份add(new Label(" 年:")); //增加组件到ApplettfYear.setText(String.valueOf(searchYear)); //设置文本域文字add(tfYear);add(new Label(" 月:"));monthChoice.setFont(smallFont); //设置月份选择下拉框的显示字体for (int i = 0; i < 12; i++) {monthChoice.addItem(months[i]); //增加下拉框选项}monthChoice.select(searchMonth); //设置下拉框当前选择项add(monthChoice);add(btUpdate);int componentCount=this.getComponentCount(); //得到Applet中的组件数量for (int i=0;i<componentCount;i++){getComponent(i).setFont(smallFont); //设置所有组件的显示字体}}public void paint(Graphics g){FontMetrics fontMetric; //显示字体的FontMetrics对象int fontAscent;int dayPos;int totalWidth, totalHeight; //总的宽度,高度int numRows; //行数int xNum, yNum; //水平和垂直方向单元格数量int numDays;String dayStr; //显示天数字符串int margin;g.setColor(Color.red); //设置当前颜色g.setFont(bigFont); //设置当前使用字体g.drawString(searchYear+"年",60,TOP+70); //绘制字符串g.drawString((searchMonth+1)+"月",200,TOP+130);g.setColor(Color.black);g.setFont(smallFont);fontMetric = g.getFontMetrics(); //获取变量初值fontAscent = fontMetric.getAscent();dayPos = TOP + fontAscent / 2;totalWidth = 7 * CELLWIDTH; //得到总的表格宽度for (int i = 0; i < 7; i++) {g.drawString(days[i], (CELLWIDTH-fontMetric.stringWidth(days[i]))/2 +i*CELLWIDTH,dayPos-20); //绘制表格标题栏}numRows = getNumberRows(searchYear, searchMonth); //计算需要的行的数量totalHeight = numRows * CELLHEIGHT; //得到总的表格高度for (int i = 0; i <= totalWidth; i += CELLWIDTH) {g.drawLine(i, TOP , i, TOP+ totalHeight); //绘制表格线}for (int i = 0, j = TOP ; i <= numRows; i++, j += CELLHEIGHT) {g.drawLine(0, j, totalWidth, j); //绘制表格线}xNum = (getFirstDayOfMonth(searchYear, searchMonth) + 1) * CELLWIDTH - MARGIN;yNum = TOP + MARGIN + fontAscent;numDays = daysInMonth[searchMonth] + ((calendar.isLeapYear(searchYear) &&(searchMonth == FEBRUARY)) ? 1 : 0);for (int day = 1; day <= numDays; day++) {dayStr = Integer.toString(day);g.drawString(dayStr, xNum - fontMetric.stringWidth(dayStr), yNum); //绘制字符串xNum += CELLWIDTH;if (xNum > totalWidth) {xNum = CELLWIDTH - MARGIN;yNum += CELLHEIGHT;}}}public boolean action(Event e, Object o){int searchYearInt;if (e.target==btUpdate){searchMonth = monthChoice.getSelectedIndex(); //得到查询月份searchYearInt = Integer.parseInt(tfYear.getText(), 10); //得到查询年份if (searchYearInt > 1581) {searchYear = searchYearInt;}repaint(); //重绘屏幕return true;}return false;}private int getNumberRows(int year, int month) { //得到行数量int firstDay;int numCells;if (year < 1582) { //年份小于1582年,则返回-1return (-1);}if ((month < 0) || (month > 11)) {return (-1);}firstDay = getFirstDayOfMonth(year, month); //计算月份的第一天if ((month == FEBRUARY) && (firstDay == 0) && !calendar.isLeapYear(year)) { return 4;}numCells = firstDay + daysInMonth[month];if ((month == FEBRUARY) && (calendar.isLeapYear(year))) {numCells++;}return ((numCells <= 35) ? 5 : 6); //返回行数}private int getFirstDayOfMonth(int year, int month) { //得到每月的第一天int firstDay;int i;if (year < 1582) { //年份小于1582年,返回-1return (-1);}if ((month < 0) || (month > 11)) { //月份数错误,返回-1return (-1);}firstDay = getFirstDayOfYear(year); //得到每年的第一天for (i = 0; i < month; i++) {firstDay += daysInMonth[i]; //计算每月的第一天}if ((month > FEBRUARY) && calendar.isLeapYear(year)) {firstDay++;}return (firstDay % 7);}private int getFirstDayOfYear(int year){ //计算每年的第一天int leapYears;int hundreds;int fourHundreds;int first;if (year < 1582) { //如果年份小于1582年return (-1); //返回-1}leapYears = (year - 1581) / 4;hundreds = (year - 1501) / 100;leapYears -= hundreds;fourHundreds = (year - 1201) / 400;leapYears += fourHundreds;first=5 + (year - 1582) + leapYears % 7; //得到每年第一天return first;}}程序运行效果初始界面选择日期后收获感悟经过这次课程设计使我们对JA V A图形用户界面的有关知识有了更深入了解,理,完成了简单的日历功能,输入查询的年份与月份,单击“选择”按扭,则可得到相应的日历,简单实用。
工业职业技术学院课程名称JA V A程序设计班级: 网络技术081姓名:王卉指导老师:目录前言 (2)1需求分析 (2)1。
1需求分析 (2)1。
2功能设计 (2)2。
概要设计 (2)2。
1程序设计思路 (2)2.2程序运行界面 (3)2。
3流程图 (4)3。
各模块的功能及程序说明 (4)3.1。
初始化组件 (4)3.2.初始化数据 (5)3。
3。
绘制程序界面 (6)4.测试分析 (7)5.源程序清单 (9)6.课程设计体会 (17)参考文献 (17)前言Java的前身是Oak,它一开始只是被应用于消费性电子产品中。
后来它的开发者们发现它还可以被用于更大范围的Internet上。
1995年,Java语言的名字从Oak编程了Java。
1997年J2SE1.1发布。
1998年J2SE1.2发布,标志Java2的诞生。
十多年来,Java编程语言及平台成功地运用在网络计算及移动等各个领域.Java的体系结构由Java语言、Java class、Java API、Java虚拟机组成。
它具有简单、面向对象、健壮、安全、结构中立、可移植和高效能等众多优点。
Java 支持多线程编程,Java运行时系统在多线程同步方面具有成熟的解决方案.Java 的平台标准有Java ME,Java SE和Java EE。
Java发展到今天,它的卓越成就及在业界的地位毋庸置疑。
目前在众多的支持Java的开发工具中主要的7有Java Development Kit,NetBeans,Jcreator,JBuilder,JDeveloper和Eclipse等。
其中Java Development Kit 简称JDK是大多开发工具的基础。
以上的每种开发工具都有优缺点,对于开发者来说,重要的是要根据自己的开发规模、开发内容和软硬件环境等因素来选择一种合适的开发工具.1需求分析1.1需求分析本程序的要求为:1.使用图形用户界面;2.本程序能够实现日期与星期的查询。
用Java程序编写万年历程序的设计报告一、引言万年历是一种常见的工具,用于显示特定日期的星期几、农历日期、节气等信息。
本设计报告旨在介绍使用Java编写万年历程序的设计过程和实现细节。
二、需求分析1. 显示当前日期的星期几和农历日期;2. 支持用户输入指定日期,显示该日期的星期几和农历日期;3. 显示当月的日历,并标记当天;4. 支持用户选择不同年份和月份,显示相应的日历;5. 显示当天的节气。
三、设计思路1. 日期计算:使用Java提供的日期和时间类,如`java.util.Calendar`,来进行日期计算和格式化;2. 农历计算:根据农历算法,计算指定日期的农历日期和节气;3. 用户界面:使用Java图形用户界面(GUI)库,如`javax.swing`,来实现用户界面;4. 数据展示:使用文本框、标签等组件来展示日期、星期几、农历日期和节气。
四、程序实现1. 创建Java项目,并导入所需的库;2. 创建主界面窗口,设置布局和组件;3. 添加日期选择器,用于选择年份和月份;4. 添加按钮,用于触发日期查询和切换月份;5. 根据用户选择的日期,计算并显示星期几、农历日期和节气;6. 根据用户选择的年份和月份,计算并显示当月的日历;7. 标记当天的日期;8. 添加事件监听器,处理用户的操作。
五、代码示例```java// 导入所需的库import java.util.Calendar;import java.util.Date;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class CalendarProgram extends JFrame {private JLabel dateLabel;private JLabel weekdayLabel;private JLabel lunarLabel;private JLabel festivalLabel;private JPanel calendarPanel;public CalendarProgram() {// 设置窗口标题和大小setTitle("万年历程序");setSize(800, 600);// 创建主界面布局和组件JPanel mainPanel = new JPanel(); dateLabel = new JLabel();weekdayLabel = new JLabel(); lunarLabel = new JLabel();festivalLabel = new JLabel();calendarPanel = new JPanel();// 将组件添加到主界面布局中 mainPanel.add(dateLabel);mainPanel.add(weekdayLabel); mainPanel.add(lunarLabel);mainPanel.add(festivalLabel);mainPanel.add(calendarPanel); // 设置主界面布局和显示setContentPane(mainPanel);setVisible(true);}public void updateCalendar(Date selectedDate) {// 根据选择的日期更新界面显示Calendar calendar = Calendar.getInstance();calendar.setTime(selectedDate);// 计算并显示星期几、农历日期和节气int weekday = calendar.get(Calendar.DAY_OF_WEEK); String lunarDate = calculateLunarDate(calendar);String festival = calculateFestival(calendar);dateLabel.setText("日期:" + selectedDate.toString()); weekdayLabel.setText("星期几:" + weekday);lunarLabel.setText("农历日期:" + lunarDate);festivalLabel.setText("节气:" + festival);// 计算并显示当月的日历int year = calendar.get(Calendar.YEAR);int month = calendar.get(Calendar.MONTH);int daysInMonth = calculateDaysInMonth(year, month); displayCalendar(year, month, daysInMonth);}private String calculateLunarDate(Calendar calendar) {// 根据农历算法计算农历日期// 实现略}private String calculateFestival(Calendar calendar) {// 根据日期计算节气// 实现略}private int calculateDaysInMonth(int year, int month) {// 计算指定年份和月份的天数// 实现略}private void displayCalendar(int year, int month, int daysInMonth) { // 显示当月的日历// 实现略}public static void main(String[] args) {CalendarProgram program = new CalendarProgram();program.updateCalendar(new Date());}}```六、总结通过使用Java编写万年历程序,我们可以实现日期计算、农历计算和节气计算等功能,并通过图形界面展示给用户。
JAVA软件项目设计.................................................................. 错误!未定义书签。
项目报告............................................................................. 错误!未定义书签。
1.设计分析: (2)1.1需求分析 (2)1.2功能设计 (2)2.程序结构: (3)3.各模块功能及程序说明: (4)3.1 各种包的导入: (4)3.2控件的初始化 (4)3.3绘制万年历的界面 (5)3.4 增加控件 (6)3.5增加监听 (6)3.6封装操作 (7)4.源程序 (7)5.操作方法(流程) (22)6.实验结果 (22)7.心得体会: (24)1.设计分析:1.1需求分析对于万年历系统,其主要功能是:查看当日日期(年、月、日)、查看当时时间、查看其他年月的月历、对选中的日期进行标识。
主要使用流程是:查看万年历系统,查看其他月份或年份日历,选中其他日期查看,查看当日日期。
1.2功能设计本程序要构建的万年历程序,其功能有以下几个方面:(1)通过JDK环境运行,实现图像界面。
(2)能以月历的形式显示日期与星期。
(3)支持用户自己用下拉形式来选择年份和月份。
(4)通过点击“更新时间”按钮来刷新当前时间。
2.程序结构:3.各模块功能及程序说明:3.1 各种包的导入:import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.GridLayout;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Calendar;import java.util.GregorianCalendar;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;3.2控件的初始化contentPane = new JPanel(new BorderLayout());// 主面板northPane = new JPanel(new BorderLayout()); // 放按钮面板的面板centerPane = new JPanel(new GridLayout(7, 7)); // 中间的日期(七行七列)面板buttonPane = new JPanel(); // 按钮面板labelYear = new JButton("年份:"); // 显示年份文字labelMonth = new JButton("月份:"); // 显示月份文字buttonPreYear = new JButton("<<"); // 上一年按钮buttonNextYear = new JButton(">>"); // 下一年的按钮buttonPreMonth = new JButton("<<"); // 上一月按钮buttonNextMonth = new JButton(">>");// 下一月按钮newTime = new JButton("更新时间");boxYear = new JComboBox(); // 年的下拉列表boxMonth = new JComboBox(); // 月的下拉列表ca = new GregorianCalendar();Jhour = new JTextField(2);Jminute = new JTextField(2);Jsecond = new JTextField(2);date = new JPanel();dateTime1 = new JLabel("时");dateTime2 = new JLabel("分");dateTime3 = new JLabel("秒");3.3绘制万年历的界面public WanNianLi() {this.setTitle("万年历");this.setSize(500, 400);// 将面板居中处理Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); int screenWidth = d.width;int screenHeight = d.height;this.setLocation((screenWidth - this.getWidth()) / 2,(screenHeight - this.getHeight()) / 2);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(true);}3.4 增加控件// 把按钮放到了按钮面板里buttonPane.add(labelYear);buttonPane.add(buttonPreYear);buttonPane.add(boxYear);buttonPane.add(buttonNextYear);buttonPane.add(labelMonth);buttonPane.add(buttonPreMonth);buttonPane.add(boxMonth);buttonPane.add(buttonNextMonth);// 把按钮面板放到了northPane中northPane.add(buttonPane, BorderLayout.CENTER); // 把northPane和centerPane放到主面板中contentPane.add(northPane, BorderLayout.NORTH); contentPane.add(centerPane, BorderLayout.CENTER); contentPane.add(date,BorderLayout.SOUTH);3.5增加监听//给年份下拉列表添加事件监听器boxYear.addActionListener(new ActionListener())//给月份下拉列表添加事件监听器boxMonth.addActionListener(new ActionListener())//上一年的按钮监听buttonPreYear.addActionListener(new ActionListener())//下一年的按钮监听buttonNextYear.addActionListener(new ActionListener() //上一月的监听buttonPreMonth.addActionListener(new ActionListener() //下一月的监听buttonNextMonth.addActionListener(new ActionListener() //更新时间按钮的监听newTime.addActionListener(new ActionListener() )3.6封装操作// 封装给年份下拉列表赋值的操作private void initBoxYear()//封装给月份下拉列表赋值的操作private void initBoxMonth()4.源程序package 万年历;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.GridLayout;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Calendar;import java.util.GregorianCalendar;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;public class WanNianLi extends JFrame{private int temp0 = 0;private int year = 0;private int month = 0;private int hour = 0;private int minute = 0;private int second = 0;Calendar ca = null;private JTextField Jhour = null;private JTextField Jminute = null;private JTextField Jsecond = null;private JPanel contentPane = null;// 主面板private JPanel northPane = null; // 放按钮面板的面板private JPanel centerPane = null; // 中间的日期面板private JPanel buttonPane = null; // 按钮面板private JPanel time = null; //时钟面板private JPanel date = null;private JButton labelYear = null; // 显示年份文字private JButton labelMonth = null; // 显示月份文字private JButton buttonPreYear = null; // 上一年按钮private JButton buttonNextYear = null; // 下一年的按钮private JButton buttonPreMonth = null; // 上一月按钮private JButton buttonNextMonth = null;// 下一月按钮private JButton newTime = null;private JComboBox boxYear = null; // 年的下拉列表private JComboBox boxMonth = null; // 月的下拉列表private JLabel[] labelDays = null; // 存储天private JLabel dateTime1 = null;private JLabel dateTime2 = null;private JLabel dateTime3 = null;public WanNianLi() {this.setTitle("万年历");this.setSize(500, 400);// 将面板居中处理Dimension d = Toolkit.getDefaultToolkit().getScreenSize();int screenWidth = d.width;int screenHeight = d.height;this.setLocation((screenWidth - this.getWidth()) / 2,(screenHeight - this.getHeight()) / 2);init(); // 封装初始化主控件的操作this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(true);}// 初始化主控件private void init() {contentPane = new JPanel(new BorderLayout());// 主面板northPane = new JPanel(new BorderLayout()); // 放按钮面板的面板centerPane = new JPanel(new GridLayout(7, 7)); // 中间的日期(七行七列)面板buttonPane = new JPanel(); // 按钮面板labelYear = new JButton("年份:"); // 显示年份文字labelMonth = new JButton("月份:"); // 显示月份文字buttonPreYear = new JButton("<<"); // 上一年按钮buttonNextYear = new JButton(">>"); // 下一年的按钮buttonPreMonth = new JButton("<<"); // 上一月按钮buttonNextMonth = new JButton(">>");// 下一月按钮newTime = new JButton("更新时间");boxYear = new JComboBox(); // 年的下拉列表initBoxYear();boxMonth = new JComboBox(); // 月的下拉列表initBoxMonth();ca = new GregorianCalendar();hour = ca.get(Calendar.HOUR_OF_DAY); minute = ca.get(Calendar.MINUTE);second = ca.get(Calendar.SECOND);Jhour = new JTextField(2);Jhour.setText(hour+"");Jminute = new JTextField(2);Jminute.setText(minute+"");Jsecond = new JTextField(2);Jsecond.setText(second+"");date = new JPanel();dateTime1 = new JLabel("时");dateTime2 = new JLabel("分");dateTime3 = new JLabel("秒");date.add(Jhour);date.add(dateTime1);date.add(Jminute);date.add(dateTime2);date.add(Jsecond);date.add(dateTime3);date.add(newTime);// 把按钮放到了按钮面板里buttonPane.add(labelYear);buttonPane.add(buttonPreYear);buttonPane.add(boxYear);buttonPane.add(buttonNextYear);buttonPane.add(labelMonth);buttonPane.add(buttonPreMonth);buttonPane.add(boxMonth);buttonPane.add(buttonNextMonth);// 把按钮面板放到了northPane中northPane.add(buttonPane, BorderLayout.CENTER);// 初始化放日期的labellabelDays = new JLabel[49]; // 存储天for (int i = 0; i < labelDays.length; i++){labelDays[i] = new JLabel("", JLabel.CENTER);centerPane.add(labelDays[i]);}boxYear.setSelectedItem(2013);boxMonth.setSelectedItem(12);YMD();//将第一界面设为系统当前事件setDate();// 把northPane和centerPane放到主面板中contentPane.add(northPane, BorderLayout.NORTH); contentPane.add(centerPane, BorderLayout.CENTER); contentPane.add(date,BorderLayout.SOUTH);this.add(contentPane);//更新时分秒newTime.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {Calendar ca = null;ca = new GregorianCalendar();int a = ca.get(Calendar.HOUR_OF_DAY);int b = ca.get(Calendar.MINUTE);int c = ca.get(Calendar.SECOND);Jhour.setText(a+"");Jminute.setText(b+"");Jsecond.setText(c+"");}});//给年份下拉列表添加事件监听器boxYear.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){year = (Integer)boxYear.getSelectedItem();setDate();});//给月份下拉列表添加事件监听器boxMonth.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e){month = (Integer)boxMonth.getSelectedItem();setDate();}});//上一年的按钮监听buttonPreYear.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {int resertYear = (Integer) boxYear.getSelectedItem();boxYear.setSelectedItem(resertYear-1);}});//下一年的按钮监听buttonNextYear.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e)int resertYear = (Integer) boxYear.getSelectedItem();boxYear.setSelectedItem(resertYear+1);}});//上一月的监听buttonPreMonth.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e){int resertMonth =(Integer)boxMonth.getSelectedItem();int resertYear = (Integer) boxYear.getSelectedItem();if(resertMonth == 1){if(resertYear == 1901){boxMonth.setSelectedItem(resertMonth);boxYear.setSelectedItem(resertYear);}else{resertMonth = 12;boxMonth.setSelectedItem(resertMonth);boxYear.setSelectedItem(resertYear-1);}}else{boxMonth.setSelectedItem(resertMonth-1);}}});//下一月的监听buttonNextMonth.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e){int resertMonth =(Integer)boxMonth.getSelectedItem();if(resertMonth == 12){resertMonth = 1;boxMonth.setSelectedItem(resertMonth);int resertYear = (Integer)boxYear.getSelectedItem();boxYear.setSelectedItem(resertYear+1);}else{boxMonth.setSelectedItem(resertMonth+1);}}});}// 封装给年份下拉列表赋值的操作private void initBoxYear(){for (int i = 0; i < 1000; i++){boxYear.addItem(1901 + i);}}//封装给月份下拉列表赋值的操作private void initBoxMonth(){for (int i = 1; i <= 12; i++){boxMonth.addItem(i);}}public void setDate() {String[] tempDate = new String[42];tempDate = initDate();// 给前七个label赋属性String[] logs = new String[] { "日", "一", "二", "三", "四", "五", "六" };//将logs数组中的值放到label中for (int i = 0; i < 7; i++){labelDays[i].setText(logs[i]);}//初始化label的颜色for (int i = 0; i < labelDays.length; i++) {labelDays[i].setForeground(Color.BLACK);}// 设置星期日的颜色为红色for (int i = 0; i < labelDays.length; i += 7){labelDays[i].setForeground(Color.red);}// 设置星期六的颜色为蓝色for (int i = 6; i < labelDays.length; i += 7){labelDays[i].setForeground(Color.blue);}//将日期放到label中,并把系统当天的时间设为绿色for (int i = 7; i < 49; i++){int t = 0;t = day();labelDays[i].setText(tempDate[i-7]);if(i == Calendar.getInstance().get(Calendar.DAY_OF_MONTH)){labelDays[i+t+6].setForeground(Color.GREEN);}}}//设置面板第一次显示的内容void YMD(){year=0;month=0;Calendar c=Calendar.getInstance();//获得系统当前日期year=c.get(Calendar.YEAR);month=c.get(Calendar.MONTH)+1;//系统日期从0开始算起boxYear.setSelectedItem((Integer)year);boxMonth.setSelectedItem((Integer)month);}//将某年某月中的日期放到一个数组中返回,包括前面的空余部分public String[] initDate(){String[] date = new String[42];int sum = 0;int m = 0;for (int i = 1900; i < year; i++) {if (((i % 4 == 0) && (i % 100 != 0)) || (i % 400 == 0)) {sum = sum + 366;} else {sum = sum + 365;}}for (int nomonth = 1; nomonth <= month; nomonth++) { if (nomonth == 1 || nomonth == 3 || nomonth == 5 || nomonth == 7|| nomonth == 8 || nomonth == 10 || nomonth == 12) {m = 31;} else if (nomonth == 2) {if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {m = 29;} else {m = 28;}} else {m = 30;}if (nomonth < month) {sum = sum + m;}}sum += 1;temp0 = (sum % 7);for (int p = 0; p < temp0; p++) {date[p] = "";}for (int w = 1; w <= m; w++) {date[temp0++] = w + "";}return date;}//获取系统当前月第一天离星期天有几天public int day(){int temp = 0;int sum = 0;int m = 0;for (int i = 1900; i < year; i++) {if (((i % 4 == 0) && (i % 100 != 0)) || (i % 400 == 0)) {sum = sum + 366;} else {sum = sum + 365;}}for (int nomonth = 1; nomonth <= month; nomonth++) { if (nomonth == 1 || nomonth == 3 || nomonth == 5 || nomonth == 7|| nomonth == 8 || nomonth == 10 || nomonth == 12) {m = 31;} else if (nomonth == 2) {if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {m = 29;} else {m = 28;}} else {m = 30;}if (nomonth < month) {sum = sum + m;}}sum += 1;temp = (sum % 7);return temp;}public static void main(String[] args){new WanNianLi();}}5.操作方法(流程)5.1 制作万年历的界面5.2 在界面上增加控件5.3 给事件增加监听5.4给事件增加命令符5.5运行调试6.实验结果6.1 获取当前时间6.2 获取闰年2月的时间6.3获取平年2月的时间6.4选择2010年2月的时间7.心得体会:通过这次的项目设计,我们学到了很多东西,下面就是这次项目设计的心得体会:在这次的项目设计中,主要是运用Java语言来编写,并可运用其他的技术。
JA V A课程设计报告模拟时钟系别:计算机系班级:计本1119班姓名:高鹏飞学号:1162041928指导教师:白茹意一、课程设计任务及要求(1)设计任务:编写一个Java 万年历程序,实现日期和星期的查询。
(2)设计要求:1、主界面采用边界布局,北面是一个设置年月的控制面板,中间是日历绘制区;2、单击“月份”的下拉式菜单,可以显示当前月的上一月/下一月的日历;3、单击“年份”的下拉式菜单,可以显示上一年/下一年的当前日历;二、需求分析对于万年历系统,其主要功能是:查看当日日期(年、月、日)、查看当时时间、查看其他年月的月历。
主要使用流程是:查看万年历系统,查看其他月份或年份日历,选中其他日期查看,查看当日日期。
主要功能的用例描述如下:(1)查看万年历1、进入万年历系统;2、系统获取当日日期并设置日历为当月,红框选中当日日期;(2)查询当月其他年份改变年份的方法是选择年份。
下拉菜单选择年份:1、点击选择年份的下拉菜单,选择年份;2、系统获取已选择的年份;3、系统设置为已选年份当月的月历并显示在界面显示。
(3)查询当年的其他月份改变月份的方法是选择月份。
下拉菜单选择月份:1、点击选择月份的下拉菜单,选择年份;2、系统获取已选择的月份;3、系统设置已选月份为当年的月历并在界面显示。
(4)选择其他日期查看1、点击其他日期;2、系统对所有日期的label进行监听,判断点击的那个label;3、返回被点击的label的数组下标;4、对该下标的label设置蓝色边框,表示选中。
三、设计思路本课设界面由边界布局构成,主要有北方部件和中央部件两部分。
(1)北方部件上有查询输入部分和时间显示部分:1、两个下拉列表用于选择的年份和月份,如改变年份后系统用getShowYear()获得组合框中的中文年份,getCombYear()转换成英文格式,得到用户选择的年份,同时用getMonthDays()和getInitWeek()得到界面的设定,最终,用setDays()设置出用户所要求的月历,改变月份同理;2、四个button按钮用于实现年—、年+、月—、月+,如摁了“年+”后,系统用getShowYear()和getCombYear()获得组合框中的年份,在其年份上加1,将改变的年份显示在组合框里,计算出该年月日历的安排,并用setDays()设置出用户所要求得月历,其他按钮同理;3、一个button按钮用于实现对今日日期的查找,当用户摁了“今日”后,系统用getNowYear()和getNowMonth()得到当日年月,并调用setNowDay()将系统组合框内年月设为当日年月,显示当月月历,同时系统用getNowDay()获得当日日期,用labelDay[i].setBorder(border1)语句将当日的日期用蓝色的边框框住表示选定;4、创建一个标签用于显示时间,监听系统时间并显示。
用Java程序编写万年历程序的设计报告设计报告:用Java程序编写万年历程序一、引言万年历是一种常见的日历工具,用于显示特定年份和月份的日历信息。
本设计报告旨在详细描述用Java程序编写万年历程序的设计过程和实现方法。
二、需求分析1. 功能需求:a) 显示指定年份的全年日历信息;b) 显示指定月份的日历信息;c) 支持用户输入指定年份和月份;d) 提供用户友好的界面,方便操作。
2. 非功能需求:a) 界面美观、简洁;b) 程序运行稳定、快速响应。
三、设计思路1. 用户界面设计:a) 使用Java Swing库创建图形用户界面(GUI);b) 设计主窗口,包含输入框和按钮,用于用户输入年份和月份;c) 设计日历显示区域,用于显示日历信息。
2. 数据结构设计:a) 定义一个Calendar类,用于处理日历相关的计算和数据操作;b) 使用HashMap或二维数组存储每个月份的天数和星期几;c) 使用数组或集合存储月份和星期的名称。
3. 功能实现:a) 根据用户输入的年份和月份,调用Calendar类的方法计算指定月份的日历信息;b) 将日历信息显示在界面的日历显示区域中;c) 实现按钮的事件监听,当用户点击按钮时,触发相应的操作。
四、程序结构设计1. 主类设计:a) 创建一个Main类作为程序入口;b) 在Main类中创建GUI窗口对象,并设置窗口的属性和布局;c) 添加事件监听器,处理用户输入和按钮点击事件。
2. Calendar类设计:a) 定义一个Calendar类,包含计算日历信息的方法和数据成员;b) 实现计算指定年份和月份的日历信息的方法,返回一个二维数组或字符串;c) 使用日期类(如java.util.Calendar)来辅助计算。
五、界面设计1. 主窗口设计:a) 创建一个JFrame对象作为主窗口;b) 设置窗口的标题、大小和关闭操作;c) 使用布局管理器(如FlowLayout或GridBagLayout)布置输入框和按钮。
附录二程序源代码#include <stdio.h>#include <stdlib.h>#include <conio.h>#define X " Sun Mon Tue Wed Thu Fri Sat"void menu(){system("cls");printf("\n+++++++++++++++++++++++++++++++++欢迎使用!++++++++++++++++++++++++++++++++");printf("\n系统菜单!");printf("\n你可以选择一下选项:");printf("\n\n");printf("1 显示系统时间设置闹钟.\n");printf("2 找出这年是不是闰年.\n");printf("3 输入年份显示当年日历.\n");printf("4 退出系统.\n");printf("\n+++++++++++++++++++制作者:信管082001班胡德谋++++++++++++++++++++++++");printf("输入你的选项:");}int getday(int year,int month,int date){int isleap(int year);int flag,s,i;int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};int cont=0;flag=isleap(year);if(flag==1)a[2]++;for(i=1;i<month;i++){cont=cont+a[i];}cont=cont+date;s=year+1+(year-1)/4+(year-1)/100+(year-1)/400+cont;return s%7;}int isleap(int year){if(year%4==0&&year%100||year%400==0)return 1;elsereturn 0;}void print(int n){int i;for(i=0;i<n;i++) printf(" ");}int day(int year){long a,b;if(year<=2000){a=2000-year;b=6-(a+a/4-a/100+a/400)%7;return b;}else{a=year-2000;b=(a+1+(a-1)/4-(a-1)/100+(a-1)/400)%7+6;return b%7;}}void printcalendar(int year){int i,j,k,m,n,f1,f2,d;int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; printf("\nThe calendar of the year %d.\n\n",year); d=day(year);if(isleap(year)==1)a[2]++;for(i=1;i<=12;i+=2){m=0; n=0; f1=0; f2=0;switch(i){case 1:printf(" Januray 1 ");break;case 3:printf(" March 3 ");break;case 5:printf(" May 5 ");break;case 7:printf(" July 7 "); break;case 9:printf(" September 9 ");break; case 11:printf(" Nevember 11 ");break; }print(21);switch(i+1){case 2:printf(" February 2 "); break; case 4:printf(" April 4 "); break; case 6:printf(" June 6 "); break;case 8:printf(" August 8 "); break; case 10:printf(" October 10 "); break; case 12:printf(" December 12"); break; }printf("\n");printf(X);print(6);printf(X);printf("\n");for(j=0;j<6;j++){if(j==0){print(d*4);for(k=0;k<7-d;k++){printf("%4d",++m);}print(6);d+=a[i]%7;d%=7;print(d*4);for(k=0;k<7-d;k++){printf("%4d",++n);}printf("\n");}else{for(k=0;k<7;k++){if(m<a[i]){printf("%4d",++m);}else{print(4);}if(m==a[i]) f1=1;}print(6);for(k=0;k<7;k++){if(n<a[i+1]){printf("%4d",++n);}else{print(4);}if(n==a[i+1]) f2=1;}printf("\n");if(f1&&f2) break;}}d+=a[i+1]%7;d%=7;printf(" ");for(k=0;k<27;k++){printf("=");}print(6);printf(" ");for(k=0;k<27;k++){printf("=");}printf("\n");if(i==5){printf("Press any key to others!"); getch();system("cls");}}}void main(){int choice;int year,month,date;int day,flag;char con;menu();scanf("%d",&choice);if(choice==1){r1:system("cls");printf("\n请按这样的年月日格式输入(XXXX,XX,XX):"); scanf("%d,%d,%d",&year,&month,&date);day=getday(year,month,date);if(day==0)printf("\n%d-%d-%d is Sunday!\n",year,month,date);if(day==1)printf("\n%d-%d-%d is Monday!\n",year,month,date);if(day==2)printf("\n%d-%d-%d is Tuesday!\n",year,month,date); if(day==3)printf("\n%d-%d-%d is Wednesday!\n",year,month,date); if(day==4)printf("\n%d-%d-%d is Thursday!\n",year,month,date); if(day==5)printf("\n%d-%d-%d is Friday!\n",year,month,date);if(day==6)printf("\n%d-%d-%d is Saturday!\n",year,month,date); printf("\nContinue...(Y/N)");con=getch();if((con=='y')||(con=='Y'))goto r1;if((con=='n')||(con=='N'))main();}if(choice==2){r2:system("cls");printf("\n请输入年份(XXXX):");scanf("%d",&year);flag=isleap(year);if(flag==1)printf("\nThe year %d is leap year!\n",year);if(flag==0)printf("\nThe year %d is not leap year!\n",year); printf("\nContinue...(Y/N)");con=getch();if((con=='y')||(con=='Y'))goto r2;if((con=='n')||(con=='N'))main();}if(choice==3){r3:system("cls");printf("\n请输入年份(XXXX):");scanf("%d",&year);printcalendar(year);printf("Continue...(Y/N)");con=getch();if((con=='y')||(con=='Y'))goto r3;if((con=='n')||(con=='N'))main();}if(choice==4){system("cls");printf("\nDo you want to Exit?(Y/N)");con=getch();if((con=='y')||(con=='Y'))return;if((con=='n')||(con=='N'))main();}}。
... . . . 资料 . .. 学号
《面向对象程序设计》 课程设计报告
题目: 万年历系统 专业: 计算机科学与技术 班级: : 学号: 指导教师: 成绩:
2013-2014学年 第二学期 ... . .
. 资料 . .. 计算机与信息工程系 2014年6月6日... . .
. 资料 . .. 目录
1 设计容及要求 ........................................................................................... 1 1.1设计容 ................................................................................................ 1 1.2设计任务及具体要求 .......................................................................... 1 2 概要设计 ................................................................................................. 2 2.1程序设计思路 ..................................................................................... 2 2.2 总体程序框图 ................................................................................... 3 3 设计过程或程序代码 ................................................................................ 4 3.1各个模块的程序流程图及运行界面 ...................................................... 4 3.2对关键代码加以分析说明 ................................................................... 7 4 设计结果与分析 ..................................................................................... 13 4.1程序调试的结果 ............................................................................... 13 4.2程序设计分析 .................................................................................. 13 5 小 结 ..................................................................................................... 14 致 ............................................................................................................... 15 参考文献 ..................................................................................................... 16 源程序 ........................................................................................................ 17 ... . .
. 资料 . .. 1 设计容及要求 1.1设计容 万年历,实际上就是记录一定时间围(比如100年或更多)的具体阳历或阴历的日期的年历,方便有需要的人查询使用。在我设计的万年历中主要有: (1)使用图形用户界面来查询用用户所需的日期信息,符合日常软件使用规。 (2)按月份查询,实现了显示查询1901~2100年某月所有日期的阴阳历对照。 (3)并且添加了重大节日与星座等信息,界面采用日常的星期与月份结合的格式,方便查看与使用。 (4)编写万年历的课程设计,是为了使同学们更加了解高级程序设计语言
的结构,掌握基本的程序设计过程和技巧,掌握基本的分析问题和利用计算机求解问题的能力,具备初步的高级语言程序设计能力。为后续各门计算机课程的学习和毕业设计打下坚实基础。
1.2设计任务及具体要求 利用JAVA语言编写的万年历系统采用了多种JAVA语句来实现多种功能。用户可以通过本程序的applet运行界面来查找一整年某月的农历和阳历,可以查找用户所想了解的某一天具体为星期几,并且可以看到一些重大节日的具体时间。 要求:满足设计万年历系统的目的,即当用户查询年份与月份时,系统就要将这一年的某一月份的阴历与阳历全部显示出来,并且附带这一月份的重大节日。当用户随意改动年份或月份时系统自动显示与星期对应的日期。用户点击日... . . . 资料 . .. 期时系统会显示具体星座信息。
2 概要设计 2.1程序设计思路 (1)总天数的算法:首先用if语句判断定义年到输入年之间每一年是否为闰年,是闰年,该年的总天数为366,否则,为365。然后判断输入的年是否为定义年,若是,令总天数S=1,否则,用累加法计算出定义年到输入年之间的总天数,再把输入年的一月到要输出的月份之间的天数累加起来,若该月是闰年中的月份并且该月还大于二月,再使总天数加1,否则,不加,既算出从定义年一月一日到输出年的该月一日的总天数。 (2)输出月份第一天为星期几的算法:使总天数除以7取余加2得几既为星期几,若是7,则为星期日。 (3)算出输出月份第一天为星期几的算法:算出输出月份第一天为星期几后,把该日期以前的位置用空格补上,并总该日起一次输出天数直到月底,该月中的天数加上该月一日为星期几的数字再除以7得0换行,即可完整的输出该月的日历。 (4)如果年份小于1900年则程序不予判断。 (5)公历与农历的换算。公历年对应的农历数据,每年三字节,格式第一字节BIT7-4 位 表示闰月月份,值为0 为无闰月,算法:日期+年份+所过闰年数+月较正数之和除7 的余数就是星期但如果是在闰年又不到3 月份上述之和要减一天再除7星期数为0。阴历月份计算:4\6\9\11几个小月返回29天, 2月经过判断是... . . . 资料 . .. 否为闰年,选择返回28或29天,其余月份为大月,返回31天。 (6)系统时间的设置。直接从系统得到时间,设置字体,背景,显示框架等等,绘图就好了。
2.2 总体程序框图 开始 程序初始化 获取当年系统时间和年份
计算与绘制屏幕
是否输入年份或选择月份 ... . . . 资料 . .. 图1 总体程序框图 3 设计过程或程序代码 3.1各个模块的程序流程图及运行界面 (1)公历转农历流程图与运行界面 ① 流程图
提交年月变更数据结束 ... . .
. 资料 . .. 图2 公历转农历流程图 ② 运行界面 ... . .
. 资料 . .. 图3 公农历运行图
(2)时间调整程序流程图 ... . .
. 资料 . .. ①流程图
图4 时间调整流程图 ... . .
. 资料 . .. ②运行界面
图5 万年历运行图 3.2对关键代码加以分析说明 (1)本次程序设计共用了class MainCal extends JFrame implements,class CurrTimeLabel和class Lunar三个大类,用于绘制万年历,显示万年历的系统时间和完成农历与公历的换算。这些类中又运用了许多函数,来完成月份的输出,背景的设置,特定日期的标注等等。 引用的类有: import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.border.*; import java.util.*; ... . . . 资料 . .. import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Timer; (2)农历的显示:要算出某年某月某日的农历日期,只能采用逐日递推的方式,这里以1900年1月31的日期为依据,它的农历日期为一九零零年大年初一,调用日历类可以算出所求日期与1900年1月31日相差多少天,设这个差值为offset,则用offset与1900~2100的农历年天数逐个相减,当offset<=0时,可以确定当前农历年份。再用相减后得到的offset与得到的年份的农历月天数逐个相减,同理,当offset<=0时可以确定农历月份。最后,Offset加上当前月份的天数就可以得到农历日期了,至此,阳历与农历的转换算法得以实现。在排列阳历日期时,调用阳历与农历的转换算法得到相应农历日期,将对应阳历日期标签下的容设为农历日期,主要源代码如下: public Lunar(Calendar cal) { // cal.add(cal.get(Calendar.DAY_OF_MONTH),1); int monCyl; 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