用java编程实现2016 12的日历。

用Java实现的日历程序如下(图形界面程序)

进口?Java . awt . borderlayout;

进口?Java . awt . color;

进口?Java . awt . font;

进口?Java . awt . gridlayout;

进口?Java . awt . event . action event;

进口?Java . awt . event . action listener;

进口?java.util .日历;

进口?javax . swing . border factory;

进口?javax . swing . jbutton;

进口?javax . swing . jframe;

进口?javax . swing . jlabel;

进口?javax . swing . jpanel;

公共?班级?CCI?延伸?JFrame?器物?ActionListener{

JButton?JB 1 =新?JButton(" & lt;& lt”);

JButton?jb2 =新?JButton("<);

JButton?jb3 =新?JButton(" >);

JButton?jb4 =新?JButton(" & gt;>);

JPanel?jp 1 =新?JPanel();

JPanel?jp2 =新?JPanel();

JPanel?jp3 =新?JPanel();

JPanel?jp4 =新?JPanel();

JLabel?JL 1 =新?JLabel();

JLabel?jl2 =新?JLabel();

JLabel[]jl=new?JLabel[49];

字符串?[]week={"Sun "," Mon "," Tue "," Wed "," Thu "," Fri "," Sat " };

日历?c = calendar . getinstance();

int?年、月、日;

int?现在年,现在月,现在日;

CCI(){

超级(“简易日历”);

nowyear=c.get(日历。年份);

nowmonth=c.get(日历。月)+1;

nowday=c.get(日历。月日);

year = nowyear

month = nowmonth

day = nowday

字符串?s =年+“年”+月+“月”;

jl1.setForeground(颜色。红色);

jl1.setFont(新?字体(空,字体。粗体,20));

JL 1 . settext(s);

JB 1 . addactionlistener(this);

jb2 . addactionlistener(this);

jb3 . addactionlistener(this);

jb4 . addactionlistener(this);

jp 1 . add(JB 1);jp 1 . add(jb2);jp 1 . add(JL 1);jp 1 . add(jb3);jp 1 . add(jb4);

jp2 . set layout(null);

createMonthPanel();

jp2 . add(jp3);

jl2.setFont(新?字体(空,字体。粗体,20));

Jl2.setText("今天是"+nowear+"年"+nowmonth+"月"+nowday+"日");

jp4 . add(jl2);

add(jp1,BorderLayout。北);

添加(jp2,BorderLayout。中心);

添加(jp4,BorderLayout。南);

setSize(500,500);

setDefaultCloseOperation(JFrame。EXIT _ ON _ CLOSE);

setLocationRelativeTo(null);

set visible(true);

}

@覆盖

公共?作废?actionPerformed(ActionEvent?ae)?{

if(ae.getSource()==jb1){

年份=年份-1;

字符串?s =年+“年”+月+“月”;

JL 1 . settext(s);

jp3 . remove all();

createMonthPanel();

jp3 . validate();

}

if(ae.getSource()==jb2){

if(month==1){

年份=年份-1;

月= 12;

}否则{

月=月-1;

}

字符串?s =年+“年”+月+“月”;

JL 1 . settext(s);

jp3 . remove all();

createMonthPanel();

jp3 . validate();

}

if(ae.getSource()==jb3){

if(month==12){

年份=年份+1;

月= 1;

}否则{

月=月+1;

}

字符串?s =年+“年”+月+“月”;

JL 1 . settext(s);

jp3 . remove all();

createMonthPanel();

jp3 . validate();

}

if(ae.getSource()==jb4){

年份=年份+1;

字符串?s =年+“年”+月+“月”;

JL 1 . settext(s);

jp3 . remove all();

createMonthPanel();

jp3 . validate();

}

}

公共?静电?作废?main(String[]?args)?{

新的?CCI();

}

公共?int?getMonthDays(int?年份,?int?月)?{?

切换?(月)?{

案子?1:?

案子?3:?

案子?5:?

案子?7:

案子?8:?

案子?10:?

案子?12:

回归?31;?

案子?2:?

如果?((年份% 4 = = 0 & amp& amp年份%100!= 0)| |年%400==0)?{?

回归?29;?

}?不然呢?{?

回归?28;?

}?

默认:?

回归?30;?

}?

}?

公共?作废?createMonthPanel(){

c.set(年份,?月-1,?getMonthDays(年,月));

int?weekOfMonth=c.get(日历。月的周);

if(weekOfMonth==6){

jp3.setLayout(新?GridLayout(7,7));

jp3.setBounds(50,?20,?420,?350);

}否则{

jp3.setLayout(新?GridLayout(6,7));

jp3.setBounds(50,?20,?420,?300);

}

jp3 . set border(border factory . createetchedborder());

for(int?I = 0;我& lt7;i++){

jl[i]=new?JLabel(周[i],JLabel。中心);

jl[i]。setFont(新?字体(空,字体。粗体,20));

jl[i]。set border(border factory . createetchedborder());

jp3 . add(JL[I]);

}

c.set(年份,?月-1,?1);

int?emptyFirst=c.get(日历。星期几)-1;

int?daysOfMonth=getMonthDays(年,月);

for(int?I = 6+empty first;我& gt=7;我- ){

int?intyear = year

int?intmonth =月;

if(intmonth==1){

int year = int year-1;

int month = 12;

}否则{

int month = int month-1;

}

int?intdays=getMonthDays(intyear,int month);

jl[i]=new?JLabel((intdays+7-i)+" ",JLabel。中心);

jl[i]。setFont(新?字体(空,字体。粗体,20));

jl[i]。setForeground(颜色。灰色);

jl[i]。set border(border factory . createetchedborder());

jp3 . add(JL[I]);

}

for(int?I = 7+empty first;我& ltdays of month+7+empty first;i++){

jl[i]=new?JLabel((I-7-empty first+1)+" ",JLabel。中心);

jl[i]。setFont(新?字体(空,字体。粗体,20));

if((i+1)%7==0?||?(i+1)%7==1){

jl[i]。setForeground(颜色。红色);

}别的?if((I-7-empty first+1)= = now day & amp;& ampmonth = = nowmonth & amp& ampyear==nowyear)

jl[i]。setForeground(颜色。蓝色);

其他

jl[i]。setForeground(颜色。黑色);

jl[i]。set border(border factory . createetchedborder());

jp3 . add(JL[I]);

}

if(weekOfMonth==6)

for(int?i = 48我& gt= days ofmonth+empty first+7;我- ){

jl[i]=new?JLabel((49-i)+" ",JLabel。中心);

jl[i]。setFont(新?字体(空,字体。粗体,20));

jl[i]。setForeground(颜色。灰色);

jl[i]。set border(border factory . createetchedborder());

jp3 . add(JL[I]);

}

其他

for(int?I = 41;我& gt= days ofmonth+empty first+7;我- ){

jl[i]=new?JLabel((42-i)+" ",JLabel。中心);

jl[i]。setFont(新?字体(空,字体。粗体,20));

jl[i]。setForeground(颜色。灰色);

jl[i]。set border(border factory . createetchedborder());

jp3 . add(JL[I]);

}

}

}运行结果