版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、1、 設計內(nèi)容和要求 1、簡單萬年歷的基本要求 本程序的要求為:1.使用圖形用戶界面;2.本程序能夠?qū)崿F(xiàn)日期與星期的查詢。 2、需要實現(xiàn)的主要功能(1). 能夠顯示實現(xiàn)1800-2100年的日期,包括公歷、農(nóng)歷、星期、二十四節(jié)氣。(2). 能夠按照月份格式顯示,或按照星期格式顯示。(3). 能夠顯示系統(tǒng)時間。(4). 能夠設置鬧鐘。(5). 能夠查詢?nèi)舾蓢H大城市(分布在不同時區(qū))的時間。二、系統(tǒng)分析與概要設計1. 總天數(shù)的算法:首先用if語句判斷定義年到輸入年之間每一年是否為閏年,是閏年,該年的總天數(shù)為366,否則,為365。然后判斷輸入的年是否為定義年,若是,令總天數(shù)S=1,否則,用累加法
2、計算出定義年到輸入年之間的總天數(shù),再把輸入年的一月到要輸出的月份之間的天數(shù)累加起來,若該月是閏年中的月份并且該月還大于二月,再使總天數(shù)加1,否則,不加,既算出從定義年一月一日到輸出年的該月一日的總天數(shù)。2. 輸出月份第一天為星期幾的算法:使總天數(shù)除以7取余加2得幾既為星期幾,若是7,則為星期日。3. 算出輸出月份第一天為星期幾的算法:算出輸出月份第一天為星期幾后,把該日期以前的位置用空格補上,并總該日起一次輸出天數(shù)直到月底,該月中的天數(shù)加上該月一日為星期幾的數(shù)字再除以7得0換行,即可完整的輸出該月的日歷。4. 如果年份小于1900年則程序不予判斷5.公歷與農(nóng)歷的換算。公歷年對應的農(nóng)歷數(shù)據(jù),每年
3、三字節(jié),格式第一字節(jié)BIT7-4 位 表示閏月月份,值為0 為無閏月,算法:日期+年份+所過閏年數(shù)+月較正數(shù)之和除7 的余數(shù)就是星期但如果是在閏年又不到3 月份上述之和要減一天再除7星期數(shù)為0。 6、系統(tǒng)時間的設置。直接從系統(tǒng)得到時間,設置字體,背景,顯示框架等等,繪圖就好了。三、詳細設計和編碼程序初始化獲取當前系統(tǒng)時間年份等數(shù)據(jù)提交年月變更數(shù)據(jù) 是否輸入年份或選擇月份開始計算,繪制屏幕結束 總流程圖 公歷轉農(nóng)歷流程圖圖3時間調(diào)整程序流程圖 2、類的定義及關系本次程序設計共用了 class Main Frame extends JFrame, class Clock extends Canva
4、s implements Runnable和class Lunar三個大類,用于繪制萬年歷,顯示萬年歷的系統(tǒng)時間和完成農(nóng)歷與公歷的換算。這些類中又運用了許多函數(shù),來完成月份的輸出,背景的設置,特定日期的標注等等。引用的類有:import java.ait.*;import java.ait.event.Accentuation;import java.ait.event.Action Listener;import java.ait.event.Mouse Event;import java.ait.event.McAlister;import java.io.Buffered;import
5、java.io.Buffered;import java.io.File;import java.io.Nonmainstream/import java.ql.Date;import java.text.Date Format;import java.text.Simplehearted;import java.til.Calendar;import java.til.Date;import Java.swing.Button;import Java.swing.Dialog;import Java.swing.Frame;import Java.swing.J Label;import J
6、ava.swing.Optionee;import Java.swing.J Panel;import Java.swing.Enscroll;import Java.swing.Getable;import Java.swing.Tartarean;import Java.swing.Leftfield;import Java.swing.table.Democratifiable;import java.text.*;import java.ait.*;import Java.swing.*;import java.ait.event.*;import Java.swing.border.
7、*;import java.til.*;import Java.swing.*; 3、 運行效果4、 源代碼 class MainFrame extends JFrame private static final long serialVersionUID = 1L; JPanel panel = new JPanel(new BorderLayout(); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(new GridLayout(7, 7); JPanel panel3 = new JPanel(); JLabel lab
8、el = new JLabel49; JLabel y_label = new JLabel(年份); JLabel m_label = new JLabel(月份); JComboBox com1 = new JComboBox(); JComboBox com2 = new JComboBox(); JButton but1=new JButton(上個月); JButton but2=new JButton(下個月); int re_year, re_month; int x_size, y_size; String year_num; Calendar now = Calendar.g
9、etInstance(); / 實例化Calendar MainFrame() super(萬年歷); setSize(600, 700); x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth(); y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight(); setLocation(x_size - 300) / 2, (y_size - 350) / 2); setDefaultCloseOperation(JFrame.
10、EXIT_ON_CLOSE); panel1.add(but1); panel1.add(y_label); panel1.add(com1); panel1.add(m_label); panel1.add(com2); panel1.add(but2); for (int i = 0; i 49; i+) labeli = new JLabel(, JLabel.CENTER);/ 將顯示的字符設置為居中 panel2.add(labeli); panel3.add(new Clock(this); panel.add(panel1, BorderLayout.NORTH); panel.
11、add(panel2, BorderLayout.CENTER); panel.add(panel3, BorderLayout.SOUTH); panel.setBackground(Color.white); panel1.setBackground(Color.white); panel2.setBackground(Color.white); panel3.setBackground(Color.gray); Init(); but1.addActionListener(new AnAction(); but2.addActionListener(new AnAction(); com
12、1.addActionListener(new ClockAction(); com2.addActionListener(new ClockAction(); setContentPane(panel); setVisible(true); setResizable(false); class AnAction implements ActionListener public void actionPerformed(ActionEvent e) int c_year, c_month,c_week; c_month = Integer.parseInt(com2.getSelectedIt
13、em().toString() - 1; / 得到當前月份,并減1,計算機中的月為011 c_year = Integer.parseInt(com1.getSelectedItem().toString()-1; / 得到當前所選年份 if(e.getSource()=but1) if(c_month=0) c_year=c_year-1; c_month=11; else c_month=c_month-1; if(e.getSource()=but2) if(c_month=11) c_year=c_year+1; c_month=0; else c_month=c_month+1; c
14、om1.setSelectedIndex(c_year); com2.setSelectedIndex(c_month); c_year = Integer.parseInt(com1.getSelectedItem().toString(); / 得到當前所選年份 c_month = Integer.parseInt(com2.getSelectedItem().toString() - 1; / 得到當前月份,并減1,計算機中的月為011 c_week = use(c_year, c_month); / 調(diào)用函數(shù)use,得到星期幾 Resetday(c_week, c_year, c_mo
15、nth); / 調(diào)用函數(shù)Resetday class NextAction implements ActionListener public void actionPerformed(ActionEvent arg0) int c_year, c_month,c_week; c_month = Integer.parseInt(com2.getSelectedItem().toString() - 1; / 得到當前月份,并減1,計算機中的月為011 c_year = Integer.parseInt(com1.getSelectedItem().toString(); / 得到當前所選年份
16、if(c_month=11) c_year=c_year+1; c_month=0; c_week = use(c_year, c_month); / 調(diào)用函數(shù)use,得到星期幾 Resetday(c_week, c_year, c_month); / 調(diào)用函數(shù)Resetday class ClockAction implements ActionListener public void actionPerformed(ActionEvent arg0) int c_year, c_month, c_week; c_year = Integer.parseInt(com1.getSelecte
17、dItem().toString(); / 得到當前所選年份 c_month = Integer.parseInt(com2.getSelectedItem().toString() - 1; / 得到當前月份,并減1,計算機中的月為011 c_week = use(c_year, c_month); / 調(diào)用函數(shù)use,得到星期幾 Resetday(c_week, c_year, c_month); / 調(diào)用函數(shù)Resetday public void Init() int year, month_num, first_day_num; String log = 星期日, 星期一, 星期二,
18、 星期三, 星期四, 星期五, 星期六 ; for (int i = 0; i 7; i+) labeli.setText(logi); for (int i = 0; i 49; i = i + 7) labeli.setForeground(Color.blue); / 將星期日的日期設置為紅色 for (int i = 6; i 49; i = i + 7) labeli.setForeground(Color.blue);/ 將星期六的日期設置為hong色 for (int i = 1; i 10000; i+) com1.addItem( + i); for (int i = 1;
19、i 13; i+) com2.addItem( + i); month_num = (int) (now.get(Calendar.MONTH); / 得到當前時間的月份 year = (int) (now.get(Calendar.YEAR); / 得到當前時間的年份 com1.setSelectedIndex(year - 1); / 設置下拉列表顯示為當前年? com2.setSelectedIndex(month_num); / 設置下拉列表顯示為當前月 first_day_num = use(year, month_num); Resetday(first_day_num, year
20、, month_num); public int use(int reyear, int remonth) int week_num; now.set(reyear, remonth, 1); / 設置時間為所要查詢的年月的第一天 week_num = (int) (now.get(Calendar.DAY_OF_WEEK);/ 得到第一天的星期 return week_num; / SuppressWarnings(deprecation) public void Resetday(int week_log, int year_log, int month_log) int month_da
21、y_score; / 存儲月份的天數(shù) int count; Lunar lunar; int month_day; String LunarDate=new String49; month_day_score = 0; count = 1; for (int i = 1; i 49; i +) for(int j=0;j49;j=j+7) if(i!=j&i!=j+6) labeli.setForeground(Color.black); Date date = new Date(year_log, month_log + 1, 1); / now MONTH是從0開始的, 對于一月第幾天來說
22、,DAY_OF_MONTH第一天就是1. 對于一年第幾個月來說,MONTH一月份是0,二月份是1. Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, -1); / 前個月 month_day_scor= cal.getActualMaximum(Calendar.DAY_OF_MONTH);/ 最后一天 month_day=month_day_score;for (int i = 7; i 49; i+) / 初始化標簽 labeli.setText(); week_log = w
23、eek_log + 6; / 將星期數(shù)加6,使顯示正確 month_day_score = month_day_score + week_log; lunar=new Lunar(); for(int i=0;imonth_day;i+) LunarDatei=lunar.getLunarDate( year_log, month_log+1, i+1); for (int i = week_log; i 0x8; i = 1) if (lunarInfoy - 1900 & i) != 0) sum += 1; return (sum + leapDays(y); /= 傳回農(nóng)歷 y年閏月的
24、天數(shù) final private static int leapDays(int y) if (leapMonth(y) != 0) if (lunarInfoy - 1900 & 0x10000) != 0) return 30; else return 29; else return 0; /= 傳回農(nóng)歷 y年閏哪個月 1-12 , 沒閏傳回 0 final private static int leapMonth(int y) return (int) (lunarInfoy - 1900 & 0xf); /= 傳回農(nóng)歷 y年m月的總天數(shù) final private static int
25、 monthDays(int y, int m) if (lunarInfoy - 1900 & (0x10000 m) = 0) return 29; else return 30; /= 傳回農(nóng)歷 y年的生肖 final public String animalsYear() final String Animals = new String鼠, 牛, 虎, 兔, 龍, 蛇, 馬, 羊, 猴, 雞, 狗, 豬; return Animals(year - 4) % 12; /= 傳入 月日的offset 傳回干支, 0=甲子 final private static String cycl
26、icalm(int num) final String Gan = new String甲, 乙, 丙, 丁, 戊, 己, 庚, 辛, 壬, 癸; final String Zhi = new String子, 丑, 寅, 卯, 辰, 巳, 午, 未, 申, 酉, 戌, 亥; return (Gannum % 10 + Zhinum % 12); /= 傳入 offset 傳回干支, 0=甲子 final public String cyclical() int num = year - 1900 + 36; return (cyclicalm(num); public static Stri
27、ng getChinaDayString(int day) String chineseTen = 初, 十, 廿, 卅; int n = day % 10 = 0 ? 9 : day % 10 - 1; if (day 30) return ; if (day = 10) return 初十; else return chineseTenday / 10 + chineseNumbern; * 傳出y年m月d日對應的農(nóng)歷. * yearCyl3:農(nóng)歷年與1864的相差數(shù) ? * monCyl4:從1900年1月31日以來,閏月數(shù) * dayCyl5:與1900年1月31日相差的天數(shù),再加40
28、 ? public String getLunarDate(int year_log, int month_log, int day_log) /SuppressWarnings(unused) int yearCyl, monCyl, dayCyl; int leapMonth = 0; String nowadays; Date baseDate = null; Date nowaday=null; try baseDate = chineseDateFormat.parse(1900年1月31日); catch (ParseException e) e.printStackTrace()
29、; /To change body of catch statement use Options | File Templates. nowadays=year_log+年+month_log+月+day_log+日; try nowaday = chineseDateFormat.parse(nowadays); catch (ParseException e) e.printStackTrace(); /To change body of catch statement use Options | File Templates. /求出和1900年1月31日相差的天數(shù) int offset
30、 = (int) (nowaday.getTime() - baseDate.getTime() / L); dayCyl = offset + 40; monCyl = 14; /用offset減去每農(nóng)歷年的天數(shù) / 計算當天是農(nóng)歷第幾天 /i最終結果是農(nóng)歷的年份 /offset是當年的第幾天 int iYear, daysOfYear = 0; for (iYear = 1900; iYear 0; iYear+) daysOfYear = yearDays(iYear); offset -= daysOfYear; monCyl += 12; if (offset 0) offset +
31、= daysOfYear; iYear-; monCyl -= 12; /農(nóng)歷年份 year = iYear; yearCyl = iYear - 1864; leapMonth = leapMonth(iYear); /閏哪個月,1-12 leap = false; /用當年的天數(shù)offset,逐個減去每月(農(nóng)歷)的天數(shù),求出當天是本月的第幾天 int iMonth, daysOfMonth = 0; for (iMonth = 1; iMonth 0; iMonth+) /閏月 if (leapMonth 0 & iMonth = (leapMonth + 1) & !leap) -iMo
32、nth; leap = true; daysOfMonth = leapDays(year); else daysOfMonth = monthDays(year, iMonth); offset -= daysOfMonth; /解除閏月 if (leap & iMonth = (leapMonth + 1) leap = false; if (!leap) monCyl+; /offset為0時,并且剛才計算的月份是閏月,要校正 if (offset = 0 & leapMonth 0 & iMonth = leapMonth + 1) if (leap) leap = false; else leap = true; -iMonth; -monCyl; /offset小于0時,也要校正 if (offset 0) offset += daysOfMonth; -iMonth; -monCyl; month = iMonth; day = offset + 1; if(month)=1)&day=1) return 春節(jié); else if(month)=1)&day=15) return 元宵; else if(month)=5)&day=5)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 道樂物流交通安全培訓課件
- 辰溪縣交通安全課件大賽
- 車險險種介紹課件
- 車隊安全生產(chǎn)課件
- 車隊安全培訓常識大全課件
- 2026年項目經(jīng)理年終總結范文(2篇)
- 車間負責人安全培訓記錄課件
- 車間級安全教育培訓題庫課件
- 2025年電商合規(guī)經(jīng)營(資質(zhì)-廣告)與風險規(guī)避工作心得(3篇)
- 2026年消毒供應中心不良事件的應急及對策(2篇)
- 2025年菏澤巨野縣高鐵北站公開招聘客運服務人員(6人)備考筆試試題及答案解析
- 2026年陜西能源職業(yè)技術學院教師招聘(42人)參考筆試題庫附答案解析
- 2025年榆林市住房公積金管理中心招聘(19人)筆試考試參考題庫及答案解析
- 福州古厝課件
- 2026年鞍山職業(yè)技術學院單招職業(yè)技能考試題庫參考答案詳解
- 眩暈護理的研究方向與趨勢
- 2025年新公開選拔中小學校長筆試試題與答案
- 2025年度吊燈市場調(diào)研:時尚美觀、風格多樣及餐廳客廳需求
- 北京市西城區(qū)2024-2025學年六年級上學期期末英語試題
- 福建農(nóng)林大學研究生學位論文格式的統(tǒng)一要求(2025年修訂)
- 基坑回填安全措施方案
評論
0/150
提交評論