Java编写 万年历参考模板_第1页
Java编写 万年历参考模板_第2页
Java编写 万年历参考模板_第3页
Java编写 万年历参考模板_第4页
Java编写 万年历参考模板_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、陕西师范大学远程教育学院考查课科目java程序设计姓 名霍娟学 号61861610310002专 业计算机科学与技术批 次 161层 次专升本学习中心铜川职业技术学院1 / 22Java万年历一 项目概述:这个项目是一个简单的Java万年历,可以实现所有年份的公历日期的查询,并且在相应的日期做备忘录,以及可以显示当前的日期以及时间。使用的是Oracle数据库进行连接。 二 具体功能介绍:(1)万年历查询:点击图形界面中的上年、下年键用来调整要查询的年份,或者可以直接在上年下年按钮直接的文本框中输入年份(负数表示公元前),以回车结束;点击上月或者下月来调整要查询的月份,然后可以看到这个月的每一天

2、所对应的星期。(2)Clock功能:在万年历下面显示当前的年月日时分秒,相当于一个时钟的功能。(3)记事本功能:可以任选某年某月的某一天,单击,在右侧会出现这一天的备忘录,如果存在,则显示某年某月某日有日志记载,是否想看,否则,则在文本框中显示无记录;然后可以编辑这一天的备忘录,编辑好了之后,点击保存日志,弹出对话框某年某月某日保存日志吗,点击保存,则日志被保存,反之未被保存;若想删除某日的日志,则单击这一天,然后点击右侧的删除日志,显示删除某年某月某日的日志吗,点击是,则日志被删除。从文件中读取备忘录的内容,用数据库进行存储和删除操作。三 设计与实现(需要附全部代码,GUI自动生成代码除外)

3、: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:斜体(定义在Cl

4、ock中的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;

5、 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("下月");RMon

6、th=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.setTex

7、t(""+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); showMon

8、th.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;

9、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.set

10、Foreground(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.add

11、ActionListener(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.getMo

12、nth(); 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(

13、); 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 Note

14、Pad 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(Cale

15、ndar.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+":&qu

16、ot;+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);

17、 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

18、,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

19、; 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+""+d

20、ay; 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+"这一天有日志

21、记载,想看吗?" 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 m

22、onth,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_M

23、ESSAGE); 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

24、 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+"'&quo

25、t; 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(); d

26、b.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"), "

27、;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+

28、"月"+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=(Ha

29、shtable)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=&

30、quot;"+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='" +k

31、ey+"'" 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 Ru

32、nnable 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.s

33、leep(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

34、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.d

35、rawString(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.d

36、river.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 create

37、Conn() 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 = f

38、alse;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 = tru

39、e; catch (Exception e) return b;public String getValue(String field) String value = ""try if(rs!=null)value = rs.getString(field); catch (Exception e) e.printStackTrace();if (value = null) value = ""return value;public void closeConn() try if (conn != null)conn.close(); catch (SQ

40、LException e) public void closeStm() try if (stm != null)stm.close(); catch (SQLException e) public void closeRs() try if (rs != null)rs.close(); catch (SQLException e) public Connection getConn() return conn;public void setConn(Connection conn) this.conn = conn;public ResultSet getRs() return rs;pu

41、blic void setRs(ResultSet rs) this.rs = rs;public Statement getStm() return stm;public void setStm(Statement stm) this.stm = stm;/Calendar日历记事本package javaapplication13;import java.util.Calendar;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;/文件访问(Java IO)粗体+波浪线impor

42、t java.util.Hashtable;/哈希表 粗下划线public class CalendarPad extends JFrame implements MouseListener int year,month,day; Hashtable hashtable; File file; JTextField showDay; JLabel title; JLabel label = new JLabel49; JLabel y_label = new JLabel("年份"); JLabel m_label = new JLabel("月份");

43、 Calendar cal; Calendar now = Calendar.getInstance(); / 实例化Calendar int week; NotePad notepad=null; Month ChangeMonth; Year ChangeYear; String w="星期日","星期一","星期二","星期三","星期四","星期五","星期六" JPanel leftPanel,rightPanel; public Calenda

44、rPad(int year,int month,int day)/构造函数 leftPanel=new JPanel(); JPanel leftCenter=new JPanel(); JPanel leftNorth=new JPanel(); leftCenter.setLayout(new GridLayout(7,7); rightPanel=new JPanel(); this.year=year;this.month=month;this.day=day; ChangeYear=new Year(this); ChangeYear.setYear(year); ChangeMon

45、th=new Month(this); ChangeMonth.setMonth(month); title=new JLabel7; showDay=new JTextField42; for(int j=0;j<7;j+) titlej=new JLabel(); titlej.setText(wj); titlej.setBorder(BorderFactory.createRaisedBevelBorder(); leftCenter.add(titlej); title0.setForeground(Color.red); title6.setForeground(Color.blue); for(int i=0;i<42;i+) showDayi=new JTextField(); showDayi.addMouseListener(this); showDayi.setEditable(false); leftCente

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论