進口?Java . awt . borderlayout;
進口?Java . awt . color;
進口?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(顏色。紅色);
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.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?月)?{?
切換?(月)?{?
案子?3:?
案子?5:?
案子?7:
案子?8:?
案子?10:?
回歸?31;?
案子?1:?
如果?((年份% 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]。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]。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。中心);
if((i+1)%7==0?||?(i+1)%7==1?||?(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]。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]。setForeground(顏色。灰色);
jl[i]。set border(border factory . createetchedborder());
jp3 . add(JL[I]);
}
}
}運行結果: