JAVA《万年历系统》课程设计报告附源码
JAVA万年历设计报告

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课程设计-万年历doc资料

河北科技大学课程设计报告学生姓名:学号:专业班级:课程名称: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的思想运用到实际系统的详细设计之中。
java_java__Java_万年历源代码参考

jtfYear.setText(strDate[0]); jtfMonth.setText(strDate[1]); this.showTextArea(strDate[2]); ld.setFont(new Font(" ",Font.BOLD,24)); new Timer(lt).start(); this.setBounds(200,200,600,320); this.setResizable(false); this.setVisible(true); } 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 || { return 29; } else { return 28; } } year%400 == 0)// public void actionPerformed(ActionEvent e) { if(e.getSource() == jtfYear || e.getSource() == bLastYear || e.getSource() == bNextYear || e.getSource() == bLastMonth || e.getSource() == bNextMonth) { int m, y; { if try// (jtfYear.getText().length() != 4) { throw new NumberFormatException(); } y = Integer.parseInt(jtfYear.getText()); m = Integer.parseInt(jtfMonth.getText()); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(this, " 4 0-9 ", " ", JOptionPane.ERROR_MESSAGE); return; } ld.setText(" "); for (int i = 0; i < lastTime+1; i++){ dtm.removeRow(0);} if(e.getSource() == bLastYear){ jtfYear.setText(String.valueOf(--y)); } if(e.getSource() == bNexttring.valueOf(++y)); } if(e.getSource() == bLastMonth) { if(m == 1) { jtfYear.setText(String.valueOf(--y)); m = 12; jtfMonth.setText(String.valueOf(m)); } else { jtfMonth.setText(String.valueOf(--m)); } } if(e.getSource() == bNextMonth) { if(m == 12) { jtfYear.setText(String.valueOf(++y)); m = 1; jtfMonth.setText(String.valueOf(m)); } else
java程序设计报告2万年历

程序设计报告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程序效果图程序打开时的效果,左边是记事本框,右边是万年历框。
java课程设计报告之万历程序

目录前言 (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换行,即可完整的输出该月的日历。
万年历Java课程设计

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版万年历设计报告及流程图

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图形用户界面的有关知识有了更深入了解,理,完成了简单的日历功能,输入查询的年份与月份,单击“选择”按扭,则可得到相应的日历,简单实用。
java课程设计 万年历

}
3.4代码实现
package ss2;
import java.applet.Applet;
import java.awt.*;
import java.util.*;
public class WanNianLi extends Applet{
static final int TOP = 70; //顶端距离
int searchMonth,searchYear; //查询的年份及月份
}
(4)绘制程序的界面
public void init(){
setBackground(Color.green); //设置背景颜色
searchMonth = calendar.get(Calendar.MONTH); //得到系统年份,当前的年份
add(monthChoice);
add(btUpdate);
int componentCount=this.getComponentCount(); //得到Applet中的组件数量
for (int i=0;i<componentCount;i++){
getComponent(i).setFont(smallFont); //设置所有组件的显示字体
monthChoice.addItem(months[i]); //增加下拉框选项
}
monthChoice.select(searchMonth); //设置下拉框当前选择项
add(monthChoice);
add(btUpdate);
int componentCount=this.getComponentCount(); //得到Applet中的组件数量
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
... . . . 资料 . .. 学号
《面向对象程序设计》 课程设计报告
题目: 万年历系统 专业: 计算机科学与技术 班级: : 学号: 指导教师: 成绩:
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