📄 calendar_32_1.java
字号:
import java.applet.*;
import java.awt.*;
import java.util.Date;
public class Calendar_32_1 extends Applet
{
Graphics g;
Font font;
FontMetrics font_metrics;
Date current_date,month_to_show;
//Date current_date;
int width,height,year,month;
//int width,height,year,month,date;
int date,hour,minute,second;
public void init()
{
g=getGraphics();
current_date=new Date();
year=current_date.getYear();
month=current_date.getMonth();
//date=current_date.getDay();
date=current_date.getDate();
hour=current_date.getHours();
minute=current_date.getMinutes();
second=current_date.getSeconds();
month_to_show=new Date(year,month,1);
Panel p=new Panel();
add(p,"North");
p.setBackground(Color.yellow);
Button b1=new Button("<");
p.add(b1);
b1.setBackground(Color.yellow);
Button b2=new Button(">");
p.add(b2);
b2.setBackground(Color.yellow);
}
public boolean action(Event e,Object o)
{
if(o.equals("<"))
{
if(--month<0)
{
month=11;
year--;
}
}
else
{
if(++month>11)
{
month=1;
year++;
}
}
/*year=current_date.getYear();
month=current_date.getMonth();
date=current_date.getDay();*/
month_to_show=new Date(year,month,1);
repaint();
return true;
}
String day_of_week(int day)
{
switch(day)
{
case 0:return("Sun");
case 1:return("Mon");
case 2:return("Tue");
case 3:return("Wed");
case 4:return("Thu");
case 5:return("Fri");
default:return("Sat");
}
}
String month_name(int month)
{
switch(month)
{
case 0:return("January");
case 1:return("February");
case 2:return("March");
case 3:return("April");
case 4:return("May");
case 5:return("June");
case 6:return("July");
case 7:return("August");
case 8:return("September");
case 9:return("October");
case 10:return("November");
default:return("December");
}
}
int number_of_days(int month,int year)
{
switch(month+1)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:return(31);
case 4:case 6:case 9:case 11:return(30);
default:
if((year%4==0&year%100!=0)||(year%400==0))return(29);
else return(28);
/*if(year%4!=0)return(28);
else if(year%100!=0)return(29);
else if(year%400!=0)return(28);
else return(29);*/
}
}
public void paint(Graphics g)
{
width=size().width;
height=size().height;
/*if(width<height)
height=width;
else width=height;*/
g.setColor(Color.blue);
//g.fillRect(0,0,size().width,size().height);
g.fillRect(0,0,width,height);
g.setColor(Color.white);
for(int i=2;i<9;i++)
{
int y=(height*i)/8;
g.drawLine(0,y,width,y);
}
int y=height/4;
for(int i=0;i<8;i++)
{
int x=(width*i)/7;
g.drawLine(x,y,x,height);
}
font=new Font("TimesRoman",Font.BOLD,height/20);
g.setFont(font);
font_metrics=g.getFontMetrics();
/*g.drawString(month_name(current_date.getMonth())+" "+
(1900+current_date.getYear()),0,height/8);*/
g.drawString(month_name(month_to_show.getMonth())+" "+
(1900+month_to_show.getYear()),0,height/8);
g.drawString("今天此时的时间是 "+hour+":"+minute+":"+second,width*3/5,height/8);
//y=height/4;
for(int i=0;i<7;i++)
g.drawString(day_of_week(i),(width*i)/7,y);
int first_day=month_to_show.getDay();
//int first=current_date.getDay();
int last_day=number_of_days(month_to_show.getMonth(),month_to_show.getYear());
//int last=number_of_days(current_date.getMonth(),current_date.getYear());
y=(height*11)/32;
for(int i=1;i<=last_day;i++)
{
if(date==i)
{
g.setColor(Color.green);
g.setFont(new Font("TimesRoman",Font.BOLD,height/10));
}
else
{
g.setColor(Color.white);
g.setFont(new Font("TimesRoman",Font.BOLD,height/20));
}
g.drawString(""+i,(width*first_day)/7,y);
if(++first_day>6)
{
first_day=0;
y+=height/8;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -