程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 用Java寫一個日歷小程序-代碼

用Java寫一個日歷小程序-代碼

編輯:關於JAVA

SDate部分:

class SDate {
     public static int accountDays(int year,int month) {
         switch (month) {
         case 1:
         case 3:
         case 5:
         case 7:
         case 8:
         case 10:
         case 12:
             return 31;
         case 2:
             if(isLeap(year))
                 return 29;
             else return 28;
         default:
             return 30;
         }
     }
     public static boolean isLeap(int year) {
         if (year%4 == 0 && year%100 != 0)
             return true;
         else if (year%100 == 0 && year%400 == 0)
             return true;
         else  
             return false;
     }
     public static int fixDay(int year,int month) {
         int base_year = 1;
         int[] base_month = {0,0,3,3,6,1,4,0,3,5,0,3,5};
         if (isLeap(year)) {
             base_year++;
             for (int i = 3; i<=6; i++) {
                 base_month[i] += 1;
                 if (base_month[i] == 7) {
                     base_month[i] = 0;
                 }
             }
             for (int i = 9; i <= 12; i++) {
                 base_month[i] += 1;
                 if (base_month[i] == 7) {
                     base_month[i] = 0;
                 }
             }
         }
         return (year+year/4+year/400-year/100-base_year+base_month[month]+1)%7;
     }
}

DateGrid部分:

import Javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.*;
public class DateGrid extends JWindow implements ActionListener {
     //DateGrid的構造方法
     public DateGrid() {
         super();
         this.design();
     }

//整體布局設計:

Calendar c = Calendar.getInstance();
     JPanel calendar_ym = null;
         Color deepblue = new Color(19,63,164);
         JButton left = new JButton("<<");
         JButton right = new JButton(">>");
         Label year = new Label();
         Label month = new Label();
         int year_now = c.get(Calendar.YEAR);
         int month_now = c.get(Calendar.MONTH);
     JPanel calendar_wd = null;//是caledar_week和calendar_days的總包容體
         JPanel calendar_week = null;
     JPanel calendar_exit = null;
         JButton quit = new JButton("關閉");
     Border empty = BorderFactory.createEmptyBorder();
    
