Java小项目之:教你做电子日历!

Java小项目之:教你做电子日历!
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>日历控件</title>
<script src="js/new_file.js" defer></script>
</head>

<body>
<input id="calendar" type="text" />
</body>
</html>

var days = new Array("日","一","二","三","四","五","六");//星期
var today = new Date();//当天日期,备用
var month_big = new Array("1","3","5","7","8","10","12"); //包含所有大月的数组
var month_small = new Array("4","6","9","11"); //包含所有小月的数组
var separator = "-";//间隔符

var calendar = document.getElementById("calendar");
var cal_parent = calendar.parentNode;//获取父元素
var cal_width = ((calendar.clientWidth<150) ? 150 : calendar.clientWidth);//获取input的宽度,如果input宽度小于150,调整为150,150为日历块最小宽度
var cal_height = calendar.clientHeight;//获取input的高度,整数
var cal_X = calendar.offsetLeft;//获取input左边 距父元素的距离,整数
var cal_Y = calendar.offsetTop;//获取input顶部 距父元素的距离,整数

calendar.style.cursor = "pointer";//将input的鼠标设置成小手
calendar.readOnly = "readOnly";//设置input为只读
calendar.onblur = hideCalendar; //当input失去焦点时,隐藏cal_body
calendar.onclick = showCalendar;//点击input时调用showCalendar函数

//取input宽度的七分之一再减一作为方格的边长
var pane_height = cal_width/7 - 1;

function hideCalendar(){
var cal_body = document.getElementById("cal_body");
if(cal_body != undefined){
cal_body.parentNode.removeChild(cal_body);
}
}

