自己写的一个小小日历。执行程序是柯自己主动定位到当前年月日,当点击下个月button是会定位到下个月的这一天,就是说天数不会变。当在一个月中点击某一天时,以下的时间也会随时变化。

import java.util.Calendar;
public class CalendarMainClass {
   public static void main(String args[]) {
      CalendarFrame frame=new CalendarFrame();

      frame.setBounds(100,100,360,300);
      frame.setVisible(true);
      frame. setYearAndMonth((Calendar.getInstance()).get(Calendar.YEAR),(Calendar.getInstance()).get(Calendar.MONTH)+1);
   }
}

import java.util.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;
public class CalendarFrame extends JFrame implements ActionListener, MouseListener{
     /**
     * 
     */
    private static final long serialVersionUID = 1L;
     JLabel labelDay[]=new JLabel[42];
     JButton titleName[]=new JButton[7];
     JLabel labelCom=new JLabel();//跟踪选中的日期
     String name[]={"日","一","二","三", "四","五","六"};
     JButton nextMonth,previousMonth;
     CalendarBean calendar;
     JLabel showMessage=new JLabel("",JLabel.CENTER); 
     int year=2011,month=2;
     public CalendarFrame() {
        JPanel pCenter=new JPanel();
        pCenter.setBackground(Color.white);
        pCenter.setLayout(new GridLayout(7,7)); //将pCenter的布局设置为7行7列的GridLayout 布局。
        for(int i=0;i<7;i++) {
           titleName[i]=new JButton(name[i]); 
           titleName[i].setBorder(new SoftBevelBorder(BevelBorder.RAISED));
           pCenter.add(titleName[i]);
        }
        for(int i=0;i<42;i++) {
            labelDay[i]=new JLabel("",JLabel.CENTER);
            //labelDay[i].setBorder(new EtchedBorder(EtchedBorder.RAISED));
            labelDay[i].setBorder(BorderFactory.createLineBorder (Color.orange, 1));

           pCenter.add(labelDay[i]);//pCenter加入组件labelDay[i]。
        }
        calendar=new  CalendarBean();
        nextMonth=new JButton("下月");
        previousMonth=new JButton("上月");
        nextMonth.addActionListener(this);
        previousMonth.addActionListener(this);
        JPanel pNorth=new JPanel(),
              pSouth=new JPanel();
        pCenter.setBackground(Color.white);
        pNorth.setBackground(Color.white);
        pSouth.setBackground(Color.white);
        pNorth.add(previousMonth);
        pNorth.add(nextMonth);
        pSouth.add(showMessage);
        add(pCenter,BorderLayout.CENTER); 
        add(pNorth,BorderLayout.NORTH); // 窗体加入pNorth 在北面区域
        add(pSouth ,BorderLayout.SOUTH); // 窗体加入pSouth 在南区域。
        //setYearAndMonth(year,month);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

     }
     public void setYearAndMonth(int y,int m) {
         year=y;
         month=m;
         calendar.setYear(y);
         calendar.setMonth(m);       
         calendar.setDay((Calendar.getInstance()).get(Calendar.DAY_OF_MONTH));

         String day[]=calendar.getCalendar();
         for(int i=0;i<42;i++) 
         {
                 labelDay[i].setText(day[i]); 
             labelDay[i].addMouseListener(this);//对每一个标签监听
             if(day[i]!=null&&day[i].equals(String.valueOf(calendar.getDay())))//系统日期为黄色
             {

                 labelDay[i].setOpaque(true); // 标签设置不透明才干设置颜色          
                 labelDay[i].setBackground(Color.orange);
                 labelCom= labelDay[i];//点亮的标签

             }
         }

         showMessage.setText("日历:"+calendar.getYear()+"年"+ calendar.getMonth()+"月"+ calendar.getDay()+"日");
     }
     public void actionPerformed(ActionEvent e) {
        if(e.getSource()==nextMonth) {
          month=month+1;
          if(month>12)
          {
                month=1;
                year=year+1;
          }          
          calendar.setMonth(month);
          calendar.setYear(year);
          String day[]=calendar.getCalendar();
          for(int i=0;i<42;i++) {
              labelDay[i].setOpaque(false);         
          }

          for(int i=0;i<42;i++) {

            labelDay[i].setText(day[i]);      
            labelDay[i].addMouseListener(this);
            if(day[i]!=null&&day[i].equals(String.valueOf(calendar.getDay())))
            {           
              labelDay[i].setOpaque(true);          
              labelDay[i].setBackground(Color.orange);
              labelCom= labelDay[i];
             }
          }
        }
        else if(e.getSource()==previousMonth) {
          month=month-1;
          if(month<1)
          {
              month=12;
              year=year-1;
          }
          calendar.setMonth(month);
          calendar.setYear(year);
          String day[]=calendar.getCalendar();
          for(int i=0;i<42;i++) {
              labelDay[i].setOpaque(false);         
          }
          for(int i=0;i<42;i++)
          {

              labelDay[i].setText(day[i]);  
              labelDay[i].addMouseListener(this);
              if(day[i]!=null&&day[i].equals(String.valueOf(calendar.getDay())))
             {

                 labelDay[i].setOpaque(true);               
                 labelDay[i].setBackground(Color.orange);
                 labelCom= labelDay[i];
             }
          }           
       }
        showMessage.setText("日历:"+calendar.getYear()+"年"+ calendar.getMonth()+"月"+ calendar.getDay()+"日");
     }
    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mousePressed(MouseEvent e) {
        JLabel i=(JLabel)e.getSource();
        if(i.getText()!=null)
        {
            labelCom.setOpaque(false);
            i.setOpaque(true); 
            i.setBackground(Color.orange);    
            showMessage.setText("日历:"+calendar.getYear()+"年"+ calendar.getMonth()+"月"+Integer.parseInt(i.getText())+"日");
            labelCom=i;
            //刷新界面
            for(int j=0;j<42;j++)
            {
               labelDay[j].repaint();
            }
        }   
    }
    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }
}

import java.util.Calendar;

public class CalendarBean {
   String  day[];
   int year=2005,month=0;
   int DDay=0;
   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 DDay) {
       this.DDay=DDay;
   }
   public int getDay() {
       return DDay; 
   }
   public String[] getCalendar() {
       String a[]=new String[42];     
       Calendar 日历=Calendar.getInstance();//获得系统时间

       日历.set(year,month-1,1); //设置指定时间            
       int 星期几=日历.get(Calendar.DAY_OF_WEEK)-1;

       int day=0;
       if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
          day=31;
       if(month==4||month==6||month==9||month==11)
          day=30;
       if(month==2) {
         if(((year%4==0)&&(year%100!=0))||(year%400==0))
            day=29;
         else
            day=28;
       }
       for(int i=星期几,n=1;i<星期几+day;i++) {
          a[i]=String.valueOf(n) ;
          n++;
       }  
       return a;
   } 
} 

这里写图片描写叙述
这里写图片描写叙述