您好,登錄后才能下訂單哦!
今天小編給大家分享一下java如何實現(xiàn)日歷窗口小程序的相關知識點,內(nèi)容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
標簽:java基礎
功能:
1.按月翻頁。
2.輸入月份年份,直接跳轉(zhuǎn)。
效果圖:
//CalendarMainClass.java public class CalendarMainClass{ public static void main(String args[]){ CalendarFrame frame = new CalendarFrame(); frame.setBounds(100, 100, 360, 300); frame.setVisible(true); frame.setYearAndMonth(2017, 12); } }
//CalendarBean.java import java.util.Calendar; public class CalendarBean{ int year = 2017, month = 12; 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]; //日歷最多可達6行 Calendar rili = Calendar.getInstance(); rili.set(year, month - 1, 1); //模擬翻日歷 int weekDay = rili.get(Calendar.DAY_OF_WEEK) - 2; ///計算出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; } //if(weekDay == 0) weekDay += 7; for(int i = 0; i < weekDay; i++) a[i] = " "; //日歷順序輸出,格式控制 for(int i = weekDay, n = 1; i < weekDay + day; i++){ a[i] = String.valueOf(n); n++; } for(int i = weekDay + day; i < a.length; i++) a[i] = " "; return a; } }
//CalendarFrame.java import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class CalendarFrame extends JFrame implements ActionListener{ JLabel labelDay[] = new JLabel[42]; JButton titleName [] = new JButton[7]; String name [] = {"一", "二", "三", "四", "五", "六", "日"}; JButton nextMonth, previousMonth, go; JTextField textYear, textMonth; CalendarBean calendar; JLabel showMessage = new JLabel("", JLabel.CENTER); int year = 2017, month = 12; 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.RAISED)); pCenter.add(labelDay[i]); } calendar = new CalendarBean(); nextMonth = new JButton("next"); previousMonth = new JButton("previous"); go = new JButton("goto"); textYear = new JTextField(4); textMonth = new JTextField(2); nextMonth.addActionListener(this); previousMonth.addActionListener(this); go.addActionListener(this); JPanel pNorth = new JPanel(), pSouth = new JPanel(); pNorth.add(previousMonth); pNorth.add(showMessage); pNorth.add(nextMonth); pSouth.add(textYear); pSouth.add(textMonth); pSouth.add(go); 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++){ if(i % 7 == 0) System.out.println(""); System.out.printf("%4s", day[i]); } */ for(int i = 0; i < 42; i++) labelDay[i].setText(day[i]); showMessage.setText(calendar.getYear() + "/" + calendar.getMonth()); } public void actionPerformed(ActionEvent e){ if(e.getSource() == nextMonth){ //下一個月 month += 1; if(month > 12){ year += 1; month = 1; } 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 -= 1; if(month < 1){ year -= 1; month = 12; } 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() == go){ //跳轉(zhuǎn) year = Integer.parseInt(textYear.getText().trim()); month = Integer.parseInt(textMonth.getText().trim()); 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()); } }
以上就是“java如何實現(xiàn)日歷窗口小程序”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。