//有待完善,有点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);
    }
}

Calendar日历小程序的更多相关文章

  1. Java可视化日历(Date类、DATe Format类、Calendar类综合运用),开发可视化日历小程序

    Java时间日期类综合运用,开发可视化日历小程序 由键盘输入指定格式的日期,打印这个月的日历 1.代码 import java.text.DateFormat; import java.text.Pa ...

  2. Java实现日历小程序【代码】

    这个没啥难点的,主要是界面设计吧 界面就是这个样子 运行时请在src同目录下放上我女神的照片 -----------------------------------代码如下-------------- ...

  3. 微信小程序踩坑集合

    1:官方工具:https://mp.weixin.qq.com/debug/w ... tml?t=1476434678461 2:简易教程:https://mp.weixin.qq.com/debu ...

  4. 微信小程序——极点日历使用方法

    极点日历github项目地址 添加至自己的小程序方法 极点日历属性接口文档 代码实例: xml: <calendar calendar-style="calendar" he ...

  5. 微信小程序组件 日历

    js文件 'use strict'; let choose_year = null,   choose_month = null; const conf = {   data: {     hasEm ...

  6. 推荐简约漂亮的小程序插件 calendar

    公司团队制作,主要用于内部使用,觉得这个感觉不错,所以推荐出来,让大家试试~ 日历功能 日历基本功能,自定义样式 先睹为快 使用方法: 1. 在微信小程序管理后台--设置--第三方服务,按 AppID ...

  7. 微信小程序:手写日历组件

    一.前言 最近公司要做一个酒店入住的小程序,不可避免的一定会使用到日历,而小程序没有内置的日历组件.在网上看了一下也没有非常适合需求的日历,于是自己写了一个. 二.代码 1. 原理分析 写一个日历只需 ...

  8. 微信小程序+OLAMI(欧拉蜜)自然语言API接口制作智能查询工具--快递、聊天、日历等

    微信小程序最近比较热门,再加上自然语义理解也越来越被人关注,于是我想赶赶潮流,做一个小程序试试.想来想去快递查询应该是一种比较普遍的需求. 如果你也在通过自然语言接口做点什么,希望我的这篇博客能帮到你 ...

  9. 微信小程序横版日历,tab栏

    代码地址如下:http://www.demodashi.com/demo/14243.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

随机推荐

  1. Total Commander 常用快捷键(并附快捷键大全)

    Total Commander 常用快捷键 喜欢用Total Commander的人,都会记住它的一些快捷键,这会给你的操作带来很大的方便,以下是经常会用到的快捷键,大家可以记住一些自己用得最多的操作 ...

  2. Final-阶段站立会议3

    组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...

  3. oracle 11g 通过在线重定义方式修改表结构

    今天因为要对一套数据库的数据抽取进行io优化,希望通过修改表结构将抽取io降下来,因为抽取只针对标签HAVE_FLAG为"0"的值进行抽取,抽取之后更新HAVE_FLAG为其他值, ...

  4. Tomcat负载均衡配置-未完成

    集群技术是目前非常流行的提高系统服务能力与高可靠性( HA- High Availability )的手段,通过把多个独立的服务器组成一个集群可以实现失效无缝转移.也就是说当有某一台集群中的服务器当机 ...

  5. JS小游戏-蓝色拼图

    // a[href=#viewSource]"); //查看源代码标签 viewSourceArr.attr("title", "查看源代码"); v ...

  6. IOS移动设备处理器指令集 armv6、armv7、armv7s及arm64

    Arm处理器,因为其低功耗和小尺寸而闻名,几乎所有的手机处理器都基于arm,其在嵌入式系统中的应用非常广泛,它的性能在同等功耗产品中也很出色. Armv6.armv7.armv7s.arm64都是ar ...

  7. Oracle序列和索引

    序列和索引 一.序列 1.序列的概念: 序列(Sequence)是用来生成连续的整数数据的对象.它常常用来作为主键的增长列,可以升序,也可以降序. 2.创建序列: 语法:创建序列           ...

  8. iScroll.js几个问题及其解决办法

    1.在一个页面中需要点击tab切换,而且每个切换的内容都需要下拉刷新加载,这个时候需要在点击的时候用到myScroll.refresh();这个函数,刷新iScroll.js这个函数. 2.在页面中有 ...

  9. 谈谈Linux下动态库查找路径的问题 ldconfig LD_LIBRARY_PATH PKG_CONFIG_PATH

    谈谈Linux下动态库查找路径的问题 ldconfig LD_LIBRARY_PATH  PKG_CONFIG_PATH 转载自:http://blog.chinaunix.net/xmlrpc.ph ...

  10. sql操作之修改表结构

    修改表的语法=========================增加列[add 列名]=========================①alter table 表名 add 列名 列类型 列参数[加的 ...