Java編寫(xiě)-萬(wàn)年歷_第1頁(yè)
Java編寫(xiě)-萬(wàn)年歷_第2頁(yè)
Java編寫(xiě)-萬(wàn)年歷_第3頁(yè)
Java編寫(xiě)-萬(wàn)年歷_第4頁(yè)
Java編寫(xiě)-萬(wàn)年歷_第5頁(yè)
已閱讀5頁(yè),還剩17頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、22陜西師范大學(xué)遠(yuǎn)程教育學(xué)院考查課科目java程序設(shè)計(jì)姓 名霍娟學(xué) 號(hào)61861610310002專 業(yè)計(jì)算機(jī)科學(xué)與技術(shù)批 次 161層 次專升本學(xué)習(xí)中心銅川職業(yè)技術(shù)學(xué)院Java萬(wàn)年歷一 項(xiàng)目概述:這個(gè)項(xiàng)目是一個(gè)簡(jiǎn)單的Java萬(wàn)年歷,可以實(shí)現(xiàn)所有年份的公歷日期的查詢,并且在相應(yīng)的日期做備忘錄,以及可以顯示當(dāng)前的日期以及時(shí)間。使用的是Oracle數(shù)據(jù)庫(kù)進(jìn)行連接。 二 具體功能介紹:(1)萬(wàn)年歷查詢:點(diǎn)擊圖形界面中的上年、下年鍵用來(lái)調(diào)整要查詢的年份,或者可以直接在上年下年按鈕直接的文本框中輸入年份(負(fù)數(shù)表示公元前),以回車結(jié)束;點(diǎn)擊上月或者下月來(lái)調(diào)整要查詢的月份,然后可以看到這個(gè)月的每一天所對(duì)應(yīng)的

2、星期。(2)Clock功能:在萬(wàn)年歷下面顯示當(dāng)前的年月日時(shí)分秒,相當(dāng)于一個(gè)時(shí)鐘的功能。(3)記事本功能:可以任選某年某月的某一天,單擊,在右側(cè)會(huì)出現(xiàn)這一天的備忘錄,如果存在,則顯示某年某月某日有日志記載,是否想看,否則,則在文本框中顯示無(wú)記錄;然后可以編輯這一天的備忘錄,編輯好了之后,點(diǎn)擊保存日志,彈出對(duì)話框某年某月某日保存日志嗎,點(diǎn)擊保存,則日志被保存,反之未被保存;若想刪除某日的日志,則單擊這一天,然后點(diǎn)擊右側(cè)的刪除日志,顯示刪除某年某月某日的日志嗎,點(diǎn)擊是,則日志被刪除。從文件中讀取備忘錄的內(nèi)容,用數(shù)據(jù)庫(kù)進(jìn)行存儲(chǔ)和刪除操作。三 設(shè)計(jì)與實(shí)現(xiàn)(需要附全部代碼,GUI自動(dòng)生成代碼除外):1 類

3、的設(shè)計(jì)(繼承、多態(tài)、數(shù)據(jù)結(jié)構(gòu)):核心類是Month,Year,NotePad,Clock,DBAccess,CalendarPad.(其中繼承用粗體,接口用粗斜體,數(shù)據(jù)結(jié)構(gòu)是哈希表,用粗下劃線,多態(tài)用斜體+點(diǎn)點(diǎn)短線式下劃線)2 Java IO (文件訪問(wèn)):用的是粗體+浪線3 JDBC (數(shù)據(jù)庫(kù)訪問(wèn)):使用Oracle數(shù)據(jù)庫(kù)連接,是直連(雙下劃線)數(shù)據(jù)庫(kù)是:create table mynotes( mydate varchar2(50) primary key, note varchar2(100) not null);4 Socket + Multi-Thread:斜體(定義在Clock中

4、的Thread t) 5 GUI (用戶界面):點(diǎn)下劃線來(lái)表示GUI用戶界面6 其他功能:(無(wú))以下是全部代碼(共六個(gè).Java文件)/對(duì)月份的選擇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; Cal

5、endarPad 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=n

6、ew 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(&q

7、uot;"+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.s

8、etText(""+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); /對(duì)年分的選擇package javaapplication13;impo

9、rt 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.setFore

10、ground(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.addActi

11、onListener(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(

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

13、atch(NumberFormatException ee) showYear.setText(""+year); cal.setYear(year); cal.setCal(year,cal.getMonth(); /對(duì)備忘錄的操作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

14、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)/構(gòu)造函數(shù) this.calendar=calendar; Calendar now = Calendar.getInstance(); int hour=now.get(Calendar

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

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

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

18、); 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; pu

19、blic 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;

20、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,"詢問(wèn)",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if(ok=JOptionPane.YES_OPTION) text.setText(String)table.get(key); else text.setText(""); else text.setText("無(wú)記錄"); public void saveLog(int year,int month

22、,int day) String 日志內(nèi)容=text.getText(); String key=""+year+""+month+""+day; String m=""+year+"年"+month+"月"+day+"保存日志嗎?" int ok=JOptionPane.showConfirmDialog(this,m,"詢問(wèn)",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSA

23、GE); 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,日志內(nèi)容); FileOutputStream out=new FileOutputStream(file);ObjectOutputStream obj

24、ectOut=new ObjectOutputStream(out); objectOut.writeObject(table); objectOut.close(); out.close(); catch(Exception ee) /向數(shù)據(jù)庫(kù)中添加數(shù)據(jù)。先查詢數(shù)據(jù)庫(kù),判斷是否已有該日記錄,若有則更新,否則插入 DBAccess db = new DBAccess(); if(db.createConn() String testSql = "select * from mynotes where mydate='"+key+"'" d

25、b.query(testSql); if (db.next() String updatesql = "update mynotes set note='" + 日志內(nèi)容 + "' where mydate='" + key +"'" try updatesql = new String(updatesql.getBytes("ISO8859-1"), "UTF-8"); catch (Exception e) e.printStackTrace(); db.cl

26、oseRs(); db.closeStm(); db.closeConn(); return; db.closeRs(); / 組合新增SQL String sql = "insert into mynotes (mydate, note) " sql += " values('" + key + "','"+日志內(nèi)容 + "')" / 轉(zhuǎn)換參數(shù)編碼 try sql = new String(sql.getBytes("ISO8859-1"), "UTF

27、-8"); catch (Exception e) e.printStackTrace(); /*/ 執(zhí)行插入 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+&quo

28、t;月"+day+"日的日志嗎?" int ok=JOptionPane.showConfirmDialog(this,m,"詢問(wèn)",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if(ok=JOptionPane.YES_OPTION) try FileInputStream inOne=new FileInputStream(file); ObjectInputStream inTwo=new ObjectInputStream(inOne); table=(Hashta

29、ble)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、;"+year+"年"+month+"月"+day+"無(wú)日志記錄" JOptionPane.showMessageDialog(this,m,"提示",JOptionPane.WARNING_MESSAGE); /刪除數(shù)據(jù)庫(kù)記錄 DBAccess db = new DBAccess(); if(db.createConn() / 根據(jù)name組成刪除SQL,執(zhí)行刪除 String sql = "delete from mynotes where mydate='" +key+&

31、quot;'" db.update(sql); db.closeStm(); db.closeConn(); /提取當(dāng)前的年月日時(shí)分秒,時(shí)鐘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 Runnab

32、le 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); /實(shí)例化線程 t.start(); /調(diào)用線程 public void run() while(true) try Thread.sleep

33、(1000); /休眠1秒鐘 catch(InterruptedException e) System.out.println("異常"); this.repaint(100); /重畫(huà)屏幕 /*public abstract void drawString(AttributedCharacterIterator iterator, int x, int y)依據(jù) TextAttribute 類的規(guī)范應(yīng)用指定迭代器的屬性,呈現(xiàn)迭代器的文本。最左側(cè)字符的基線位于此圖形上下文坐標(biāo)系的 (x, y) 位置處。 */對(duì)paint函數(shù)進(jìn)行重寫(xiě)(多態(tài)) public void pain

34、t(Graphics g) Font f=new Font("宋體",Font.BOLD,16); SimpleDateFormat SDF=new SimpleDateFormat(" yyyy'年'MM'月'dd'日'HH:mm:ss");/格式化時(shí)間顯示類型 Calendar now=Calendar.getInstance(); time=SDF.format(now.getTime(); /得到當(dāng)前日期和時(shí)間 g.setFont(f); g.setColor(Color.RED); g.drawS

35、tring(time,25,25); /用Oracle的方式連接數(shù)據(jù)庫(kù)package javaapplication13;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/JDBC 數(shù)據(jù)庫(kù)連接,直連,利用Oracle(雙下劃線)public class DBAccess public static String drv = "oracle.jdbc.drive

36、r.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

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

38、;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; c

39、atch (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 (SQLExc

40、eption 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;public

41、 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.*;/文件訪問(wèn)(Java IO)粗體+波浪線import ja

42、va.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("月份"); Cal

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

44、(int year,int month,int day)/構(gòu)造函數(shù) 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); ChangeMonth=n

45、ew 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); leftCenter.add(showDayi); cal=Calendar.getInstance(); Box box=Bo

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論