Calendar日历小程序

//有待完善,有点bug
package com.sunshine.framework.calendar.model;

import java.util.Calendar;

/**
 *
 * <p>
 * 描述该类情况 {@link 代表跟谁有关系}
 * </p>
 *
 * @author 王超
 * @since 1.0
 * @date 2016年10月20日 下午8:19:15
 * @see 新建|修改|放弃
 * @see com.sunshine.framework.calendar.model.CalendarBean
 */

public class CalendarBean {
    String day[];
    int year = 2005, month = 0;

public String[] getCalendar() {
        String a[] = new String[42];
        // 获取日历的实例对象
        Calendar date = Calendar.getInstance();
        // 设置日历日期
        date.set(this.year, this.month - 1, 1);
        // 获取周数
        int week = date.get(Calendar.DAY_OF_WEEK);
        int day = 0;
        // 判断大月份
        if (this.month == 1 || this.month == 3 || this.month == 5 || this.month == 7 || this.month == 8
                || this.month == 10 || this.month == 12) {
            day = 31;
        }
        // 判断小月
        if (this.month == 4 || this.month == 6 || this.month == 9 || this.month == 11) {
            day = 30;
        }
        // 单独判断2月
        if (this.month == 2) {
            // 判断是闰年 能被4或400整除 但不能被100整除
            if ((this.year % 4 == 0) || (this.year % 100 != 0) || (this.year % 400 == 0)) {
                day = 29;
            } else {
                day = 28;
            }
        }
        for (int i = week, n = 1; i < week + day; i++) {
            a[i] = String.valueOf(n);
            n++;
        }
        return a;
    }

public int getMonth() {
        return this.month;
    }

public int getYear() {
        return this.year;
    }

public void setMonth(int month) {
        this.month = month;
    }

public void setYear(int year) {
        this.year = year;
    }

}

package com.sunshine.framework.calendar;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.sunshine.framework.calendar.model.CalendarBean;

/**
 *
 * <p>
 * 描述该类情况 {@link 代表跟谁有关系}
 * </p>
 *
 * @author 王超
 * @since 1.0
 * @date 2016年10月20日 下午8:41:58
 * @see 新建|修改|放弃
 * @see com.sunshine.framework.calendar.CalendarFrame
 */

