📄 mycalendar.java
字号:
import java.awt.*;
import java.awt.event.*;
public class MyCalendar extends Frame implements ActionListener, ItemListener{
boolean IsLeapYear;
static int Year=2000; static int Month=1; int Days;
int TotalDays=0; int BeforeDays = 0; int FirstDayOfMonth=0;
Choice YearChoice = new Choice();
Choice MonthChoice = new Choice();
Label YearLabel = new Label("年");
Label MonthLabel = new Label("月");
Label sun=new Label("SUN");
Label mon=new Label("MON");
Label tue=new Label("TUE");
Label wed=new Label("WED");
Label thu=new Label("THU");
Label fri=new Label("FRI");
Label sat=new Label("SAT");
Panel panel=new Panel(new GridLayout(6,7));
Label[] l=new Label[42];
public MyCalendar(String title){
super(title);
for(int i=0;i<42;i++){
l[i]=new Label("");
panel.add(l[i]);
}
String[] Year=new String[10];
for(int i=0;i<10;i++){
Year[i]=String.valueOf(i+2000);
YearChoice.add(Year[i]);
}
String[] Month={"1","2","3","4","5","6","7","8","9","10","11","12"};
for(int i=0;i<Month.length;i++){
MonthChoice.add(Month[i]);
}
YearChoice.addItemListener(this);
MonthChoice.addItemListener(this);
setLayout(null);
YearChoice.setBounds(50,40,60,60);
YearLabel.setBounds(110,43,15,15);
add(YearChoice); add(YearLabel);
MonthChoice.setBounds(170,40,60,60);
MonthLabel.setBounds(230,43,15,15);
add(MonthChoice); add(MonthLabel);
sun.setBounds(20,70,30,20);
mon.setBounds(60,70,30,20);
tue.setBounds(100,70,30,20);
wed.setBounds(140,70,30,20);
thu.setBounds(180,70,30,20);
fri.setBounds(220,70,30,20);
sat.setBounds(260,70,30,20);
add(sun); add(mon); add(tue); add(wed); add(thu); add(fri); add(sat);
panel.setBounds(25,95,280,140);
panel.setVisible(true);
add(panel);
addWindowListener(new WindowAdapter (){
public void windowClosing(WindowEvent e){
MyCalendar.this.dispose();
}
});
}
public void PrintCalendar(int Year,int Month){
if (Year % 4 == 0 && !(Year % 100 == 0) || Year % 400 == 0){
IsLeapYear = true; // 闰年
}
else{
IsLeapYear = false;// 平年
}
for (int i = 1900; i < Year; i++) {
if (i % 4 == 0 && !(i % 100 == 0) || i % 400 == 0) TotalDays = TotalDays + 366; // 闰年366天
else TotalDays = TotalDays + 365; // 平年365天
}
for (int i = 1; i <= Month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: Days = 31; break;
case 2: if (IsLeapYear) Days = 29;
else Days = 28;
break;
default: Days = 30; break;
}
if (i < Month) {
BeforeDays = BeforeDays + Days;
}
}
TotalDays = TotalDays + BeforeDays;
int Remainder=1+TotalDays%7;
if(Remainder==7) FirstDayOfMonth=0;
else FirstDayOfMonth=Remainder;
for(int i=0;i<FirstDayOfMonth;i++)
l[i].setText("");
for(int i=0;i<Days;i++)
l[i+FirstDayOfMonth].setText(String.valueOf(i+1));
for(int i=41;i>=Days+FirstDayOfMonth;i--)
l[i].setText("");
TotalDays=0; FirstDayOfMonth=0; BeforeDays = 0;
}
public void actionPerformed(ActionEvent e){}
public void itemStateChanged(ItemEvent e){
if(e.getSource()==MonthChoice) {
Month=Integer.parseInt(MonthChoice.getSelectedItem());
PrintCalendar(Year,Month);
System.out.println(Month);
}
if(e.getSource()==YearChoice){
Year=Integer.parseInt(YearChoice.getSelectedItem());
PrintCalendar(Year,Month);
System.out.println(Year);
}
}
public static void main(String[] args)throws Exception{
MyCalendar frame=new MyCalendar("2000年-2010年日历");
frame.setSize(310,250);
frame.setVisible(true);
frame.PrintCalendar(Year,Month);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -