java實現(xiàn)日歷窗口小程序
本文實例為大家分享了java實現(xiàn)日歷窗口小程序的具體代碼,供大家參考,具體內(nèi)容如下
標(biāo)簽:java基礎(chǔ)
功能:
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()); ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA導(dǎo)入JDBC驅(qū)動的jar包步驟詳解
JDBC是一種底層的API,是連接數(shù)據(jù)庫和Java應(yīng)用程序的紐帶,因此我們在訪問數(shù)據(jù)庫時需要在業(yè)務(wù)邏輯層中嵌入SQL語句,這篇文章主要介紹了IDEA導(dǎo)入JDBC驅(qū)動的jar包,需要的朋友可以參考下2023-07-07Spring中propagation的7種事務(wù)配置及說明
這篇文章主要介紹了Spring中propagation的7種事務(wù)配置及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06SpringSecurity6.x多種登錄方式配置小結(jié)
SpringSecurity6.x變了很多寫法,本文就來介紹一下SpringSecurity6.x多種登錄方式配置小結(jié),具有一定的參考價值,感興趣的可以了解一下2023-12-12spring cloud中微服務(wù)之間的調(diào)用以及eureka的自我保護機制詳解
這篇文章主要介紹了spring cloud中微服務(wù)之間的調(diào)用以及eureka的自我保護機制詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07解決IDEA錯誤 Cause: java.sql.SQLException: The server time zone
這篇文章主要介紹了解決IDEA錯誤 Cause: java.sql.SQLException: The server time zone value的問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08SpringBoot集成EasyExcel的應(yīng)用場景分析
這篇文章主要介紹了SpringBoot集成EasyExcel的應(yīng)用場景,java領(lǐng)域解析、生成excel比較有名的框架有apache poi、jxl等,今天通過實例代碼給大家詳細介紹,需要的朋友可以參考下2021-07-07Java List集合返回值去掉中括號(''[ ]'')的操作
這篇文章主要介紹了Java List集合返回值去掉中括號('[ ]')的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08spring?boot如何配置靜態(tài)路徑詳解(404出現(xiàn)的坑)
這篇文章主要給大家介紹了關(guān)于spring?boot如何配置靜態(tài)路徑的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-02-02