public class CalendarFrame extends JFrame implements ActionListener {
    /***/
    private static final long serialVersionUID = 1L;
    JButton button = new JButton();
    CalendarBean calendar;
    JLabel labelDay[] = new JLabel[42];
    JLabel lbl1 = new JLabel("请输入年份:");
    JLabel lbl2 = new JLabel("");
    String name[] = { "日", "一", "二", "三", "四", "五", "六" };
    JButton nextMonth, previousMonth;
    JLabel showMessage = new JLabel("", JLabel.CENTER);
    JTextField text = new JTextField(10);
    JButton titleName[] = new JButton[7];
    int year = 1996, month = 1;

public CalendarFrame() {
        JPanel pCenter = new JPanel();
        // 将pCenter的布局设置为7行7列的GridLayout布局
        pCenter.setLayout(new GridLayout(7, 7));
        // pCenter添加组件titleName[i]
        for (int i = 0; i < 7; i++) {
            // 把星期值存入到titleName数组里
            this.titleName[i] = new JButton(this.name[i]);
        }
        // pCenter添加组件labelDay[i]
        for (int i = 0; i < 42; i++) {
            this.labelDay[i] = new JLabel("", JLabel.CENTER);
            pCenter.add(this.labelDay[i]);
        }
        this.text.addActionListener(this);
        this.calendar = new CalendarBean();
        this.calendar.setYear(this.year);
        this.calendar.setMonth(this.month);
        String day[] = this.calendar.getCalendar();
        for (int i = 0; i < 42; i++) {
            this.labelDay[i].setText(day[i]);
        }
        this.nextMonth = new JButton("下月");
        this.previousMonth = new JButton("上月");
        this.button = new JButton("确定");
        // 注册监听器
        this.nextMonth.addActionListener(this);
        this.previousMonth.addActionListener(this);
        this.button.addActionListener(this);
        JPanel pNorth = new JPanel(), pSouth = new JPanel();
        pNorth.add(this.showMessage);
        pNorth.add(this.lbl2);
        pNorth.add(this.previousMonth);
        pNorth.add(this.nextMonth);
        pNorth.add(this.lbl1);
        pNorth.add(this.text);
        pNorth.add(this.button);
        this.showMessage.setText("日历:" + this.calendar.getYear() + "年" + this.calendar.getMonth() + "月");
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.add(pCenter);
        // 窗口添加scrollPane在中心区域
        add(scrollPane, BorderLayout.CENTER);
        // 窗口添加pNorth 在北面区域
        add(pNorth, BorderLayout.NORTH);
        // 窗口添加pSouth 在南区域。
        add(pSouth, BorderLayout.SOUTH);
    }

/*
     * (方法重写)
     *
     * @see
     * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.nextMonth) {
            this.month = this.month + 1;
            if (this.month > 12) {
                this.month = 1;
            }
            this.calendar.setMonth(this.month);
            String day[] = this.calendar.getCalendar();
            for (int i = 0; i < 42; i++) {
                this.labelDay[i].setText(day[i]);
            }
        } else if (e.getSource() == this.previousMonth) {
            this.month = this.month - 1;
            if (this.month < 1) {
                this.month = 12;
            }
            this.calendar.setMonth(this.month);
            String day[] = this.calendar.getCalendar();
            for (int i = 0; i < 42; i++) {
                this.labelDay[i].setText(day[i]);
            }
        } else {
            this.month = this.month + 1;
            if (this.month > 12) {
                this.month = 1;
            }
            this.calendar.setYear(Integer.parseInt(this.text.getText()));
            String day[] = this.calendar.getCalendar();
            for (int i = 0; i < 42; i++) {
                this.labelDay[i].setText(day[i]);
            }
        }
        this.showMessage.setText("日历:" + this.calendar.getYear() + "年" + this.calendar.getMonth() + "月");
    }
}

package com.sunshine.framework.calendar;

import javax.swing.JFrame;
import javax.swing.UIManager;

/**
 *
 * <p>
 * 描述该类情况 {@link 代表跟谁有关系}
 * </p>
 *
 * @author 王超
 * @since 1.0
 * @date 2016年10月20日 下午9:34:15
 * @see 新建|修改|放弃
 * @see com.sunshine.framework.calendar.CalendarMain
 */

public class CalendarMain {
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        CalendarFrame frame = new CalendarFrame();
        frame.setBounds(100, 100, 360, 300);
        frame.setTitle("日历小程序");
        // 窗体居中显示
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

时间: 2024-08-05 03:26:28

Calendar日历小程序的相关文章

阅读项目:微信日历小程序插件

韩颖 1759110 软工一班 我在网上找到了一段别人写的微信小程序代码,主题是日历以及签到.由于市面上的小程序鲜少有签到及积分功能,此款程序内容完备且观点新颖,因此值得我们学习与借鉴. ----------------------------------------------------------- 以下是小程序插件的代码链接 https://www.cnblogs.com/zengxuelan/p/10030598.html -------------------------------

简单的日历小程序

需求:在控制台输入年月(yyyy-MM),则打印出该年该月的日历信息: 分析: 1:计算1900年1月1日距离输入日期的天数,日数可以算作1日 2:总天数对7取余应该是该天所在周数 3:然后根据显示该月的打印信息 具体代码如下: 主方法类: 1 /** 2 * 3 */ 4 package com.hlcui.cal; 5 6 import java.util.Scanner; 7 8 import com.hlcui.util.CalendarUtil; 9 10 /** 11 * @auth

微信小程序-整理各种小程序源码和资料免费下载

微信小程序整理下载 [小程序源码]微信小程序-车源宝微信版 [小程序源码]小程序-微赞社区(论坛demo) [小程序源码]微信小程序-收支账单 [小程序工具]微信小程序-日历 [小程序源码]小程序-在线聊天功能 [小程序源码]微信小程序-大好商城(新增功能天气查询和2048游戏) [小程序源码]微信小程序-查询号码归属地 [小程序源码]微信小程序-备忘录2 [小程序源码]微信小程序-QQ音乐 [小程序源码]小程序-货币汇率 [小程序源码]微信小程序-大学图书馆 [小程序源码]小程序-积分商城 [

微信小程序商店 | 即速商店_二手类小程序

即速应用商店_分类最齐全的小程序商店,收录当前最新最热门小程序,汇集各行业小程序案例及评测,在这里可以轻松找到各种实用好玩的小程序,也可免费发布 自己的小程序获取巨大流量 . PS:关于微信小程序商店_即速商店?   如何免费发布微信小程序,获取流量? 微信小程序商店|即速商店_二手小程序 要求:本次安利的可是二手类福利小程序,专业回收/出售各种闲置商品!注意,想在该类目下提交的微信小程序须具备购物.二手相关属性.如小程序内发布虚假违法信息,由小程序主体承担.点击 > 即刻提交 < 本期二手小

微信小程序商店 | 即速商店_福利类小程序

即速应用商店_分类最齐全的小程序商店,收录当前最新最热门小程序,汇集各行业小程序案例及评测,在这里可以轻松找到各种实用好玩的小程序,也可免费发布 自己的小程序获取 巨大流量 . PS:关于微信小程序商店_即速商店?   如何免费发布微信小程序,获取流量? 微信小程序商店|即速商店_福利小程序 要求:本次安利的可是一堆堆福利小程序,各大电商自有平台优惠券代金券随便领!注意,想在该类目下提交的微信小程序须具备购物.福利相关属性.如小程序内发布虚假违法信息,由小程序主体承担.点击 > 即刻提交 <

微信小程序商店 | 即速商店_商城类小程序

即速应用商店-分类最齐全的小程序商店,收录当前最新最热门小程序,汇集各行业小程序案例及评测,在这里可以轻松找到各种实用好玩的小程序,也可免费发布 自己的小程序获取巨大流量 . PS:关于微信小程序商店_即速商店?   如何免费发布微信小程序,获取流量? 微信小程序商店|即速商店_商城小程序 要求:该类目下提交的微信小程序须具备购物.商城相关属性.如小程序内发布虚假违法信息,由小程序主体承担.点击 > 即刻提交 < 本期商城小程序推荐: ? 女王名品show 测评:女王名品show小程序,免费学

微信小程序商店 | 即速商店_团购类小程序

即速应用商店-分类最齐全的小程序商店,收录当前最新最热门小程序,汇集各行业小程序案例及评测,在这里可以轻松找到各种实用好玩的小程序,也可 免费发布 自己的小程序获取 巨大流量 . PS:关于微信小程序商店_即速商店?   如何免费发布微信小程序,获取流量? 微信小程序商店|即速商店_团购小程序 要求:该类目下提交的微信小程序须具备购物.团购相关属性.如小程序内发布虚假违法信息,由小程序主体承担.点击 > 即刻提交 < 本期团购小程序推荐: ? 网易一起拼lite 测评:网易一起拼小程序只做优质

Java Date类和Calendar类的一个控制台打印日期的小程序

Java Date类和Calendar类的一个打印日期的小程序,可以直接用. package com.boy.Idate.calendar; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 控制台可视化日历 * @author 田硕 */ public class VisualCalendar {

微信小程序下订单插件(日历)

最近做小程序开发,出于练手,也是工作需要,就做了个微信小程序的类似于酒店预订的日历插件.先上图: 这个插件分为上下两部分,上边是tab栏,会根据当前的日期自动定位到当前,并展示以后7天的日期,下边为内容展示,随tab栏变化而变化.思路:首先用`new Data()`时间对象初始化时间,获取当前的日期,用`new Date(Date.UTC(year, month - 1, 1)).getDay()`获取每个月的第一天是星期几. // 计算每月第一天是星期几 function getFirstDa