Java實現(xiàn)桌面日歷
本文實例為大家分享了Java實現(xiàn)桌面日歷的具體代碼,供大家參考,具體內(nèi)容如下
問題描述:
編寫一個程序,有一個窗口,該窗口為BorderLayout布局。窗口的中心添加一個Panel容器:pCenter,pCenter的布局是7行7列的GridLayout布局,pCenter的中放置49個標簽,用來顯示日歷。窗口北面添加一個Panel容器pNorth,其布局是FlowLayout布局,pNorth放置兩個按鈕:nextMonth和previousMonth按鈕,單擊nextMonth,可以顯示當前月的下一個月的日歷;單擊previousMonth按鈕,可以顯示當前月的上一個月的日歷。窗口的南面添加一個Panel容器pSouth,其布局是FlowLayout布局,pSouth中放置一個標簽用來顯示一些信息。請完成界面設計和相關(guān)功能。運行結(jié)果如下圖所示。
問題解決:
新建Java項目,在項目下新建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的布局設置為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); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ); ?} }
運行結(jié)果:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot項目對數(shù)據(jù)庫用戶名密碼實現(xiàn)加密過程解析
這篇文章主要介紹了Springboot項目對數(shù)據(jù)庫用戶名密碼實現(xiàn)加密過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06Spring 中使用Quartz實現(xiàn)任務調(diào)度
這篇文章主要介紹了Spring 中使用Quartz實現(xiàn)任務調(diào)度,Spring中使用Quartz 有兩種方式,感興趣的小伙伴們可以參考一下。2017-02-02