⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calendar.java

📁 java applet实用编程教程第二部分,非常好的applet源代码
💻 JAVA
字号:

import java.applet.Applet; 

import java.awt.*; 

import java.util.Date; 



public class Calendar extends Applet 

{ 

static final int TOP = 90;  

static final int HEADER = 30;

static final int NCELLX = 7; 

static final int CELLSIZE = 60;

static final int MARGIN = 8;  

static final int FEBRUARY = 1; 





Label yearLabel = new Label(" 年:"); 

TextField yearTextField = new TextField("1996", 5); 

Label monthLabel = new Label(" 月:"); 

Choice monthChoice = new Choice(); 

Button updateButton = new Button("更新"); 





Date now = new Date(); 





Font smallArialFont = new Font("Arial", Font.PLAIN, 15); 



Font bigArialFont = new Font("Arial", Font.BOLD, 30); 



String days[] = {"星期日", "星期一", "星期二", "星期三", 

                 "星期四", "星期五", "星期六"}; 



String months[] = {"一月", "二月", "三月", "四月", 

                   "五月", "六月", "七月", "八月", "九月", 

                   "十月", "十一月", "十二月"}; 



int DaysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 



int userMonth; 

int userYear; 



public void init() 



   { 

    setBackground(Color.white); 



    
    userMonth = now.getMonth(); 

    userYear = now.getYear() + 1900; 



    

    yearLabel.setFont(smallArialFont); 

    add(yearLabel); 



    

    yearTextField.setFont(smallArialFont); 

    yearTextField.setText(String.valueOf(userYear)); 

    add(yearTextField); 



    

    monthLabel.setFont(smallArialFont); 

    add(monthLabel); 



    

    monthChoice.setFont(smallArialFont); 

    for (int i = 0; i < 12; i++) 

    monthChoice.addItem(months[i]); 

    monthChoice.select(userMonth); 

    add(monthChoice); 



    

    updateButton.setFont(smallArialFont); 

    add(updateButton); 

    }


public void paint(Graphics g) 


    { 

    FontMetrics fm;   
    int fontAscent;   

    int dayPos;       

    int xSize, ySize; 

    int numRows;       

    int xNum, yNum;     
    int numDays;      

    String dayStr;     

    int margin;          
    String caption;    

    

    fm = g.getFontMetrics(); 

    fontAscent = fm.getAscent(); 

    dayPos = TOP + (HEADER + fontAscent) / 2; 



   
    xSize = NCELLX * CELLSIZE; 



    

    g.drawRect(0, TOP, xSize, HEADER); 



    

     for (int i = 0; i < NCELLX; i++) 

     g.drawString(days[i], (CELLSIZE-fm.stringWidth(days[i]))/2 + i*CELLSIZE, 

                  dayPos); 



    

    numRows = NumberRowsNeeded(userYear, userMonth); 



    

    ySize = numRows * CELLSIZE; 

    for (int i = 0; i <= xSize; i += CELLSIZE) 

    g.drawLine(i, TOP + HEADER, i, TOP + HEADER + ySize); 



     

     for (int i = 0, j = TOP + HEADER; i <= numRows; i++, j += CELLSIZE) 

     g.drawLine(0, j, xSize, j); 



    

    xNum = (CalcFirstOfMonth(userYear, userMonth) + 1) * CELLSIZE - MARGIN; 

    yNum = TOP + HEADER + MARGIN + fontAscent; 



    
    numDays = DaysInMonth[userMonth] + 
    ((IsLeapYear(userYear) && (userMonth == FEBRUARY)) ? 1 : 0); 



     

     for (int day = 1; day <= numDays; day++) 

     { 

     dayStr = String.valueOf(day); 

     g.drawString(dayStr, xNum - fm.stringWidth(dayStr), yNum); 

     xNum += CELLSIZE; 



     if (xNum > xSize) 

        { 

         xNum = CELLSIZE - MARGIN; 

         yNum += CELLSIZE; 

         } 

     } 



   

    g.setFont(bigArialFont); 



 fm = g.getFontMetrics(); 

 

 margin = 2 * fm.getDescent(); 





 caption = String.valueOf(userYear) + "年" +months[userMonth] ; 

 g.drawString(caption, (xSize-fm.stringWidth(caption))/2, TOP - margin); 

 }


public boolean action(Event e, Object o) 



 { 

 int userYearInt; 



 if (e.target instanceof Button) 

 { 

 if ("更新".equals((String)o)) 

 { 

 
 userMonth = monthChoice.getSelectedIndex(); 




 userYearInt = Integer.parseInt(yearTextField.getText(), 10); 

 if (userYearInt > 1581) 

 userYear = userYearInt; 





 repaint(); 

 return true; 

 } 

 } 



 return false; 

 } 



int NumberRowsNeeded(int year, int month) 



 { 

 int firstDay; 

 int numCells; 



 if (year < 1582) return (-1); 





 if ((month < 0) || (month > 11)) return (-1); 




 firstDay = CalcFirstOfMonth(year, month); 





 if ((month == FEBRUARY) && (firstDay == 0) && !IsLeapYear(year)) 

 return (4); 



 numCells = firstDay + DaysInMonth[month]; 



 if ((month == FEBRUARY) && (IsLeapYear(year))) numCells++; 



 
 return ((numCells <= 35) ? 5 : 6); 

 } 



int CalcFirstOfMonth(int year, int month) 
 

 { 

 int firstDay; 

 int i;




 if (year < 1582) return (-1); 




 if ((month < 0) || (month > 11)) return (-1); 



 
 firstDay = CalcJanuaryFirst(year); 




 for (i = 0; i < month; i++) 

 firstDay += DaysInMonth[i]; 


 if ((month > FEBRUARY) && IsLeapYear(year)) firstDay++; 




 return (firstDay % 7); 

 } 



boolean IsLeapYear(int year) 


 { 




 if ((year % 100) == 0) return((year % 400) == 0); 



 return ((year % 4) == 0); 

 } 



int CalcJanuaryFirst(int year) 



 { 



 

 if (year < 1582) return (-1); 



 return ((5 + (year - 1582) + CalcLeapYears(year)) % 7); 

 } 



int CalcLeapYears(int year) 


 { 

 int leapYears;  

 int hundreds;

 int fourHundreds; 



 

 if (year < 1582) return (-1); 



 leapYears = (year - 1581) / 4; 



  hundreds = (year - 1501) / 100; 

 leapYears -= hundreds; 





 fourHundreds = (year - 1201) / 400; 

 leapYears += fourHundreds; 



 return (leapYears); 

 } 



} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -