java實(shí)現(xiàn)日歷窗口小程序
更新時(shí)間:2022年06月13日 10:58:01 作者:李典金
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)日歷窗口小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了java實(shí)現(xiàn)日歷窗口小程序的具體代碼,供大家參考,具體內(nèi)容如下
標(biāo)簽:java基礎(chǔ)
功能:
1.按月翻頁(yè)。
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]; ?//日歷最多可達(dá)6行
? ? ? ? Calendar rili = Calendar.getInstance();
? ? ? ? rili.set(year, month - 1, 1); ?//模擬翻日歷
? ? ? ? int weekDay = rili.get(Calendar.DAY_OF_WEEK) - 2; ?///計(jì)算出1號(hào)的星期
? ? ? ? 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){ ?//下一個(gè)月
? ? ? ? ? ? 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){ ?//上一個(gè)月
? ? ? ? ? ? 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());
? ? }
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis映射和實(shí)際類型不一致的問(wèn)題
這篇文章主要介紹了mybatis映射和實(shí)際類型不一致的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
java數(shù)據(jù)結(jié)構(gòu)圖論霍夫曼樹及其編碼示例詳解
這篇文章主要為大家介紹了java數(shù)據(jù)結(jié)構(gòu)圖論霍夫曼樹及其編碼示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-11-11
IISExpress?配置允許外部訪問(wèn)詳細(xì)介紹
這篇文章主要介紹了?IISExpress?配置允許外部訪問(wèn)詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11
SpringCloud?Eureka應(yīng)用全面介紹
Eureka是Netflix開發(fā)的服務(wù)發(fā)現(xiàn)框架,本身是一個(gè)基于REST的服務(wù),主要用于定位運(yùn)行在AWS域中的中間層服務(wù),以達(dá)到負(fù)載均衡和中間層服務(wù)故障轉(zhuǎn)移的目的2022-09-09