//显示日历主体
function showCalendar(){
var cal_body = document.getElementById("cal_body");
if(cal_body != undefined){
cal_body.parentNode.removeChild(cal_body);
}
else{
var cal_body = document.createElement("DIV");
cal_body.id = "cal_body";
cal_body.style.width = cal_width + "px";
cal_body.style.height = "auto";
cal_body.style.overflow = "hidden";
cal_body.style.position = "absolute";
cal_body.style.zIndex = "9";
cal_body.style.left = cal_X + "px";
cal_body.style.top = (cal_Y + cal_height + 5) + "px";
cal_body.style.border = "solid 1px #CCCCCC";
//鼠标移动到cal_body上时,禁用input的onblur事件,防止cal_body因input失去焦点而被隐藏
cal_body.onmouseover = function(){
calendar.onblur = undefined;
}
//鼠标从cal_body移除时,启用input的onblur事件,并且先让input获得焦点,否则当在cal_body上空白处点击后再移出在其他地方点击时,input因没有焦点而无法触发onblur事件
cal_body.onmouseout = function(){
calendar.focus();
calendar.onblur = hideCalendar;
}
cal_parent.appendChild(cal_body);

    var line1 = document.createElement("DIV");
    line1.style.width = cal_width + "px";
    line1.style.height =  pane_height + "px";
    line1.style.backgroundColor = "#0FF";

    var btn1 = document.createElement("DIV");
    btn1.style.width = (cal_width/3 - 3) + "px";
    btn1.style.height = pane_height + "px";
    btn1.style.lineHeight = pane_height + "px";
    btn1.style.textAlign = "center";
    btn1.innerHTML = "<";
    btn1.style.cursor = "pointer";
    btn1.style.cssFloat = "left";
    btn1.onclick = function(){
        if(isValidated()){
            var old_year = parseInt(document.getElementById("input_year").value);
            if(old_year > 1960){
                var year = old_year - 1;
                var month = parseInt(document.getElementById("input_month").value);
                var val = year + separator + month + separator + 1;
                init(val);
            }
        }
    };
    line1.appendChild(btn1);

    var input_year = document.createElement("INPUT");
    input_year.id = "input_year";
    input_year.style.width = (cal_width/3) + "px";
    input_year.style.height = "70%";
    input_year.style.cssFloat = "left";
    input_year.style.textAlign = "center";
    input_year.onchange = function(){
        changed();
    };
    line1.appendChild(input_year);

    var btn2 = document.createElement("DIV");
    btn2.style.width = (cal_width/3 - 3) + "px";
    btn2.style.height = pane_height + "px";
    btn2.style.lineHeight = pane_height + "px";
    btn2.style.textAlign = "center";
    btn2.innerHTML = ">";
    btn2.style.cursor = "pointer";
    btn2.style.cssFloat = "left";
    btn2.onclick = function(){
        if(isValidated()){
            var old_year = parseInt(document.getElementById("input_year").value);
            if(old_year < 2050){
                var year = old_year + 1;
                var month = parseInt(document.getElementById("input_month").value);
                var val = year + separator + month + separator + 1;
                init(val);
            }
        }
    };
    line1.appendChild(btn2);

    var line2 = document.createElement("DIV");
    line2.style.width = cal_width + "px";
    line2.style.height =  pane_height + "px";
    line2.style.backgroundColor = "#0FF";

    var btn3 = document.createElement("DIV");
    btn3.style.width = (cal_width/3 - 3) + "px";
    btn3.style.height = pane_height + "px";
    btn3.style.lineHeight = pane_height + "px";
    btn3.style.textAlign = "center";
    btn3.innerHTML = "<";
    btn3.style.cursor = "pointer";
    btn3.style.cssFloat = "left";
    btn3.onclick = function(){
        if(isValidated()){
            var old_month = parseInt(document.getElementById("input_month").value)
            if(old_month > 1){
                var year = parseInt(document.getElementById("input_year").value);
                var month = old_month - 1;
                var val = year + separator + month + separator + 1;
                init(val);
            }
            else {
                var year = parseInt(document.getElementById("input_year").value) - 1;
                var month = 12;
                var val = year + separator + month + separator + 1;
                init(val);
            }
        }
    };
    line2.appendChild(btn3);

    var input_month = document.createElement("INPUT");
    input_month.id = "input_month";
    input_month.style.width = (cal_width/3) + "px";
    input_month.style.height = "70%";
    input_month.style.cssFloat = "left";
    input_month.style.textAlign = "center";
    input_month.onchange = function(){
        changed();
    };
    line2.appendChild(input_month);

    var btn4 = document.createElement("DIV");
    btn4.style.width = (cal_width/3 - 3) + "px";
    btn4.style.height = pane_height + "px";
    btn4.style.lineHeight = pane_height + "px";
    btn4.style.textAlign = "center";
    btn4.innerHTML = ">";
    btn4.style.cursor = "pointer";
    btn4.style.cssFloat = "left";
    btn4.onclick = function(){
        if(isValidated()){
            var old_month = parseInt(document.getElementById("input_month").value)
            if(old_month < 12){
                var year = parseInt(document.getElementById("input_year").value);
                var month = parseInt(document.getElementById("input_month").value) + 1;
                var val = year + separator + month + separator + 1;
                init(val);
            }
            else {
                var year = parseInt(document.getElementById("input_year").value) + 1;
                var month = 1;
                var val = year + separator + month + separator + 1;
                init(val);
            }
        }
    };
    line2.appendChild(btn4);

    cal_body.appendChild(line1);
    cal_body.appendChild(line2);

    for(var i=0; i < 7; i++){
        var pane = document.createElement("DIV");
        pane.className = "pane";
        pane.style.width = pane_height + "px";
        pane.style.height = pane_height + "px";
        pane.style.lineHeight = pane_height + "px";
        pane.style.textAlign = "center";
        pane.style.cssFloat = "left";
        pane.innerHTML = days[i];
        cal_body.appendChild(pane);
    }
    init(calendar.value);
}

}

function init(val){
clearPane();

var cal_body = document.getElementById("cal_body");
var temp_date;
var year;
var month;
var date;

if(val == ""){
    temp_date = today;
    calendar.value = today.toFormatString(separator);
}
else{
    year = val.year();
    month = val.month(separator);
    date = val.date(separator);
    temp_date = new Date(year,month,date);
}

year = temp_date.getFullYear();
month = temp_date.getMonth() + 1;
date = temp_date.getDate();
temp_date.setDate(1);

var start = temp_date.getDay() + 7;
var end;

if(array_contain(month_big, month)){
    end = start + 31;
}
else if(array_contain(month_small, month)){
    end = start + 30;
}
else{
    if(isLeapYear(year)){
        end = start + 29;
    }
    else{
        end = start + 28;
    }
}

for(var i = 7; i < start; i++){
    var pane = document.createElement("DIV");
    pane.className = "pane";
    pane.style.width = pane_height + "px";
    pane.style.height = pane_height + "px";
    pane.style.lineHeight = pane_height + "px";
    pane.style.textAlign = "center";
    pane.style.cssFloat = "left";
    cal_body.appendChild(pane);
}

for(var i = start; i < end; i++){
    var pane = document.createElement("DIV");
    pane.className = "pane";
    pane.style.width = pane_height + "px";
    pane.style.height = pane_height + "px";
    pane.style.lineHeight = pane_height + "px";
    pane.style.textAlign = "center";
    pane.style.cssFloat = "left";
    pane.innerHTML = i - start + 1;
    pane.style.cursor = "pointer";
    pane.onmouseover = function(){
        this.style.backgroundColor = ‘#0FF‘;
    }
    if(date == (i - start + 1))
        pane.style.backgroundColor = ‘#0FF‘;
    else{
        pane.onmouseout = function(){
            this.style.backgroundColor = ‘‘;
        }
    }
    pane.onclick = function(){
        calendar.value = year + separator + month + separator + this.innerHTML;
        cal_body.parentNode.removeChild(cal_body);
    }
    cal_body.appendChild(pane);

    document.getElementById("input_year").value = year;
    document.getElementById("input_month").value = month;
}

}

//格式化输出
Date.prototype.toFormatString = function(separator){
var result = this.getFullYear() + separator + (this.getMonth() + 1) + separator + this.getDate();
return result;
};

//从格式化字符串中获取年份
String.prototype.year = function(){
var str = this.substring(0,4);
return str;
};

//从格式化字符串中获取月份
String.prototype.month = function(separator){
var start = this.indexOf(separator) + 1;
var end = this.lastIndexOf(separator);
return parseInt(this.substring(start, end)) - 1;
};

//从格式化字符串中获取日期
String.prototype.date = function(separator){
var start = this.lastIndexOf(separator) + 1;
return this.substring(start);
};

//判断数组array中是否包含元素obj的函数,包含则返回true,不包含则返回false
function array_contain(array, obj){
for (var i = 0; i < array.length; i++){
if (array[i] == obj)
return true;
}
return false;
}

//判断年份year是否为闰年,是闰年则返回true,否则返回false
function isLeapYear(year){
var a = year % 4;
var b = year % 100;
var c = year % 400;
if( ( (a == 0) && (b != 0) ) || (c == 0) ){
return true;
}
return false;
}

//清除方格
function clearPane(){
var limit = document.getElementsByClassName("pane").length;
for(var i=7; i < limit; i++){
var pane = document.getElementsByClassName("pane").item(7);
pane.parentNode.removeChild(pane);
}
}

//判断输入是否合法
function isValidated(){
var year = document.getElementById("input_year").value;
var month = document.getElementById("input_month").value;
if(isNaN(year) || isNaN(month)){
alert("请输入正确的年份/月份");
return false;
}
else{
if(year%1 != 0 || month%1 != 0){
alert("请输入正确的年份/月份");
return false;
}
else{
year = parseInt(year);
if(year < 1960 || year > 2050){
alert("请输入1960~2050之间的年份!");
return false;
}
else if(month < 1 || month >12){
alert("请输入正确的月份!");
return false;
}
else{
return true;
}
}
}
}

//年份月份发生变化时处理函数
function changed(){
if(isValidated()){
var year = document.getElementById("input_year").value;
var month = document.getElementById("input_month").value;
var val = year + separator + month + separator + 1;
init(val);
}
}
喜欢这样文章的可以关注我,我会持续更新,你们的关注是我更新的动力!需要更多java学习资料的也可以私信我!
祝关注我的人都:身体健康,财源广进,福如东海,寿比南山,早生贵子,从不掉发!

原文地址:https://blog.51cto.com/14623707/2472920

时间: 2024-10-12 04:13:31

Java小项目之:教你做电子日历!的相关文章

Java小项目之:教你做个聊天系统!

Java小项目之:聊天系统今天给大家带来的java练手小项目是一个简单的聊天室,界面简单,操作不难.分为注册系统,登录系统和聊天系统三部分,很适合java小白练手.完整的源码和素材请关注并私信我获取! 界面展示: 代码展示:package chatRoom2; import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWr

java小项目之:植物大战僵尸,这个僵尸有点冷!内附素材源码

Java小项目之:植物大战僵尸! <植物大战僵尸>是由PopCap Games开发的一款益智策略类单机游戏,于2009年5月5日发售,这款游戏可谓是无人不知无人不晓. 在我身边,上到40岁的大叔阿姨,下到7.8岁的小弟弟妹妹都听说和玩过这游戏.在以前智能手机还没流行的时候,各种黑网吧,游戏厅便有着玩这游戏的人.当3G技术现世,半智能手机和智能手机出现后,这款游戏更是如日中天,与愤怒的小鸟一起霸占了手机游戏市场(但当时估计都是盗版的). 相信有些使用b站的小伙伴,应该看过很多这样的视频: 这种视

Java小项目之:拼图游戏!

Java小项目之:拼图游戏!今天教大家用java做出一个拼图游戏,很适合java初学者练手.所用素材: 部分代码: package picture_mosical; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.awt.image.CropImageFilter; import java.awt.i

Java小项目之:图书馆管理系统!有借有还再借不难!

Java小项目之:图书馆管理系统!今天给大家分享的java小项目是图书馆管理系统.这个图书馆管理系统是很完善的,包括书籍信息录入.借阅者信息.书籍类别添加.新书订购等等功能.和现实生活中的图书馆管理系统没什么两样,毫不夸张的说,你只要学会了今天我分享的这个小项目,以后自己创建一个图书馆管理系统是没一点问题的.按照惯例先上图: 部分代码展示:public class BookLoginIFrame extends JFrame { private class BookResetAction imp

java小项目之:泡泡堂炸弹人,来互相伤害呀!

Java小项目之:泡泡堂!双人对弈今天给大家分享的java小项目,是披着泡泡堂外衣的炸弹人游戏.炸弹人这种休闲游戏抓住了玩家对互动娱乐的需求,它操作简单,极易上手,趣味十足,能给玩家在短时间内就可获得游戏所带来的互动趣味和成就感.需要素材的可以私信我,这个小游戏是很适合java初学者练手的.功能模块简介: 游戏开始画面 人物移动的控制实现 炸弹的产生及炸弹爆炸的实现 结果的判断实现 计时的实现 两人一起互玩的实现游戏界面展示: 代码展示: 操作介绍:1.先进入游戏画面,用户可以自己选择角色,pl

Java小项目之:投票系统

Java小项目之:投票系统今天给大家带来的是java编写的投票小系统,代码简易,适合初学者练手!代码展示:package com.tarena.wgh.servlet; import java.io.IOException;import java.io.PrintWriter;import java.util.*; import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.serv

Java小项目之:在线测评考试系统

Java小项目之:在线测评考试系统,让你可以在家考科一!今天带来的java小项目是一套在线测评考试系统,题库是科目一的.不仅可以在家练练java技术,还可以边学习学习科目一,一举两得.界面介绍:开始.分数.考试规则.离开,四个选项.还有很多题库可以选择,可以私信我!代码展示:package service; import java.util.ArrayList; import java.util.List; import java.util.Random; import util.Config;

Java小项目之:文件的加密与解密!再也不怕存的小电影被别人发现了!

Java小项目之:文件的加密与解密!再也不怕存的小电影被别人发现了!今天带来的java小项目是加密解密系统,再也不怕别人偷看自己的电脑了,也可以正大光明的存小电影了.减少借别人电脑被看隐私的尴尬,从这个项目开始!界面展示: 部分代码展示:package wt.diy.encryption.gui; import java.io.File; import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JO

Java小项目之:小说阅读器

Java小项目之:小说阅读器 今天带来的java项目是一款阅读器,老少皆宜,适合练手. 代码展示: package com; import javax.swing.JOptionPane; public class Scroll { private int n; private int size; private Thread t; private static int def_speed = 1000; private static int up_speed = -500; private s