     public void design() {
         calendar_ym = new JPanel(new BorderLayout());
         MatteBorder m_left = BorderFactory.createMatteBorder(1, 1, 1, 0, new Color(186,223,254));
         MatteBorder m_right = BorderFactory.createMatteBorder(1, 0, 1, 1, new Color(186,223,254));
         JPanel l = new JPanel(new FlowLayout(FlowLayout.LEFT));
             l.setBorder(m_left);
             l.add(left);
                 left.setBorder(empty);
                 left.setBackground(deepblue);
                 left.setForeground(Color.WHITE);
                 left.addActionListener(this);
         JPanel r = new JPanel(new FlowLayout(FlowLayout.RIGHT));
             r.setBorder(m_right);
             r.add(right);
                 right.setBorder(empty);
                 right.setBackground(deepblue);
                 right.setForeground(Color.WHITE);
                 right.addActionListener(this);
         JPanel c = new JPanel(new FlowLayout(FlowLayout.CENTER));
             c.add(year);
             year.setText(String.valueOf(year_now));
             c.add(new Label("/"));
             c.add(month);
             if (month_now == 0) {
                 month_now += 1;
             }
             month.setText(String.valueOf(month_now));
         Border line = BorderFactory.createLineBorder(new Color(186,223,254));
         c.setBorder(line);
         l.setBackground(deepblue);
         r.setBackground(deepblue);
         c.setBackground(deepblue);
         c.setForeground(Color.WHITE);
         calendar_ym.add(l,BorderLayout.WEST);
         calendar_ym.add(r,BorderLayout.EAST);
         calendar_ym.add(c,BorderLayout.CENTER);
         calendar_wd = new JPanel(new BorderLayout());
             JPanel calendar_week = new JPanel(new GridLayout(1,7));
                 calendar_week.setBackground(Color.WHITE);
                 calendar_week.setForeground(Color.BLACK);
                 MatteBorder m_fix = BorderFactory.createMatteBorder(0, 1, 0, 1, Color.BLACK);
                 calendar_week.setBorder(m_fix);
                 Label sunday = new Label("日",Label.CENTER);
                 sunday.setForeground(Color.RED);
                 calendar_week.add(sunday);
                 calendar_week.add(new Label("一",Label.CENTER));
                 calendar_week.add(new Label("二",Label.CENTER));
                 calendar_week.add(new Label("三",Label.CENTER));
                 calendar_week.add(new Label("四",Label.CENTER));
                 calendar_week.add(new Label("五",Label.CENTER));
                 Label six = new Label("六",Label.CENTER);
                 six.setForeground(Color.green);
                 calendar_week.add(six);
             calendar_wd.add(calendar_week,BorderLayout.NORTH);
             calendar_wd.add(changeDays(),BorderLayout.CENTER);
         calendar_exit = new JPanel(new FlowLayout(FlowLayout.CENTER));
             calendar_exit.setBackground(Color.WHITE);
             MatteBorder m_close = BorderFactory.createMatteBorder(0, 1, 1, 1, Color.BLACK);
             calendar_exit.setBorder(m_close);
             calendar_exit.add(quit);
                 quit.setBorder(empty);
                 quit.setBackground(Color.WHITE);
                 quit.setForeground(Color.BLUE);
             quit.addActionListener(this);
         this.setLayout(new BorderLayout());
         this.add(calendar_ym,BorderLayout.NORTH);
         this.add(calendar_wd,BorderLayout.CENTER);
         this.add(calendar_exit,BorderLayout.SOUTH);
         this.pack();
        this.setLocation(
              (int)this.getToolkit().getScreenSize().getWidth() - this.getWidth(),
              100
        );
  
        this.setVisible(true);
     }  
     //針對日期列的布局
     JPanel calendar_days = null;
     public JPanel changeDays() {
         //日期列版面的基本設計:
         calendar_days = new JPanel(new GridLayout(6,7));
             Border line = BorderFactory.createLineBorder(Color.BLACK);
             calendar_days.setBorder(line);
             calendar_days.setBackground(Color.WHITE);
             calendar_days.setForeground(Color.BLACK);
         //日期的排列方法:
         int first = SDate.fixDay(Integer.parseInt(year.getText()),Integer.parseInt(month.getText()));
         int count = SDate.accountDays(Integer.parseInt(year.getText()),Integer.parseInt(month.getText()));
         int days = 1;
         int space_length = 0; //空白處的長度
         for (int i = 0; i < 42; i++) {
             if (i == first) {  
                 count += i;
                 space_length = i-1;
             }
             if ( i>=first && i<count)  
                 calendar_days.add(new JLabel(String.valueOf(days++),JLabel.CENTER));
             else  
                 calendar_days.add(new JLabel(""));    //其他的空白處為空白表示
         }
         //對現在日期的標注:
         c.setTimeZone(TimeZone.getTimeZone("GMT+8"));
         Border b = BorderFactory.createLineBorder(Color.BLUE);
         int now_day_position = space_length+c.get(Calendar.DAY_OF_YEAR);
         int ym_year = Integer.parseInt(year.getText());
         int ym_month = Integer.parseInt(month.getText());
         int real_year = c.get(Calendar.YEAR);
         int real_month = c.get(Calendar.MONTH);
         if (real_month == 0) {
             real_month += 1;
         }
         if (real_year == ym_year && real_month == ym_month) {
             ((JComponent)calendar_days.getComponent(now_day_position)).setBorder(b);
         }
         //返回當前日期列版面
         return calendar_days;
     }
     public void actionPerformed(ActionEvent e) {
         if (e.getSource() == left e.getSource() == right) {
             if (e.getSource() == left) {
                 month_now -= 1;
                 if (month_now == 0) {
                     month_now = 12;
                     year_now -= 1;
                     month.setText(String.valueOf(month_now));
                     year.setText(String.valueOf(year_now));
                 } else {
                     month.setText(String.valueOf(month_now));
                 }
             }
             if (e.getSource() == right) {
                 month_now += 1;
                 if (month_now == 13) {
                     month_now = 1;
                     year_now += 1;
                     month.setText(String.valueOf(month_now));
                     year.setText(String.valueOf(year_now));
                 } else
                     month.setText(String.valueOf(month_now));
             }
             calendar_days.removeAll();
             calendar_wd.add(changeDays());
             setVisible(true);
         }
         if (e.getSource() == quit) {
             dispose();
         }
     }
     public static void main(String[] args) {
         new DateGrid();
     }
}

由於只作為例子,故寫的較為粗糙,還可以優化,並且只實現主要功能

--------------------------------------------------------

本文出自 “博遠至靜” 博客,請務必保留此出處http://sunspot.blog.51cto.com/372554/126810

本文配套源碼

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved