日历小程序

作者在 2011-12-24 17:01:25 发布以下内容
import java.util.Calendar;

public class MyCalendar{
    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 日历=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;
        }else if(month==4||month==6||month==9||month==11){
            day=30;
        }else 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;
    }
}
 
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class CalendarFrame extends Frame implements ActionListener{
    Label[] labelDay=new Label[42];
    Button[] titleName=new Button[7];
    String[] name={"日","一","二","三","四","五","六"};
    Button nextMonth,previousMonth;
    int year=2006,month=10;
    MyCalendar calendar;
    Label showMessage=new Label("",Label.CENTER);
    
    public CalendarFrame(){
        Panel pCenter=new Panel();
        pCenter.setLayout(new GridLayout(7,7)); //将pCenter的布局设置为7行7列的GridLayout布局
        for (int i = 0; i<7; i++){
            titleName[i]=new Button(name[i]);
            pCenter.add(titleName[i]);
            //pCenter添加组件titleName[i]
        }
        for (int i = 0; i<42; i++){
            labelDay[i]=new Label("",Label.CENTER);
            pCenter.add(labelDay[i]);
            //pCenter添加组件LabelDay[i]
        }
        
        calendar=new MyCalendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        String[] day=calendar.getCalendar();
        for (int i = 0; i<42; i++){
            labelDay[i].setText(day[i]);
        }
        nextMonth=new Button("下月");
        previousMonth=new Button("上月");
        nextMonth.addActionListener(this);
        previousMonth.addActionListener(this);
        Panel pNorth=new Panel(),
                pSouth=new Panel();
        pNorth.add(previousMonth);
        pNorth.add(nextMonth);
        pSouth.add(showMessage);
        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=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--;
            if(month<1)
                month=12;
            calendar.setMonth(month);
            String[] day=calendar.getCalendar();
            for (int i = 0; i<42; i++){
                labelDay[i].setText(day[i]);
            }
        }
        showMessage.setText("日历:"+calendar.getYear()+"年"+
                        calendar.getMonth()+"月");
    }
}
 
public class CalendarMainClass{
    
    public static void main(String[] agrs){
        CalendarFrame frame = new CalendarFrame();
        frame.setBounds(100,100,360,300);
        frame.setVisible(true);
        frame.validate();
        frame.addWindowListener(new java.awt.event.WindowAdapter(){
            public void windowClosing(java.awt.event.WindowEvent e){
                System.exit(0);
            }
        });        
    }
}
 
java codes | 阅读 1317 次
文章评论,共0条
游客请输入验证码
浏览18056次
最新评论