Java项目:日历万年历(java+swing)

源码获取:博客首页 "资源" 里下载!

功能简介:

万年历

启动类:

public class CalendarMainClass   
{   
        public static void main(String args[])   
        {   
             try {  
                 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //windows界面风格  
              }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);  
        }   
} 

实体类:

public class CalendarBean   
{   
       String day[];   
       int year=2005,month=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 String[] getCalendar()   
       {   
           String a[]=new String[42];   
           Calendar date=Calendar.getInstance();   
           date.set(year,month-1,1);   
           int week=date.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=week,n=1;i<week+day;i++)   
           {   
                   a[i]=String.valueOf(n) ;   
                   n++;   
           }              
           return a;   
        }   
}

日历显示类:

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=2018,month=1; //启动程序显示的日期信息  
       CalendarBean calendar;   
       JLabel showMessage=new JLabel("",JLabel.CENTER);   
       JLabel lbl1 = new JLabel("请输入年份:");  
       JLabel lbl2=new JLabel("      ");  
 
       public CalendarFrame()   
       {
       	setBackground(new Color(0, 128, 128));   
           JPanel pCenter=new JPanel();   
           pCenter.setBackground(new Color(0, 139, 139));
             
         //将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);   
           getContentPane().add(scrollPane,BorderLayout.CENTER);// 窗口添加scrollPane在中心区域   
           getContentPane().add(pNorth,BorderLayout.NORTH);// 窗口添加pNorth 在北面区域   
           getContentPane().add(pSouth,BorderLayout.SOUTH);// 窗口添加pSouth 在南区域。  
 
        }   
        
        public void actionPerformed(ActionEvent e)   
        {   
            if(e.getSource()==nextMonth)   
            {   
                month=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()==previousMonth)   
            {   
                month=month-1;                
                if(month<1)   
                month=12;   
                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=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()+"月" );   
       }   
}

源码获取:博客首页 "资源" 里下载!

java 万年历程序代码 package test2; import java.util.Scanner; public class Test2{ public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("请输入年份:"); int year=input.nextInt(); System.out.print("\n请输入月份:"); int month=input.nextInt(); System.out.println(); int totalDays=0; if(year>=1900) { for(int i=1900;i<year;i++) { if((i%4==0 && i0!=0)||(i%400==0)) totalDays+=366; else totalDays+=365; } int daysOfMonth=0; int days; for(int i=1;i<month;i++) { switch(i) { case 2: if((year%4==0 && year0!=0)|| year%400==0) days=29; else days=28; break; case 4: case 6: case 9: case 11: days=30; break; default: days=31; } daysOfMonth+=days; } switch(month) { case 2: if((year%4==0 && year0!=0)|| year%400==0) days=29; else days=28; break; case 4: case 6: case 9: case 11: days=30; break; default: days=31; } totalDays+=daysOfMonth; int firstDay=(totalDays)%7+1; if(firstDay==7)firstDay=0; System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");//显示月历 for(int i=0;i<firstDay;i++) System.out.print("\t"); for(int i=1;i<=days;i++) { System.out.print(i+"\t"); if((i-1)%7+firstDay==6) System.out.println(); } } else if(year>0&&year<1900) { for(int i=1899;i>year;i--) { if((i%4==0 && i0!=0)||(i%400==0)) totalDays+=366; else totalDays+=365; } int daysOfMonth=0; int days; for(int i=12;i>=month;i--) { switch(i) { case 2: if((year%4==0 && year0!=0)|| year%400==0) days=29; else days=28; break; case 4: case 6: case 9: case 11: days=30; break; default: days=31; } daysOfMonth+=days; } switch(month) { case 2: if((year%4==0 && year0!=0)|| year%400==0) days=29; else days=28; break; case 4: case 6: case 9: case 11: days=30; break; default: days=31; } totalDays+=daysOfMonth; int firstDay=8-(totalDays)%7; if(firstDay==7)firstDay=0; if(firstDay==8)firstDay=1; System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六"); for(int i=0;i<firstDay;i++) System.out.print("\t"); for(int i=1;i<=days;i++) { System.out.print(i+"\t"); if((i-1)%7+firstDay==6) System.out.println(); } } System.out.println("\n程序结束"); } }
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq1334611189

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值