java课程设计万年历程序设计报告

上传人:沈*** 文档编号:55783899 上传时间:2022-02-18 格式:DOC 页数:24 大小:245.50KB
返回 下载 相关 举报
java课程设计万年历程序设计报告_第1页
第1页 / 共24页
java课程设计万年历程序设计报告_第2页
第2页 / 共24页
java课程设计万年历程序设计报告_第3页
第3页 / 共24页
点击查看更多>>
资源描述
信息科学与工程学院山东科技大学信息学院JAVA程序设计 学 院 :信息科学与工程学院 班 级 :计算机 11-3 学 号 :201101050230 姓 名 : 赵乐 信息科学与工程学院目 录第23页目 录1前言11需求分析22.概要设计33.各模块的功能及程序说明54测试分析135源程序清单136课程设计体会22前言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.本程序能够实现日期与星期的查询。1.2功能设计本程序要构建的万年历程序,其功能有以下几个方面:(1)通过网页形式运行,实现图形界面。(2)能以月历形式显示日期与星期。 (3)支持用户自己输入年份,并提供月份的下拉形式菜单来选择月份。 (4)通过点击“更新”来刷新日历。2.概要设计2.1程序设计思路1. 总天数的算法:首先用if语句判断定义年到输入年之间每一年是否为闰年,是闰年,该年的总天数为366,否则,为365。然后判断输入的年是否为定义年,若是,令总天数S=1,否则,用累加法计算出定义年到输入年之间的总天数,再把输入年的一月到要输出的月份之间的天数累加起来,若该月是闰年中的月份并且该月还大于二月,再使总天数加1,否则,不加,既算出从定义年一月一日到输出年的该月一日的总天数。2. 输出月份第一天为星期几的算法:使总天数除以7取余加2得几既为星期几,若是7,则为星期日。3. 算出输出月份第一天为星期几的算法:算出输出月份第一天为星期几后,把该日期以前的位置用空格补上,并总该日起一次输出天数直到月底,该月中的天数加上该月一日为星期几的数字再除以7得0换行,即可完整的输出该月的日历。4. 如果年份小于1582年则程序不予判断。2.2程序运行界面图2-1 程序运行界面2.3流程图开始初始化程序,获取当前日期调取当前日期下日志文件绘制界面判断日期是否更新结束3.各模块的功能及程序说明3.1.初始化组件import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;/import java.sql.Date;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.table.DefaultTableModel;3.2.初始化数据private Calendar cld = Calendar.getInstance();private String astr = 星期一, 星期二, 星期三, 星期四, 星期五, 星期六, 星期日;private DefaultTableModel dtm = new DefaultTableModel(null,astr);private JTable table = new JTable(dtm); /装日期的表格private JScrollPane sp = new JScrollPane(table);private JButton bLastYear = new JButton(上一年);private JButton bNextYear = new JButton(下一年);private JButbNextMonth = new JButton(下月);private JTextField jtfYear = new JTextField(5);/jtfYear年份显示和输入文本框private JTextField jtfMonth = new JTextField(2);/jtfMonth月份显示文本框private JPanel p1 = new JPanel(); /装入控制日期按钮的模块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-JTextAreaprivate JScrollPane jsp = new JScrollPane(jta);private JLabel l = new JLabel(年份文本框中可直接键入要查找的年份,以提高查询效率);private JLabel lt = new JLabel();private JLabel ld = new JLabel();private int lastTime;3.3.绘制程序界面 public wannianli() super(万年历); /框架命名 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/窗口关闭函数 this.getContentPane().setLayout(new BorderLayout(10, 0); jta.setLineWrap(true); 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);/表格的高度 table.addMouseListener(this); /鼠标监听器 jtfYear.addActionListener(this);/可输入年份的文本框 /为各个按钮添加监听函数 bAdd.addActionListener(this); bDel.addActionListener(this); bLastYear.addActionListener(this); bNextYear.addActionListener(this); bLastMonth.addActionListener(this); bNextMonth.addActionListener(this); /将按钮添加到Jpanel上 p1.add(bLastYear); p1.add(jtfYear);/年份输入文本框 p1.add(bNextYear); p1.add(bLastMonth); p1.add(jtfMonth); p1.add(bNextMonth); p2.add(bAdd); p2.add(bDel); p3.add(jsp, BorderLayout.CENTER); p3.add(p2, BorderLayout.SOUTH); p3.add(ld, BorderLayout.NORTH); p4.add(l); p4.add(lt); p5.add(p4, BorderLayout.SOUTH); p5.add(sp, BorderLayout.CENTER); p5.add(p1, BorderLayout.NORTH); this.getContentPane().add(p5, BorderLayout.CENTER); this.getContentPane().add(p3, BorderLayout.EAST); String strDate = DateFormat.getDateInstance().format(new Date().split(-);/获得日期 cld.set(Integer.parseInt(strDate0), Integer.parseInt(strDate1)-1, 0); showCalendar(Integer.parseInt(strDate0), Integer.parseInt(strDate1), cld); jtfMonth.setEditable(false);/设置月份的文本框为不可编辑 jtfYear.setText(strDate0); jtfMonth.setText(strDate1); this.showTextArea(strDate2); ld.setFont(new Font(新宋体,Font.BOLD,24); new Timer(lt).start(); this.setBounds(200,200,600,320); this.setResizable(false); this.setVisible(true);3.4 触发器 public void actionPerformed(ActionEvent e) if(e.getSource() = jtfYear | e.getSource() = bLastYear | e.getSource() = bNextYear | e.getSource() = bLastMonth | e.getSource() = bNextMonth) int m, y; try/控制输入的年份正确,异常控制 if (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() = bNextYear)jtfYear.setText(String.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 jtfMonth.setText(String.valueOf(+m); cld.set(y, m-1, 0); showCalendar(y, m, cld); if(e.getSource() = bAdd) int r = table.getSelectedRow(); int c = table.getSelectedColumn(); if(!ld.getText().equals(没有选择日期) try File file = new File(ld.getText() + .txt); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file); bw.write(jta.getText(); bw.close(); catch (FileNotFoundException ex) ex.printStackTrace(); catch (IOException ex) ex.printStackTrace(); if(e.getSource() = bDel) int r = table.getSelectedRow(); int c = table.getSelectedColumn(); File filedel = new File(ld.getText() + .txt); if(filedel.exists() if(filedel.delete() jta.setText(日志删除成功); else jta.setText(日志删除失败); else jta.setText(没有找到日志文件); public void mouseClicked(MouseEvent e) jta.setText(null); int r = table.getSelectedRow(); int c = table.getSelectedColumn(); if (table.getValueAt(r,c) = null) ld.setText(没有选择日期); else this.showTextArea(table.getValueAt(r,c); public void mousePressed(MouseEvent e) public void mouseReleased(MouseEvent e) public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) private void showTextArea(Object selected) /将所选日期显示出来,能不能弄成农历显示 ld.setText(jtfYear.getText()+年+jtfMonth.getText()+月+selected+日); File filein = new File(ld.getText() + .txt); if(filein.exists() try BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filein); String strRead = br.readLine(); jta.setText(null); while(strRead != null) jta.append(strRead); strRead = br.readLine(); br.close(); catch (FileNotFoundException ex) ex.printStackTrace(); catch (IOException ex) ex.printStackTrace(); 3.5时间获取class Timer extends Thread/显示系统时间 private JLabel lt; private SimpleDateFormat fy = new SimpleDateFormat(yyyy.MM.dd G at HH:mm:ss z); private SimpleDateFormat fn = new SimpleDateFormat(yyyy.MM.dd G at HH mm ss z); private boolean b = true; public Timer(JLabel lt) this.lt = lt; public void run() while (true) try if (b) lt.setText(fy.format(new Date(); else lt.setText(fn.format(new Date(); b = !b; this.sleep(500); catch (InterruptedException ex) ex.printStackTrace(); 4测试分析 图4-1 图4-2 图4-3 图4-4 对程序进行正常使用,测试各项功能的实现过程是否存在bug,经测试,软件正常使用,无任何Bug。5源程序清单import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;/import java.sql.Date;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.table.DefaultTableModel;public class wannianli extends JFrame implements ActionListener, MouseListener private Calendar cld = Calendar.getInstance(); private String astr = 星期一, 星期二, 星期三, 星期四, 星期五, 星期六, 星期日; private DefaultTableModel dtm = new DefaultTableModel(null,astr); 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 jtfYear = new JTextField(5);/jtfYear年份显示和输入文本框 private JTextField jtfMonth = new JTextField(2);/jtfMonth月份显示文本框 private JPanel p1 = new JPanel(); /装入控制日期按钮的模块 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 wannianli() super(万年历); /框架命名 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/窗口关闭函数 this.getContentPane().setLayout(new BorderLayout(10, 0); jta.setLineWrap(true); 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);/表格的高度 table.addMouseListener(this); /鼠标监听器 jtfYear.addActionListener(this);/可输入年份的文本框 /为各个按钮添加监听函数 bAdd.addActionListener(this); bDel.addActionListener(this); bLastYear.addActionListener(this); bNextYear.addActionListener(this); bLastMonth.addActionListener(this); bNextMonth.addActionListener(this); /将按钮添加到Jpanel上 p1.add(bLastYear); p1.add(jtfYear);/年份输入文本框 p1.add(bNextYear); p1.add(bLastMonth); p1.add(jtfMonth); p1.add(bNextMonth); p2.add(bAdd); p2.add(bDel); p3.add(jsp, BorderLayout.CENTER); p3.add(p2, BorderLayout.SOUTH); p3.add(ld, BorderLayout.NORTH); p4.add(l); p4.add(lt); p5.add(p4, BorderLayout.SOUTH); p5.add(sp, BorderLayout.CENTER); p5.add(p1, BorderLayout.NORTH); this.getContentPane().add(p5, BorderLayout.CENTER); this.getContentPane().add(p3, BorderLayout.EAST); String strDate = DateFormat.getDateInstance().format(new Date().split(-);/获得日期 cld.set(Integer.parseInt(strDate0), Integer.parseInt(strDate1)-1, 0); showCalendar(Integer.parseInt(strDate0), Integer.parseInt(strDate1), cld); jtfMonth.setEditable(false);/设置月份的文本框为不可编辑 jtfYear.setText(strDate0); jtfMonth.setText(strDate1); this.showTextArea(strDate2); 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 Object7; lastTime = 0; for (int i = cld.get(Calendar.DAY_OF_WEEK)-1; i = Days; i+) aii%7 = String.valueOf(i-(cld.get(Calendar.DAY_OF_WEEK)-2); if (i%7 = 6) dtm.addRow(ai); ai = new Object7; 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() = jtfYear | e.getSource() = bLastYear | e.getSource() = bNextYear | e.getSource() = bLastMonth | e.getSource() = bNextMonth) int m, y; try/控制输入的年份正确,异常控制 if (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() = bNextYear)jtfYear.setText(String.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.
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 管理文书 > 施工组织


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!