天天看點

改寫的月曆小程式(Java)

我使用傳統的MVC結構,設計了3個類。(具體代碼和工程見附件)

CalendarViewer.java主要處理UI,沿用了已有代碼,整理之并抽出業務邏輯,使其專注于顯示層處理。

CalendarViewer.java

<b>public</b> <b>class</b> CalendarViewer <b>extends</b> JWindow <b>implements</b> ActionListener {

    JPanel calendarYmPanel = <b>null</b>;

    JButton leftButton = <b>new</b> JButton("&lt;&lt;");

    JButton rightButton = <b>new</b> JButton("&gt;&gt;");

    Label yearLabel = <b>new</b> Label();

    Label monthLabel = <b>new</b> Label();

    Label passedDaysLabel = <b>new</b> Label();

    JPanel calendarWdPanel = <b>null</b>;// 是caledar_week和calendar_days的總包容體

    JPanel calendarWeekPanel = <b>null</b>;// 針對周列的布局

    JPanel calendarDaysPanel = <b>null</b>;// 針對日期列的布局

    JPanel calendarExitPanel = <b>null</b>;

    JButton quitButton = <b>new</b> JButton("關閉");

    Border emptyBorder = BorderFactory.createEmptyBorder();

    CalendarController cController = <b>new</b> CalendarController();

    <b>public</b> CalendarViewer() {

       <b>super</b>();

       buildUI();

    }

    <b>public</b> <b>void</b> buildUI() {

       buildTopPanel();

       buildCenterPanel();

       buildBottomPanel();

       setLayout(<b>new</b> BorderLayout());

       。。。。。。

    <b>private</b> <b>void</b> buildTopPanel() {。。。。。。}

    <b>private</b> <b>void</b> buildCenterPanel() {。。。。。。}

    <b>private</b> <b>void</b> buildBottomPanel() {。。。。。。}

    <b>public</b> JPanel updateDaysPanel() {。。。。。。}

    <b>public</b> <b>void</b> updatePassedDaysLabel() {。。。。。。}

    <b>public</b> <b>void</b> actionPerformed(ActionEvent e) {。。。。。。}

    <b>public</b> <b>static</b> <b>void</b> main(String[] args) {

       SwingUtilities.invokeLater(<b>new</b> Runnable() {

           <b>public</b> <b>void</b> run() {

              <b>new</b> CalendarViewer();

           }

       });

}

UI構造主要分3塊,對應圖上中下3個panel。

buildTopPanel();

buildCenterPanel();

buildBottomPanel();

事件監聽的處理由下面方法完成。

actionPerformed(ActionEvent e);

基于事件的UI更新由以下兩個方法完成。

updateDaysPanel();

updatePassedDaysLabel();

CalendarController.java主要處理具體的業務邏輯,而所使用的一些與具體應用無關的月曆算法邏輯則交給CalendarModel.java。

CalendarModel.java

<b>public</b> <b>class</b> CalendarModel {

    <b>private</b> <b>int</b> daytab[][] = {

           { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },

           { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };

    <b>public</b> <b>boolean</b> isLeapYear(<b>int</b> year) {

       <b>return</b> ((year % 4 == 0 &amp;&amp; year % 100 != 0) || year % 400 == 0);

    <b>public</b> <b>int</b> dayOfYear(<b>int</b> day, <b>int</b> month, <b>int</b> year) {

       <b>int</b> leap = isLeapYear(year) ? 1 : 0;

       <b>for</b> (<b>int</b> i = 1; i &lt; month; i++)

           day += daytab[leap][i];

       <b>return</b> day;

    <b>public</b> <b>int</b> daysOfMonth(<b>int</b> month, <b>int</b> year) {

       <b>return</b> daytab[leap][month];

    <b>public</b> <b>int</b> dayOfWeek(<b>int</b> day, <b>int</b> month, <b>int</b> year) {

       <b>if</b> (month == 1) {

           month = 13;

           year--;

       }

       <b>if</b> (month == 2) {

           month = 14;

       <b>return</b> (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year

              / 100 + year / 400) % 7 + 1;

建立一個二維數組,分别表示閏年與非閏年的每月天數。主要方法有:

<b>boolean</b> isLeapYear(<b>int</b> year);判斷閏年

dayOfYear(<b>int</b> day, <b>int</b> month, <b>int</b> year);計算所提供日期為目前year的第幾天

daysOfMonth(<b>int</b> month, <b>int</b> year);傳回目前月份的天數

dayOfWeek(<b>int</b> day, <b>int</b> month, <b>int</b> year); 計算某年某月某日是星期幾,這裡使用了基姆拉爾森計算公式。

基姆拉爾森計算公式

W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7

d 天

m 月

y 年

1月2月換算為去年的13 14月計算

w=0是星期一,依次類推。

本文轉自zhangjunhd51CTO部落格,原文連結:http://blog.51cto.com/zhangjunhd/127768,如需轉載請自行聯系原作者