您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)如何在Java項目中實現(xiàn)一個日歷功能,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
CalendarFrame.java
import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.BevelBorder; import javax.swing.border.SoftBevelBorder; public class CalendarFrame extends JFrame implements ActionListener{ /** * @author Nut * 2016.01.13 */ private static final long serialVersionUID = -7260798316896145633L; JLabel labelDay[] = new JLabel[42]; JButton titleName[] = new JButton[7]; String name[]={"日","一","二","三","四","五","六"}; JButton nextMonth,previousMonth; JComboBox choiceYear,choiceMonth; Calendarbean calendar; JLabel showYear,showMonth; JLabel showmessage=new JLabel("",JLabel.CENTER); int year = 2011,month=2; //構(gòu)造方法初始化界面 public CalendarFrame(){ JPanel pCenter = new JPanel(); pCenter.setLayout(new GridLayout(7,7)); //星期欄 for(int i=0;i<7;i++){ titleName[i]=new JButton(name[i]); titleName[i].setBorder(new SoftBevelBorder(BevelBorder.RAISED)); pCenter.add(titleName[i]); } //日期欄 for(int i=0;i<42;i++){ labelDay[i]=new JLabel("",JLabel.CENTER); labelDay[i].setBorder(new SoftBevelBorder(BevelBorder.LOWERED)); pCenter.add(labelDay[i]); } //年月選擇欄 choiceYear=new JComboBox(); choiceMonth=new JComboBox(); showYear=new JLabel("年"); showMonth=new JLabel("月 "); for(int i=1990;i<2050;i++) choiceYear.addItem(i); choiceYear.addActionListener(this); for(int i=1;i<=12;i++) choiceMonth.addItem(i); choiceMonth.addActionListener(this); calendar=new Calendarbean(); nextMonth=new JButton("下月"); previousMonth=new JButton("上月"); nextMonth.addActionListener(this); previousMonth.addActionListener(this); JPanel pNorth=new JPanel(), pSouth=new JPanel(); pNorth.add(choiceYear); pNorth.add(showYear); pNorth.add(choiceMonth); pNorth.add(showMonth); pNorth.add(previousMonth); pNorth.add (nextMonth); pSouth.add(showmessage); add(pCenter,BorderLayout.CENTER); add(pNorth,BorderLayout.NORTH); add(pSouth,BorderLayout.SOUTH); setYearAndMonth(year,month); setDefaultCloseOperation(DISPOSE_ON_CLOSE); } public void setYearAndMonth(int y,int m){ calendar.setYear(y); calendar.setMonth(m); String day[]=calendar.getCalendar(); for(int i=0;i<42;i++) labelDay[i].setText(day[i]); SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 EEEE");//設(shè)置日期格式 showmessage.setText("系統(tǒng)時間:"+df.format(new Date())); } //事件動作 public void actionPerformed(ActionEvent e){ if(e.getSource()==nextMonth){ month=month +1; if(month>12) month=1; calendar.setMonth(month); choiceMonth.setSelectedItem(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; calendar.setMonth(month); choiceMonth.setSelectedItem(month); String day[]=calendar.getCalendar(); for(int i=0;i<42;i++){ labelDay[i].setText(day[i]); } } //選擇年份 else if (e.getSource()==choiceYear){ calendar.setYear((Integer) choiceYear.getSelectedItem()); String day[]=calendar.getCalendar(); for(int i=0;i<42;i++){ labelDay[i].setText(day[i]); } } //選擇月份 else if (e.getSource()==choiceMonth){ calendar.setMonth((Integer) choiceMonth.getSelectedItem()); String day[]=calendar.getCalendar(); for(int i=0;i<42;i++){ labelDay[i].setText(day[i]); } } // showmessage.setText("日歷:"+calendar.getYear()+"年"+calendar.getMonth()+"月"); } }
Calendarbean.java
import java.util.Calendar; public class Calendarbean { String day[]; int year = 2005,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; } }
CalendarMainClass.java
public class CalendarMainClass{ public static void main(String args[]) { CalendarFrame frame = new CalendarFrame(); frame.setBounds(100,100,360,300); frame.setTitle("Java日歷"); frame.setVisible(true); frame.setYearAndMonth(1990,1);//設(shè)置日歷初始值為1990年1月 } }
以上就是如何在Java項目中實現(xiàn)一個日歷功能,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。