Java實(shí)現(xiàn)桌面日歷
本文實(shí)例為大家分享了Java實(shí)現(xiàn)桌面日歷的具體代碼,供大家參考,具體內(nèi)容如下
問題描述:
編寫一個程序,有一個窗口,該窗口為BorderLayout布局。窗口的中心添加一個Panel容器:pCenter,pCenter的布局是7行7列的GridLayout布局,pCenter的中放置49個標(biāo)簽,用來顯示日歷。窗口北面添加一個Panel容器pNorth,其布局是FlowLayout布局,pNorth放置兩個按鈕:nextMonth和previousMonth按鈕,單擊nextMonth,可以顯示當(dāng)前月的下一個月的日歷;單擊previousMonth按鈕,可以顯示當(dāng)前月的上一個月的日歷。窗口的南面添加一個Panel容器pSouth,其布局是FlowLayout布局,pSouth中放置一個標(biāo)簽用來顯示一些信息。請完成界面設(shè)計(jì)和相關(guān)功能。運(yùn)行結(jié)果如下圖所示。

問題解決:
新建Java項(xiàng)目,在項(xiàng)目下新建package,命名為Calendar。
CalendarBean.java
package Calendar;
?
import java.util.Calendar;
public class CalendarBean
{?
? String ?day[];
? int year=2018,month=0;
? public void setYear(int year)
? { ? ?
?? ? ?this.year=year;
? }
? public int getYear()
? { ??
? ? ? return year;
? }
? public void setMonth(int month)
? { ? ?
?? ? ?this.month=month;
? }
? public int getMonth()
? { ? ?
?? ? ?return month;
? }
? public String[] getCalendar()
? { ? String a[]=new String[42]; ? ?
? ? ? Calendar 日歷=Calendar.getInstance();
? ? ? 日歷.set(year,month-1,1); ? ? ? ? ? ?
? ? ? int 星期幾=日歷.get(Calendar.DAY_OF_WEEK)-1;
? ? ? int day=0;
? ? ?if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
? ? ? { ?
? ? ?? ? day=31;
? ? ? }
? ? if(month==4||month==6||month==9||month==11)
? ? ? { ?
? ? ?? ?day=30;
? ? ? }
? ? if(month==2)
? ? ?{ ?if(((year%4==0)&&(year%100!=0))||(year%400==0))
? ? ? ? ? { ??
? ? ?? ? day=29;
? ? ? ? ? }
? ? ? ? else
? ? ? ? ? { ??
? ? ? ? ?? ?day=28;
? ? ? ? ? }
? ? ?}
? ? for(int i=星期幾,n=1;i<星期幾+day;i++)
? ? ?{
? ? ? ? a[i]=String.valueOf(n) ;
? ? ? ? n++;
? ? ?}?
? ? ?return a;
? }
}CalendarFrame.java
package Calendar;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class CalendarFrame extends Frame implements ActionListener
{ ? ?
?? ?Label labelDay[]=new Label[42];
? ? ?Button titleName[]=new Button[7];
? ? ?String name[]={"日","一","二","三", "四","五","六"};
? ? ?Button nextMonth,previousMonth;
? ? ?int year=2020,month=5;
? ? ?CalendarBean calendar;
? ? ?Label showMessage=new Label("",Label.CENTER);
? ? ?public CalendarFrame()
? ? ?{ ?Panel pCenter=new Panel();
? ? ? ?pCenter.setLayout(new GridLayout(7,7)); //將pCenter的布局設(shè)置為7行7列的GridLayout 布局。
? ? ? ? for(int i=0;i<7;i++)
? ? ? ? { ?
? ? ? ? ?? ?titleName[i]=new Button(name[i]);
? ? ? ? ? ? pCenter.add(titleName[i]);//pCenter添加組件titleName[i]。
? ? ? ? }
? ? ? ? for(int i=0;i<42;i++)
? ? ? ? {
? ? ? ? ? ?labelDay[i]=new Label("",Label.CENTER);
? ? ? ? ? ?pCenter.add(labelDay[i]);//pCenter添加組件labelDay[i]。
? ? ? ? }
? ? ? ? calendar=new ?CalendarBean();
? ? ? ? calendar.setYear(year);
? ? ? ? calendar.setMonth(month);
? ? ? ? String day[]=calendar.getCalendar();
? ? ? ? for(int i=0;i<42;i++)
? ? ? ? { ?
? ? ? ? ?? ?labelDay[i].setText(day[i]);
? ? ? ? }
? ? ? ? nextMonth=new Button("下月");
? ? ? ? previousMonth=new Button("上月");
? ? ? ? nextMonth.addActionListener(this);
? ? ? ? previousMonth.addActionListener(this);
? ? ? ? Panel pNorth=new Panel(),
? ? ? ? ? ? ? pSouth=new Panel();
? ? ? ? pNorth.add(previousMonth);
? ? ? ? pNorth.add(nextMonth);
? ? ? ? pSouth.add(showMessage);
? ? ? ? showMessage.setText("日歷:"+calendar.getYear()+"年"+ calendar.getMonth()+"月" );
? ? ? ? ScrollPane scrollPane=new ScrollPane();
? ? ? ? scrollPane.add(pCenter);
? ? ? ? add(scrollPane,BorderLayout.CENTER);// 窗口添加scrollPane在中心區(qū)域
? ? ? ? add(pNorth,BorderLayout.NORTH);// ?窗口添加pNorth 在北面區(qū)域
? ? ? ? add(pSouth,BorderLayout.SOUTH);// 窗口添加pSouth 在南區(qū)域。
? ? ?}
? ? ?public void actionPerformed(ActionEvent e)
? ? ?{ ?if(e.getSource()==nextMonth)
? ? ? ? { month=month+1;
? ? ? ? ? if(month>12) {
? ? ? ? ? ? ? month=1;
? ? ? ? ? ? ? year++;
? ? ? ? ? }
? ? ? ? ? calendar.setYear(year);
? ? ? ? ? calendar.setMonth(month);
? ? ? ? ? String day[]=calendar.getCalendar();
? ? ? ? ? for(int i=0;i<42;i++)
? ? ? ? ? ?{?
? ? ? ? ?? ? ?labelDay[i].setText(day[i]);
? ? ? ? ? ?}
? ? ? ? }
? ? ? ?else if(e.getSource()==previousMonth)
? ? ? ? { month=month-1;
? ? ? ? ?if(month<1) {
? ? ? ? ? ? ? month=12;
? ? ? ? ? ? ? year--;
? ? ? ? ?}
? ? ? ? ? calendar.setYear(year);
? ? ? ? ? calendar.setMonth(month);
? ? ? ? ? String day[]=calendar.getCalendar();
? ? ? ? ? ?for(int i=0;i<42;i++)
? ? ? ? ? ?{ ?
? ? ? ? ?? ? ? labelDay[i].setText(day[i]);
? ? ? ? ? ?}
? ? ? ? }
? ? ? ?showMessage.setText("日歷:"+calendar.getYear()+"年"+calendar.getMonth()+"月" );
? ? ?}
}CalendarMainClass.java
package Calendar;
public class CalendarMainClass
{?
?? ?public static void main(String args[])
?{ ??
?? ?CalendarFrame frame=new CalendarFrame();
? ? ?frame.setBounds(100,100,360,300);
? ? ?frame.setVisible(true);
? ? ?frame.validate();
? ? ?frame.addWindowListener(new java.awt.event.WindowAdapter()
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ?public void windowClosing(java.awt.event.WindowEvent e)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ?System.exit(0);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? );
?}
}運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot項(xiàng)目對數(shù)據(jù)庫用戶名密碼實(shí)現(xiàn)加密過程解析
這篇文章主要介紹了Springboot項(xiàng)目對數(shù)據(jù)庫用戶名密碼實(shí)現(xiàn)加密過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
java使用@Transactional時(shí)常犯的N種錯誤
@Transactional是我們在用Spring時(shí)候幾乎逃不掉的一個注解,本文主要介紹了使用?@Transactional?時(shí)常犯的N種錯誤,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Redis實(shí)現(xiàn)延遲隊(duì)列的全流程詳解
Redisson是Redis服務(wù)器上的分布式可伸縮Java數(shù)據(jù)結(jié)構(gòu),這篇文中主要為大家介紹了Redisson實(shí)現(xiàn)的優(yōu)雅的延遲隊(duì)列的方法,需要的可以參考一下2023-03-03
Mybatis配置之typeAlias標(biāo)簽的用法
這篇文章主要介紹了Mybatis配置之typeAlias標(biāo)簽的用法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Spring 中使用Quartz實(shí)現(xiàn)任務(wù)調(diào)度
這篇文章主要介紹了Spring 中使用Quartz實(shí)現(xiàn)任務(wù)調(diào)度,Spring中使用Quartz 有兩種方式,感興趣的小伙伴們可以參考一下。2017-02-02

