Java编写 万年历.doc
27陕西师范大学远程教育学院考查课科目java程序设计姓 名霍娟学 号61861610310002专 业计算机科学与技术批 次 161层 次专升本学习中心铜川职业技术学院Java万年历一 项目概述:这个项目是一个简单的Java万年历,可以实现所有年份的公历日期的查询,并且在相应的日期做备忘录,以及可以显示当前的日期以及时间。使用的是Oracle数据库进行连接。 二 具体功能介绍:(1)万年历查询:点击图形界面中的上年、下年键用来调整要查询的年份,或者可以直接在上年下年按钮直接的文本框中输入年份(负数表示公元前),以回车结束;点击上月或者下月来调整要查询的月份,然后可以看到这个月的每一天所对应的星期。(2)Clock功能:在万年历下面显示当前的年月日时分秒,相当于一个时钟的功能。(3)记事本功能:可以任选某年某月的某一天,单击,在右侧会出现这一天的备忘录,如果存在,则显示某年某月某日有日志记载,是否想看,否则,则在文本框中显示无记录;然后可以编辑这一天的备忘录,编辑好了之后,点击保存日志,弹出对话框某年某月某日保存日志吗,点击保存,则日志被保存,反之未被保存;若想删除某日的日志,则单击这一天,然后点击右侧的删除日志,显示删除某年某月某日的日志吗,点击是,则日志被删除。从文件中读取备忘录的内容,用数据库进行存储和删除操作。三 设计与实现(需要附全部代码,GUI自动生成代码除外):1 类的设计(继承、多态、数据结构):核心类是Month,Year,NotePad,Clock,DBAccess,CalendarPad.(其中继承用粗体,接口用粗斜体,数据结构是哈希表,用粗下划线,多态用斜体+点点短线式下划线)2 Java IO (文件访问):用的是粗体+浪线3 JDBC (数据库访问):使用Oracle数据库连接,是直连(双下划线)数据库是:create table mynotes( mydate varchar2(50) primary key, note varchar2(100) not null);4 Socket + Multi-Thread:斜体(定义在Clock中的Thread t) 5 GUI (用户界面):点下划线来表示GUI用户界面6 其他功能:(无)以下是全部代码(共六个.Java文件)/对月份的选择package javaapplication13;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Month extends Box implements ActionListener/ActionListener接口 int month; JTextField showMonth=null;JButton RMonth,NMonth; CalendarPad cal; public Month(CalendarPad c) super(BoxLayout.X_AXIS); this.cal=c; showMonth=new JTextField(2); month=c.getMonth(); showMonth.setEditable(false); showMonth.setForeground(Color.blue);showMonth.setFont(new Font("TimesRomn",Font.BOLD,16);NMonth=new JButton("下月");RMonth=new JButton("上月"); add(RMonth); add(showMonth); add(NMonth); RMonth.addActionListener(this); NMonth.addActionListener(this); showMonth.setText(""+month); public void setMonth(int month) if(month<=12&&month>=1) this.month=month; else this.month=1; showMonth.setText(""+month); public int getMonth() return month; public void actionPerformed(ActionEvent e) if(e.getSource()=RMonth) if(month>=2) month=month-1; cal.setMonth(month); cal.setCal(cal.getYear(),month); else if(month=1) month=12; cal.setMonth(month); cal.setCal(cal.getYear(),month); showMonth.setText(""+month); else if(e.getSource()=NMonth) if(month<12) month=month+1; cal.setMonth(month); cal.setCal(cal.getYear(),month); else if(month=12) month=1; cal.setMonth(month); cal.setCal(cal.getYear(),month); showMonth.setText(""+month); /对年分的选择package javaapplication13;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Year extends Box implements ActionListener/ActionListener接口 int year; JTextField showYear=null;JButton NYear,RYear; CalendarPad cal; public Year(CalendarPad c) super(BoxLayout.X_AXIS); showYear=new JTextField(4);showYear.setForeground(Color.blue);showYear.setFont(new Font("TimesRomn",Font.BOLD,14); this.cal=c; year=cal.getYear(); NYear=new JButton("下年");RYear=new JButton("上年"); add(RYear); add(showYear); add(NYear); showYear.addActionListener(this); RYear.addActionListener(this); NYear.addActionListener(this); public void setYear(int year) this.year=year; showYear.setText(""+year); public int getYear() return year; public void actionPerformed(ActionEvent e) if(e.getSource()=RYear) year=year-1; showYear.setText(""+year); cal.setYear(year); cal.setCal(year,cal.getMonth(); else if(e.getSource()=NYear) year=year+1; showYear.setText(""+year); cal.setYear(year); cal.setCal(year,cal.getMonth(); else if(e.getSource()=showYear) try year=Integer.parseInt(showYear.getText(); showYear.setText(""+year); cal.setYear(year); cal.setCal(year,cal.getMonth(); catch(NumberFormatException ee) showYear.setText(""+year); cal.setYear(year); cal.setCal(year,cal.getMonth(); /对备忘录的操作package javaapplication13;import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import javax.swing.event.*;import java.io.*;public class NotePad extends JPanel implements ActionListener JTextArea text;JButton save_log,del_log; Hashtable table; JLabel mes_label; int year,month,day; File file; CalendarPad calendar; public NotePad(CalendarPad calendar)/构造函数 this.calendar=calendar; Calendar now = Calendar.getInstance(); int hour=now.get(Calendar.HOUR); int minute=now.get(Calendar.MINUTE); year=calendar.getYear(); month=calendar.getMonth(); day=calendar.getDay(); table=calendar.getHashtable(); file=calendar.getFile(); mes_label=new JLabel(""+year+"年"+month+"月"+day+"日"+" "+hour+":"+minute,JLabel.CENTER);mes_label.setFont(new Font("TimesRoman",Font.BOLD,16);mes_label.setForeground(Color.MAGENTA);text=new JTextArea(10,8);save_log=new JButton("保存日志") ;del_log=new JButton("删除日志") ; save_log.addActionListener(this); del_log.addActionListener(this); setLayout(new BorderLayout(); JPanel pSouth=new JPanel();add(mes_label,BorderLayout.NORTH);pSouth.add(save_log);pSouth.add(del_log);add(pSouth,BorderLayout.SOUTH);add(new JScrollPane(text),BorderLayout.CENTER); public void actionPerformed(ActionEvent e) if(e.getSource()=save_log) saveLog(year,month,day); else if(e.getSource()=del_log) delLog(year,month,day); public void setYear(int year) this.year=year; public int getYear() return year; public void setMonth(int month) this.month=month; public int getMonth() return month; public void setDay(int day) this.day=day; public int getDay() return day; public void setMesLabel(int year,int month,int day) mes_label.setText(""+year+"年"+month+"月"+day+"日"); public void setText(String s) text.setText(s); public void getLog(int year,int month,int day) String key=""+year+""+month+""+day; try FileInputStream inOne=new FileInputStream(file);ObjectInputStream inTwo=new ObjectInputStream(inOne); table=(Hashtable)inTwo.readObject(); inOne.close(); inTwo.close(); catch(Exception ee) if(table.containsKey(key) String m=""+year+"年"+month+"月"+day+"这一天有日志记载,想看吗?" int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if(ok=JOptionPane.YES_OPTION) text.setText(String)table.get(key); else text.setText(""); else text.setText("无记录"); public void saveLog(int year,int month,int day) String 日志内容=text.getText(); String key=""+year+""+month+""+day; String m=""+year+"年"+month+"月"+day+"保存日志吗?" int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if(ok=JOptionPane.YES_OPTION) try FileInputStream inOne=new FileInputStream(file); ObjectInputStream inTwo=new ObjectInputStream(inOne); table=(Hashtable)inTwo.readObject(); inOne.close(); inTwo.close(); table.put(key,日志内容); FileOutputStream out=new FileOutputStream(file);ObjectOutputStream objectOut=new ObjectOutputStream(out); objectOut.writeObject(table); objectOut.close(); out.close(); catch(Exception ee) /向数据库中添加数据。先查询数据库,判断是否已有该日记录,若有则更新,否则插入 DBAccess db = new DBAccess(); if(db.createConn() String testSql = "select * from mynotes where mydate='"+key+"'" db.query(testSql); if (db.next() String updatesql = "update mynotes set note='" + 日志内容 + "' where mydate='" + key +"'" try updatesql = new String(updatesql.getBytes("ISO8859-1"), "UTF-8"); catch (Exception e) e.printStackTrace(); db.closeRs(); db.closeStm(); db.closeConn(); return; db.closeRs(); / 组合新增SQL String sql = "insert into mynotes (mydate, note) " sql += " values('" + key + "','"+日志内容 + "')" / 转换参数编码 try sql = new String(sql.getBytes("ISO8859-1"), "UTF-8"); catch (Exception e) e.printStackTrace(); /*/ 执行插入 db.update(sql); db.closeStm(); db.closeConn(); public void delLog(int year,int month,int day) String key=""+year+""+month+""+day; if(table.containsKey(key) String m="删除"+year+"年"+month+"月"+day+"日的日志吗?" int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if(ok=JOptionPane.YES_OPTION) try FileInputStream inOne=new FileInputStream(file); ObjectInputStream inTwo=new ObjectInputStream(inOne); table=(Hashtable)inTwo.readObject(); inOne.close(); inTwo.close(); table.remove(key); FileOutputStream out=new FileOutputStream(file);ObjectOutputStream objectOut=new ObjectOutputStream(out); objectOut.writeObject(table); objectOut.close(); out.close(); text.setText(null); catch(Exception ee) else String m=""+year+"年"+month+"月"+day+"无日志记录" JOptionPane.showMessageDialog(this,m,"提示",JOptionPane.WARNING_MESSAGE); /删除数据库记录 DBAccess db = new DBAccess(); if(db.createConn() / 根据name组成删除SQL,执行删除 String sql = "delete from mynotes where mydate='" +key+"'" db.update(sql); db.closeStm(); db.closeConn(); /提取当前的年月日时分秒,时钟package javaapplication13;import java.awt.Canvas;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.text.SimpleDateFormat;import java.util.Calendar;class Clock extends Canvas implements Runnable private static final long serialVersionUID = 3660124045489727166L; CalendarPad mf;Thread t;/Multi-Thread(斜体)String time; public Clock(CalendarPad mf) this.mf=mf; setSize(280,40); setBackground(Color.white); t=new Thread(this); /实例化线程 t.start(); /调用线程 public void run() while(true) try Thread.sleep(1000); /休眠1秒钟 catch(InterruptedException e) System.out.println("异常"); this.repaint(100); /重画屏幕 /*public abstract void drawString(AttributedCharacterIterator iterator, int x, int y)依据 TextAttribute 类的规范应用指定迭代器的属性,呈现迭代器的文本。最左侧字符的基线位于此图形上下文坐标系的 (x, y) 位置处。 */对paint函数进行重写(多态) public void paint(Graphics g) Font f=new Font("宋体",Font.BOLD,16); SimpleDateFormat SDF=new SimpleDateFormat(" yyyy'年'MM'月'dd'日'HH:mm:ss");/格式化时间显示类型 Calendar now=Calendar.getInstance(); time=SDF.format(now.getTime(); /得到当前日期和时间 g.setFont(f); g.setColor(Color.RED); g.drawString(time,25,25); /用Oracle的方式连接数据库package javaapplication13;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/JDBC 数据库连接,直连,利用Oracle(双下划线)public class DBAccess public static String drv = "oracle.jdbc.driver.OracleDriver"public static String url = "jdbc:oracle:thin:localhost:1521:orcl"public static String usr = "scott"public static String pwd = "helloneal"private Connection conn = null;private Statement stm = null;private ResultSet rs = null;public boolean createConn() boolean b = false;try Class.forName(drv).newInstance();conn = DriverManager.getConnection(url, usr, pwd);b = true; catch (SQLException e) catch (ClassNotFoundException e) catch (InstantiationException e) catch (IllegalAccessException e) return b;public boolean update(String sql) boolean b = false;try stm = conn.createStatement();stm.execute(sql);b = true; catch (Exception e) System.out.println(e.toString();return b;public void query(String sql) try stm = conn.createStatement();rs = stm.executeQuery(sql); catch (Exception e) public boolean next() boolean b = false;try if(rs.next()b = true; catch (Exception e) return b;public String get