码迷,mamicode.com
首页 > 编程语言 > 详细

Java小日历

时间:2015-07-26 22:47:29      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:java   日历   

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

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;
   } 
} 

技术分享
技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

Java小日历

标签:java   日历   

原文地址:http://blog.csdn.net/tuke_tuke/article/details/47071275

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!