📄 calendarframe1.java
字号:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class CalendarFrame1 extends Frame{
//Panel pBar = new PanelBar();
PanelDate pDate = new PanelDate();
Panel pChoice = new PanelChoice(pDate);
public CalendarFrame1(String title){
super(title);
pChoice.setBackground(new Color(200,220,240));
pDate.setBackground(new Color(100,240,225));
this.setLayout(new BorderLayout(10,10));
this.add(pChoice,BorderLayout.NORTH);
this.add(pDate);
this.setBounds(200,400,400,300);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
CalendarFrame1.this.dispose();
}
});
}
public static void main(String[] args){
CalendarFrame1 f = new CalendarFrame1("2000~2020日历");
}
}
//包含两个Choice的面板
class PanelChoice extends Panel{ //implements ItemListener{
Choice yearCh = new Choice();
Choice monthCh = new Choice();
PanelDate pDate;
public PanelChoice(PanelDate pDate){
this.pDate = pDate;
for(int i=2000;i<=2020;i++){
yearCh.add(String.valueOf(i));
}
for(int i=1;i<=12;i++){
monthCh.add(String.valueOf(i));
}
yearCh.select("2008");
monthCh.select("11");
pDate.displayDate(2008,11);
this.add(yearCh);
this.add(new Label("年"));
this.add(monthCh);
this.add(new Label("月"));
ItemListener il = new ItemListener(){
public void itemStateChanged(ItemEvent e){
int year = Integer.parseInt(yearCh.getSelectedItem());
int month = Integer.parseInt(monthCh.getSelectedItem());
PanelChoice.this.pDate.displayDate(year,month);
}
};
yearCh.addItemListener(il);
monthCh.addItemListener(il);
}
}
//日期的面板
class PanelDate extends Panel{
Label[] lBar = new Label[7];
Label[] lDate= new Label[42];
GregorianCalendar gc = new GregorianCalendar();
final String[] WEEK ={"日","一","二","三","四","五","六"};
public PanelDate(){
this.setLayout(new GridLayout(7,7,5,5));
for(int i=0;i<lBar.length;i++){
lBar[i] = new Label(WEEK[i],Label.CENTER);
lBar[i].setBackground(new Color(230,230,200));
this.add(lBar[i]);
}
for(int i=0;i<lDate.length;i++){
lDate[i] = new Label("",Label.CENTER);
this.add(lDate[i]);
}
//this.displayDate(2008,11);
}
public void displayDate(int year,int month){
for(int i=0;i<lDate.length;i++) lDate[i].setText("");//清空
gc.set(year,month-1,1);//gc的值设置为当月第一天
int firstDayLocation = gc.get(Calendar.DAY_OF_WEEK)-1;//判断是星期几
int maxDay = gc.getActualMaximum(Calendar.DAY_OF_MONTH);
//i控制日期,j控制位置,通过lDate下标
for(int i=1,j=firstDayLocation;i<=maxDay;i++,j++)
{
lDate[j].setText(String.valueOf(i));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -