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

JAVA----日历源代码

时间:2017-08-19 17:15:09      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:center   instance   win   grid   lis   arm   val   textfield   dac   

1:先创建一个CalendarBean类:

代码:

  1. import java.util.Calendar;   
  2. public class CalendarBean   
  3. {   
  4.        String day[];   
  5. int year=2005,month=0;          
  6. public void setYear(int year)   
  7.        {   
  8. this.year=year;   
  9.        }   
  10. public int getYear()   
  11.        {   
  12. return year;   
  13.        }   
  14. public void setMonth(int month)   
  15.        {   
  16. this.month=month;   
  17.        }   
  18. public int getMonth()   
  19.        {   
  20. return month;   
  21.        }   
  22. public String[] getCalendar()   
  23.        {   
  24.            String a[]=new String[42];   
  25.            Calendar date=Calendar.getInstance();   
  26.            date.set(year,month-1,1);   
  27. int week=date.get(Calendar.DAY_OF_WEEK)-1;   
  28. int day=0;   
  29. //判断大月份  
  30. if(month==1||month==3||month==5||month==
  31.              ||month==8||month==10||month==12)   
  32.            {   
  33.                day=31;   
  34.            }   
  35. //判断小月  
  36. if(month==4||month==6||month==9||month==11)   
  37.            {   
  38.                day=30;   
  39.            }   
  40. //判断平年与闰年  
  41. if(month==2)   
  42.            {   
  43. if(((year%4==0)&&(year%100!=0))||(year%400==0))   
  44.                {   
  45.                    day=29;   
  46.                }   
  47. else   
  48.                {   
  49.                    day=28;   
  50.                }   
  51.             }   
  52. for(int i=week,n=1;i<week+day;i++)   
  53.            {   
  54.                    a[i]=String.valueOf(n) ;   
  55.                    n++;   
  56.            }              
  57. return a;   
  58.         }   
  59. }

2:创建一个CalendarFrame类

import javax.swing.*;
public class CalendarFrame extends JFrame implements ActionListener {
              JLabel labelDay[]=new JLabel[42];
              JTextField text=new JTextField(10);
              JButton titleName[]=new JButton[7];
              JButton button=new JButton();
              String name[]={"日","一","二","三","四","五","六"};
              JButton nextMonth,previousMonth;
              int year=1996,month=1;
              CalendarBean calendar;
              JLabel showmessage=new JLabel("",JLabel.CENTER);
              JLabel lbl1=new JLabel("请输入年份:");
              JLabel lbl2=new JLabel("        ");
              public CalendarFrame(){
                  JPanel pCenter=new JPanel();
                  //将pCenter的布局设置为7行7列的GridLayout布局
                  pCenter.setLayout(new GridLayout(7,7));
                  //pCenter添加组件titleName[i]
                  for(int i=0;i<7;i++){
                      titleName[i]=new JButton(name[i]);
                      pCenter.add(titleName[i]);
                  }
                  //pCenter添加组件LabelDay[i]
                  for(int i=0;i<42;i++){
                      labelDay[i]=new JLabel("",JLabel.CENTER);
                      pCenter.add(labelDay[i]);
                  }
                  text.addActionListener(this);
                  calendar=new CalendarBean();
                  calendar.setYear(year);
                  calendar.setMonth(month);
                  String day[]=calendar.getCalendar();                  
              
                 for(int i=0;i<42;i++){
                     labelDay[i].setText(day[i]);
                 }
                 nextMonth=new JButton("下月");
                 previousMonth=new JButton("上月");
                 button=new JButton("确定");
                 //注册监听器
                 nextMonth.addActionListener(this);
                 previousMonth.addActionListener(this);
                 button.addActionListener(this);
                 JPanel pNorth=new JPanel(),
                 pSouth=new JPanel();
                 pNorth.add(showmessage);
                 pNorth.add(lbl2);
                 pNorth.add(previousMonth);
                 pNorth.add(nextMonth);
                 pSouth.add(lbl1);
                 pSouth.add(text);
                 pSouth.add(button);
                 showmessage.setText("日历"+calendar.getYear()+"年"+calendar.getMonth()+"月");
                 ScrollPane scrollPane=new ScrollPane();
                 scrollPane.add(pCenter);
                 add(scrollPane,BorderLayout.CENTER);//窗口添加到ScrollPane 中间位置
                 add(pNorth,BorderLayout.NORTH);     //窗口添加pNorth在窗口北面
                 add(pSouth,BorderLayout.SOUTH);     //窗口添加pSouth在窗口的南面
              }
              public void actionPerformed(ActionEvent e){
                  if(e.getSource()==nextMonth){
                      month+=1;
                      if(month>12)
                         month=1;
                      calendar.setMonth(month);
                      String day[]=calendar.getCalendar();
                      for(int i=0;i<42;i++){
                          labelDay[i].setText(day[i]);
                      }                      
                  }
                  else if(e.getSource()==button){
                      month+=1;
                      if(month>12)
                          month=1;
                      calendar.setYear(Integer.parseInt(text.getText()));
                      String day[]=calendar.getCalendar();
                      for(int i=0;i<42;i++){
                          labelDay[i].setText(day[i]);                          
                      }
                      showmessage.setText("日历"+calendar.getYear()+"年"+calendar.getMonth()+"月");
                  }
              }
    }

3:在创建一个CalendarMainClass类

代码:

import javax.swing.JFrame;
import javax.swing.UIManager;
public class CalendarMainClass {
       public static void main(String[]args){
           try{
               UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
              }
           catch(Exception e){
               e.printStackTrace();
           }
           CalendarFrame frame=new CalendarFrame();
           frame.setBounds(100,100,360,300);
           frame.setTitle("中华日历");
           frame.setLocationRelativeTo(null);//窗体居中显示
           frame.setVisible(true);
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
}
运行结果:

技术分享

 

JAVA----日历源代码

标签:center   instance   win   grid   lis   arm   val   textfield   dac   

原文地址:http://www.cnblogs.com/zhouzetian/p/7396845.html

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