导航:首页 > 编程语言 > java日历控件

java日历控件

发布时间:2024-08-26 06:49:29

1. 我要设计一个日历控件 用 java开发的我的软件是eclipse我该怎么操作呢 求大神指导

这是我以前制作的日历控件,根据网友的代码改写的,自己用着感觉还行。现在没有时间根据你的条件再改写了,如果你觉得合用,就自己再改吧。

2. 点在图片上 显示日历框 可以选择日历上的日期 来查询对应时间数据库的信息 这个JSP代码怎么写

<%@ page contentType="text/html; charset=gb2312" %>
<%@ page language="java" import="java.util.*" %>

<%
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
%>

<html>

<head>
<title>选择日期</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<META name="GENERATOR" content="IBM WebSphere Studio">
<style>
SELECT {FONT-SIZE: 9pt; LINE-HEIGHT: 11pt}
TEXTAREA {FONT-SIZE: 9pt; LINE-HEIGHT: 11pt}
INPUT {FONT-SIZE: 9pt; LINE-HEIGHT: 11pt}
TD {FONT-SIZE: 9pt; LINE-HEIGHT: 11pt}
.p { font-size: 12px; line-height: 18px}
.pp { font-size: 14px; line-height: 18px}
.cals{font-family: "Arial"color:#000000; font-size:10pt}
</style>
<%@page import = "java.util.StringTokenizer"%>
<%
String type = request.getParameter("type");
if (type==null) type="1";

Calendar cal = Calendar.getInstance();
String cur_date = cal.get(Calendar.YEAR) + "年" + (cal.get(Calendar.MONTH)+1)
+ "月" + cal.get(Calendar.DAY_OF_MONTH)+"日";

int cur_year = cal.get(Calendar.YEAR);
int cur_month = (cal.get(Calendar.MONTH)+1);
int cur_day = cal.get(Calendar.DAY_OF_MONTH);
String cur_date_string = String.valueOf(cur_year) + "-"
+ (cur_month<10?("0"+String.valueOf(cur_month)):String.valueOf(cur_month)) + "-"
+ (cur_day<10?("0"+String.valueOf(cur_day)):String.valueOf(cur_day));

String parmInitDate = request.getParameter("initdate");
int parm_year=0;
int parm_month=0;
int parm_day=0;

if(parmInitDate!=null){
StringTokenizer mytoken = null;
try{
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = null;
date = formatter.parse(parmInitDate);
//java.util.Calendar cal = new java.util.Calendar()
cal.setTime(date);
parm_year = cal.get(Calendar.YEAR);
parm_month = cal.get(Calendar.MONTH) + 1;
parm_day = cal.get(Calendar.DAY_OF_MONTH );
}catch(Exception e){
//out.print(e);
}
}
parm_year= parm_year==0?cur_year:parm_year;
parm_month= parm_month==0?cur_month:parm_month;
parm_day= parm_day==0?cur_day:parm_day;
%>

<script LANGUAGE="vbscript">
<!--
dim cal(6,7)
dim sToday
dim sCurDate
dim sSeldate
dim selx,sely

sCurdate="<%=cur_date_string%>"
sSeldate="<%=parm_year+"-"+parm_month+"-"+parm_day%>"
sub getDate()
if window.event.srcElement.innerText = "" or window.event.srcElement.innerText = " " then exit sub

for i = 1 to 6
for j = 1 to 7
if cal(i,j).bgcolor="#0000ff" then
cal(i,j).bgcolor=""
cal(i,j).style.color="black"
end if
next
next
if window.event.srcElement.bgcolor<>"#ffff00" then
window.event.srcElement.bgcolor="blue"
window.event.srcElement.style.color="white"
end if

aa = window.year.value & "-" & right(window.month.value+100,2) & "-" & right(window.event.srcelement.innertext+100,2)

window.sel_date.innerText=aa
end sub

sub getTime()
aa=""
if window.hour.value<>"00" or window.minute.value<>"00" or window.second.value<>"00" then
aa=window.hour.value & ":" & window.minute.value & ":" & window.second.value
end if
if window.sel_date.innerText = "" then
window.sel_date.innerText = sCurDate
end if
window.sel_time.innerText=aa
end sub

sub ret ()
if window.sel_date.innerText <> "" then
window.returnValue = window.sel_date.innerText
if window.sel_time.innerText="" then
window.sel_time.innerText=window.hour.value & ":" & window.minute.value & ":" & window.second.value
end if
if window.sel_time.innerText <>"" then
window.returnValue = window.sel_date.innerText & " " & window.sel_time.innerText
end if
end if

window.event.returnValue = false
set cal(6,7) = nothing
window.close()
end sub

sub cancel ()
window.returnValue = ""
window.event.returnValue = false
set cal(6,7) = nothing
window.close()
end sub

sub draw
dim yy,mm,dd,thisday,lastmm,maxday
dim nextmm

yy = window.year.value
mm = window.month.value
dd = datepart("d",sSeldate)
thisday = dateserial (yy,mm,1)
set cal(1,1) = t10:set cal(1,2) = t11:set cal(1,3) = t12:set cal(1,4) = t13:set cal(1,5) = t14:set cal(1,6) = t15:set cal(1,7) = t16
set cal(2,1) = t20:set cal(2,2) = t21:set cal(2,3) = t22:set cal(2,4) = t23:set cal(2,5) = t24:set cal(2,6) = t25:set cal(2,7) = t26
set cal(3,1) = t30:set cal(3,2) = t31:set cal(3,3) = t32:set cal(3,4) = t33:set cal(3,5) = t34:set cal(3,6) = t35:set cal(3,7) = t36
set cal(4,1) = t40:set cal(4,2) = t41:set cal(4,3) = t42:set cal(4,4) = t43:set cal(4,5) = t44:set cal(4,6) = t45:set cal(4,7) = t46
set cal(5,1) = t50:set cal(5,2) = t51:set cal(5,3) = t52:set cal(5,4) = t53:set cal(5,5) = t54:set cal(5,6) = t55:set cal(5,7) = t56
set cal(6,1) = t60:set cal(6,2) = t61:set cal(6,3) = t62:set cal(6,4) = t63:set cal(6,5) = t64:set cal(6,6) = t65:set cal(6,7) = t66

for i = 1 to 6
for j = 1 to 7
cal(i,j).innertext = " "
cal(i,j).style.cursor = ""
cal(i,j).bgcolor=""
next
next
// cal(1,cint(datepart("w",thisday))).innertext = 1
// cal(1,cint(datepart("w",thisday))).style.cursor="hand"
//计算选择缺省日期
lastmm = datepart("m",thisday)
nextmm = lastmm + 1
if nextmm= 13 then nextmm = 1
maxday = datepart("d",dateadd("d",-1,dateserial (yy,nextmm,"1") ) )
if dd>maxday then dd = maxday

//缺省
i = 1 'the line
do until (datepart("m",thisday) - lastmm <>0)
if datepart ("w",thisday) = 1 then i = i + 1
cal(i,cint(datepart("w",thisday))).innertext = datepart("d",thisday)
if thisday=date then
cal(i,cint(datepart("w",thisday))).bgcolor="#A7A4D9" 'tt1
cal(i,cint(datepart("w",thisday))).style.color="red"
else
cal(i,cint(datepart("w",thisday))).style.color="black"
end if
if(datepart("d",thisday)=dd) then
if thisday<>date then
cal(i,cint(datepart("w",thisday))).bgcolor="blue"
cal(i,cint(datepart("w",thisday))).style.color="white"
end if
dim showday
showday = datepart("yyyy",thisday)& "-"
if(datepart("m",thisday)<10) then
showday = showday & "0"
end if
showday = showday & datepart("m",thisday)& "-"
if(datepart("d",thisday)<10) then
showday = showday & "0"
end if
showday = showday & datepart("d",thisday)
window.sel_date.innerText= showday
end if

cal(i,cint(datepart("w",thisday))).style.cursor="hand"
thisday = thisday + 1
loop

for i = 1 to 6
for j = 1 to 7
cal(i,j).style.fontsize = "9pt" '" normal small-caps 8pt serif"
next
next
end sub

sub up()
for i = 1 to 6
for j = 1 to 7
if cal(i,j).bgcolor="#0000ff" then
cal(i,j).bgcolor=""
cal(i,j).style.color="black"
end if
next
next
if window.month.value = 12 then
if window.year.value < 2100 then window.year.value = window.year.value + 1
window.month.value = 1
else
window.month.value = window.month.value + 1
end if
draw()
end sub

sub down ()
for i = 1 to 6
for j = 1 to 7
if cal(i,j).bgcolor="#0000ff" then
cal(i,j).bgcolor=""
cal(i,j).style.color="black"
end if
next
next
if window.month.value = 1 then
if window.year.value > 1996 then window.year.value = window.year.value - 1
window.month.value = 12
else
window.month.value = window.month.value - 1
end if
draw()

end sub
-->
</script>

<script ID="clientEventHandlersVBS" LANGUAGE="vbscript">
<!--

Sub year_onchange
draw()
End Sub

Sub month_onchange
draw()
End Sub

sub init ()
aa = date()
bb = <%=parm_year%>
if bb >= 1950 and bb <= 2100 then window.year.value = bb
bb = <%=parm_month%>
window.month.value = bb
sToday=<%=parm_day%>
draw ()
end sub
-->
</script>
<script language="javascript" for="t1" event="onkeydown">
//Esc退出
var keyDown = event.keyCode
if(keyDown==27){
window.close()
}
</script>

</head>
<body onload="init" leftmargin="12" topmargin="7" >
<center>
<!--<font class='pp'>今天是<%=cur_date%></font><br> -->
<table border="0" width="200" cellspacing="0" cellpadding="1" bgcolor="snow" id="t1" align="center">
<tr>
<td colspan='7' nowrap >
<select id="year" name="year">
<%for (int i=2000; i<=2020; i++) {%>
<option value=<%=i%>><%=i%></option>
<%}%>
</select>
<select id="month" name="month">
<%for (int i=1; i<=12; i++) {%>
<option value=<%=i%>><%=i%>月</option>
<%}%>
</select>
<img src="../images/arr04.gif" id="up" language="vbscript" style='cursor:hand' onclick="down()" accesskey=1 align="absmiddle">
<img src="../images/arr05.gif" id="down" language="vbscript" style='cursor:hand' onclick="up()" accesskey=2 align="absmiddle">
</td>
</tr>
<tr>
<td colspan='7' height="5" > </td>
</tr>
<tr bgcolor="#7975C6">
<td align='center' class="cals" height="23" align="absmiddle">日</td>
<td align='center' class="cals" align="absmiddle">一</td>
<td align='center' class="cals" align="absmiddle">二</td>
<td align='center' class="cals" align="absmiddle">三</td>
<td align='center' class="cals" align="absmiddle">四</td>
<td align='center' class="cals" align="absmiddle">五</td>
<td align='center' class="cals" align="absmiddle">六</td>
</tr>
<%for (int i=1; i<=6; i++) {%>
<tr bgcolor="#e6e6fa">
<%for (int j=0; j<=6; j++) {%>
<td align='center' id=t<%=i%><%=j%> onclick="getDate()" ondblclick="ret()" class="cals"></td>
<%}%>
</tr>
<%}%>
<%String disp=null;%>
<%if (type.compareTo("2")==0) {%>
<tr>
<td colspan='7' nowrap height="28" align="center" valign="bottom" >
<select id="hour" name="hour" onchange='getTime()'>
<%
for (int i=0; i<=23; i++) {
disp=String.valueOf(i);
if (disp.length()==1) disp = "0"+disp;
%>
<option value=<%=disp%><%if (i==0) out.print(" selected");%>><%=disp%></option>
<%}%>
</select><font class='p'>时</font>
<select id="minute" name="minute" onchange='getTime()'>
<%
for (int i=0; i<=59; i++) {
disp=String.valueOf(i);
if (disp.length()==1) disp = "0"+disp;
%>
<option value=<%=disp%><%if (i==0) out.print(" selected");%>><%=disp%></option>
<%}%>
</select><font class='p'>分</font>
<select id="second" name="second" onchange='getTime()'>
<%
for (int i=0; i<=59; i++) {
disp=String.valueOf(i);
if (disp.length()==1) disp = "0"+disp;
%>
<option value=<%=disp%><%if (i==0) out.print(" selected");%>><%=disp%></option>
<%}%>
</select><font class='p'>秒</font>
</td></tr>
<%}%>
</table>
<table border='0' width='180' style="display:none">
<tr>
<td nowrap>
时间:
<font color='red'>
<span id='sel_date' name='sel_date'></span>
<span id='sel_time' name='sel_time'></span>
</font>
</td>
</tr>
</table>
<table border='0' cellspacing="0" cellpadding="0" >
<tr><td height="4"></td></tr>
<tr><td align="absmiddle"><img src="../images/bt_sure.gif" onclick='ret()'></td></tr>
</table>
</center>
<%
String initH="00";
String initM="00";
String initS="00";
if(type.equals("2")){
if(parmInitDate==null) parmInitDate="";
parmInitDate=parmInitDate.trim();
if(parmInitDate.length()>10){
initH=parmInitDate.substring(11,13);
initM=parmInitDate.substring(14,16);
initS=parmInitDate.substring(17,19);
}else{
//初始化为当前时间
java.text.SimpleDateFormat sdf=null;
String curTime="";
try{
sdf=new java.text.SimpleDateFormat("HH:mm:ss");
curTime=sdf.format(new java.util.Date());
}catch(Exception e){
curTime="00:00:00";
System.out.println("格式为日期出错");
}
initH=curTime.substring(0,2);
initM=curTime.substring(3,5);
initS=curTime.substring(6,8);
}
}

%>
<script language="javascript">

var itype='<%=type %>';
var hh='<%=initH %>';
var mm='<%=initM %>';
var ss='<%=initS %>';
if(itype=='2'){
//初始化数据
var objh=document.getElementById('hour');
var objm=document.getElementById('minute');
var objs=document.getElementById('second');
objh.value=hh;
objm.value=mm;
objs.value=ss;
}

</script>
</body>
</html>

3. java 日期模块

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>blog式日历控件</title>
</head>
<body>
<script type="text/javascript">
var $ = function (id) {
return "string" == typeof id ? document.getElementById(id) : id;
};

var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}

Object.extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
}

var Calendar = Class.create();
Calendar.prototype = {
initialize: function(container, options) {
this.Container = $(container);//容器(table结构)
this.Days = [];//日期对象列表

this.SetOptions(options);

this.Year = this.options.Year;
this.Month = this.options.Month;
this.SelectDay = this.options.SelectDay ? new Date(this.options.SelectDay) : null;
this.onSelectDay = this.options.onSelectDay;
this.onToday = this.options.onToday;
this.onFinish = this.options.onFinish;

this.Draw();
},
//设置默认属性
SetOptions: function(options) {
this.options = {//默认值
Year: new Date().getFullYear(),//显示年
Month: new Date().getMonth() + 1,//显示月
SelectDay: null,//选择日期
onSelectDay: function(){},//在选择日期触发
onToday: function(){},//在当天日期触发
onFinish: function(){}//日历画完后触发
};
Object.extend(this.options, options || {});
},
//上一个月
PreMonth: function() {
//先取得上一个月的日期对象
var d = new Date(this.Year, this.Month - 2, 1);
//再设置属性
this.Year = d.getFullYear();
this.Month = d.getMonth() + 1;
//重新画日历
this.Draw();
},
//下一个月
NextMonth: function() {
var d = new Date(this.Year, this.Month, 1);
this.Year = d.getFullYear();
this.Month = d.getMonth() + 1;
this.Draw();
},
//画日历
Draw: function() {
//用来保存日期列表
var arr = [];
//用当月第一天在一周中的日期值作为当月离第一天的天数
for(var i = 1, firstDay = new Date(this.Year, this.Month - 1, 1).getDay(); i <= firstDay; i++){ arr.push(""); }
//用当月最后一天在一个月中的日期值作为当月的天数
for(var i = 1, monthDay = new Date(this.Year, this.Month, 0).getDate(); i <= monthDay; i++){ arr.push(i); }

var frag = document.createDocumentFragment();

this.Days = [];

while(arr.length > 0){
//每个星期插入一个tr
var row = document.createElement("tr");
//每个星期有7天
for(var i = 1; i <= 7; i++){
var cell = document.createElement("td");
cell.innerHTML = "";

if(arr.length > 0){
var d = arr.shift();
cell.innerHTML = d;
if(d > 0){
this.Days[d] = cell;
//判断是否今日
if(this.IsSame(new Date(this.Year, this.Month - 1, d), new Date())){ this.onToday(cell); }
//判断是否选择日期
if(this.SelectDay && this.IsSame(new Date(this.Year, this.Month - 1, d), this.SelectDay)){ this.onSelectDay(cell); }
}
}
row.appendChild(cell);
}
frag.appendChild(row);
}

//先清空内容再插入(ie的table不能用innerHTML)
while(this.Container.hasChildNodes()){ this.Container.removeChild(this.Container.firstChild); }
this.Container.appendChild(frag);

this.onFinish();
},
//判断是否同一日
IsSame: function(d1, d2) {
return (d1.getFullYear() == d2.getFullYear() && d1.getMonth() == d2.getMonth() && d1.getDate() == d2.getDate());
}
};

</script>
<style type="text/css">

.Calendar {
font-family:Verdana;
font-size:12px;
background-color:#e0ecf9;
text-align:center;
width:200px;
height:160px;
padding:10px;
line-height:1.5em;
}
.Calendar a{
color:#1e5494;
}

.Calendar table{
width:100%;
border:0;
}

.Calendar table thead{color:#acacac;}

.Calendar table td {
font-size: 11px;
padding:1px;
}
#idCalendarPre{
cursor:pointer;
float:left;
padding-right:5px;
}
#idCalendarNext{
cursor:pointer;
float:right;
padding-right:5px;
}
#idCalendar td.onToday {
font-weight:bold;
color:#C60;
}
#idCalendar td.onSelect {
font-weight:bold;
}
</style>
<div class="Calendar">
<div id="idCalendarPre"><<</div>
<div id="idCalendarNext">>></div>
<span id="idCalendarYear">2008</span>年 <span id="idCalendarMonth">8</span>月
<table cellspacing="0">
<thead>
<tr>
<td>日</td>
<td>一</td>
<td>二</td>
<td>三</td>
<td>四</td>
<td>五</td>
<td>六</td>
</tr>
</thead>
<tbody id="idCalendar">
</tbody>
</table>
</div>
<script language="JavaScript">

var cale = new Calendar("idCalendar", {
SelectDay: new Date().setDate(10),
onSelectDay: function(o){ o.className = "onSelect"; },
onToday: function(o){ o.className = "onToday"; },
onFinish: function(){
$("idCalendarYear").innerHTML = this.Year; $("idCalendarMonth").innerHTML = this.Month;
var flag = [10,15,20];
for(var i = 0, len = flag.length; i < len; i++){
this.Days[flag[i]].innerHTML = "<a href='javascript:void(0);' onclick=\"alert('日期是:"+this.Year+"/"+this.Month+"/"+flag[i]+"');return false;\">" + flag[i] + "</a>";
}
}
});

$("idCalendarPre").onclick = function(){ cale.PreMonth(); }
$("idCalendarNext").onclick = function(){ cale.NextMonth(); }

</script>
</body>
</html>

4. JAVA日历代码,怎么做

import java.util.Date;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.border.LineBorder;

/**

* @company:NEUSOFT
* @Title:日期选择控件
* @Description:在原有基础上修改了以下内容:
* 1. 将容器由Frame改为了Dialog,以便在基于对话框的程序中也能够使用
* 2. 将最小日期由1980改为了1950,考虑到目前球员的出生日期可能早于1980年
* 3. 将初始显示格式设置为 yyyy年MM月dd日 格式,原有的小时去掉了,不适合于出生日期字段
*/
public class DateChooserJButton extends JButton {

private DateChooser dateChooser = null;

private String preLabel = "";

public DateChooserJButton() {
this(getNowDate());
}

public DateChooserJButton(SimpleDateFormat df, String dateString) {
this();
setText(df, dateString);
}

public DateChooserJButton(Date date) {
this("", date);
}

public DateChooserJButton(String preLabel, Date date) {
if (preLabel != null)
this.preLabel = preLabel;
setDate(date);
setBorder(null);
setCursor(new Cursor(Cursor.HAND_CURSOR));
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (dateChooser == null)
dateChooser = new DateChooser();
Point p = getLocationOnScreen();
p.y = p.y + 30;
dateChooser.showDateChooser(p);
}
});
}

private static Date getNowDate() {
return Calendar.getInstance().getTime();
}

private static SimpleDateFormat getDefaultDateFormat() {
return new SimpleDateFormat("yyyy年MM月dd日");
}

// 覆盖父类的方法
public void setText(String s) {
Date date;
try {
date = getDefaultDateFormat().parse(s);
} catch (ParseException e) {
date = getNowDate();
}
setDate(date);
}

public void setText(SimpleDateFormat df, String s) {
Date date;
try {
date = df.parse(s);
} catch (ParseException e) {
date = getNowDate();
}
setDate(date);
}

public void setDate(Date date) {
super.setText(preLabel + getDefaultDateFormat().format(date));
}

public Date getDate() {
String dateString = this.getText().substring(preLabel.length());
try {
return getDefaultDateFormat().parse(dateString);
} catch (ParseException e) {
return getNowDate();
}

}

public String getDateString()
{
Date birth =getDate();
DateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
return formatDate.format(birth).toString();

//return this.getText().substring(preLabel.length());
}

// 覆盖父类的方法使之无效
//public void addActionListener(ActionListener listener) {
//}

private class DateChooser extends JPanel implements ActionListener,
ChangeListener {
int startYear = 1950; // 默认【最小】显示年份

int lastYear = 2050; // 默认【最大】显示年份

int width = 200; // 界面宽度

int height = 200; // 界面高度

Color backGroundColor = Color.gray; // 底色
// 月历表格配色----------------//

Color palletTableColor = Color.white; // 日历表底色

Color todayBackColor = Color.orange; // 今天背景色

Color weekFontColor = Color.blue; // 星期文字色

Color dateFontColor = Color.black; // 日期文字色

Color weekendFontColor = Color.red; // 周末文字色

// 控制条配色------------------//
Color controlLineColor = Color.pink; // 控制条底色

Color controlTextColor = Color.white; // 控制条标签文字色

Color rbFontColor = Color.white; // RoundBox文字色

Color rbBorderColor = Color.red; // RoundBox边框色

Color rbButtonColor = Color.pink; // RoundBox按钮色

Color rbBtFontColor = Color.red; // RoundBox按钮文字色

JDialog dialog;

JSpinner yearSpin;

JSpinner monthSpin;

JSpinner hourSpin;

JButton[][] daysButton = new JButton[6][7];

DateChooser() {

setLayout(new BorderLayout());
setBorder(new LineBorder(backGroundColor, 2));
setBackground(backGroundColor);

JPanel topYearAndMonth = createYearAndMonthPanal();
add(topYearAndMonth, BorderLayout.NORTH);
JPanel centerWeekAndDay = createWeekAndDayPanal();
add(centerWeekAndDay, BorderLayout.CENTER);

}

private JPanel createYearAndMonthPanal() {
Calendar c = getCalendar();
int currentYear = c.get(Calendar.YEAR);
int currentMonth = c.get(Calendar.MONTH) + 1;
int currentHour = c.get(Calendar.HOUR_OF_DAY);

JPanel result = new JPanel();
result.setLayout(new FlowLayout());
result.setBackground(controlLineColor);

yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,
startYear, lastYear, 1));
yearSpin.setPreferredSize(new Dimension(48, 20));
yearSpin.setName("Year");
yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));
yearSpin.addChangeListener(this);
result.add(yearSpin);

JLabel yearLabel = new JLabel("年");
yearLabel.setForeground(controlTextColor);
result.add(yearLabel);

monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,
12, 1));
monthSpin.setPreferredSize(new Dimension(35, 20));
monthSpin.setName("Month");
monthSpin.addChangeListener(this);
result.add(monthSpin);

JLabel monthLabel = new JLabel("月");
monthLabel.setForeground(controlTextColor);
result.add(monthLabel);

hourSpin = new JSpinner(new SpinnerNumberModel(currentHour, 0, 23,
1));
hourSpin.setPreferredSize(new Dimension(35, 20));
hourSpin.setName("Hour");
hourSpin.addChangeListener(this);
result.add(hourSpin);

JLabel hourLabel = new JLabel("时");
hourLabel.setForeground(controlTextColor);
result.add(hourLabel);

return result;
}

private JPanel createWeekAndDayPanal() {
String colname[] = { "日", "一", "二", "三", "四", "五", "六" };
JPanel result = new JPanel();
// 设置固定字体,以免调用环境改变影响界面美观
result.setFont(new Font("宋体", Font.PLAIN, 12));
result.setLayout(new GridLayout(7, 7));
result.setBackground(Color.white);
JLabel cell;

for (int i = 0; i < 7; i++) {
cell = new JLabel(colname[i]);
cell.setHorizontalAlignment(JLabel.RIGHT);
if (i == 0 || i == 6)
cell.setForeground(weekendFontColor);
else
cell.setForeground(weekFontColor);
result.add(cell);
}

int actionCommandId = 0;
for (int i = 0; i < 6; i++)
for (int j = 0; j < 7; j++) {
JButton numberButton = new JButton();
numberButton.setBorder(null);
numberButton.setHorizontalAlignment(SwingConstants.RIGHT);
numberButton.setActionCommand(String
.valueOf(actionCommandId));
numberButton.addActionListener(this);
numberButton.setBackground(palletTableColor);
numberButton.setForeground(dateFontColor);
if (j == 0 || j == 6)
numberButton.setForeground(weekendFontColor);
else
numberButton.setForeground(dateFontColor);
daysButton[i][j] = numberButton;
result.add(numberButton);
actionCommandId++;
}

return result;
}

private JDialog createDialog(JDialog owner) {
JDialog result = new JDialog(owner, "日期时间选择", true);
result.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
result.getContentPane().add(this, BorderLayout.CENTER);
result.pack();
result.setSize(width, height);
return result;
}

void showDateChooser(Point position) {
JDialog owner = (JDialog) SwingUtilities
.getWindowAncestor(DateChooserJButton.this);
if (dialog == null || dialog.getOwner() != owner)
dialog = createDialog(owner);
dialog.setLocation(getAppropriateLocation(owner, position));
flushWeekAndDay();
dialog.setVisible(true);
}

Point getAppropriateLocation(JDialog owner, Point position) {
Point result = new Point(position);
Point p = owner.getLocation();
int offsetX = (position.x + width) - (p.x + owner.getWidth());
int offsetY = (position.y + height) - (p.y + owner.getHeight());

if (offsetX > 0) {
result.x -= offsetX;
}

if (offsetY > 0) {
result.y -= offsetY;
}

return result;

}

private Calendar getCalendar() {
Calendar result = Calendar.getInstance();
result.setTime(getDate());
return result;
}

private int getSelectedYear() {
return ((Integer) yearSpin.getValue()).intValue();
}

private int getSelectedMonth() {
return ((Integer) monthSpin.getValue()).intValue();
}

private int getSelectedHour() {
return ((Integer) hourSpin.getValue()).intValue();
}

private void dayColorUpdate(boolean isOldDay) {
Calendar c = getCalendar();
int day = c.get(Calendar.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH, 1);
int actionCommandId = day - 2 + c.get(Calendar.DAY_OF_WEEK);
int i = actionCommandId / 7;
int j = actionCommandId % 7;
if (isOldDay)
daysButton[i][j].setForeground(dateFontColor);
else
daysButton[i][j].setForeground(todayBackColor);
}

private void flushWeekAndDay() {
Calendar c = getCalendar();
c.set(Calendar.DAY_OF_MONTH, 1);
int maxDayNo = c.getActualMaximum(Calendar.DAY_OF_MONTH);
int dayNo = 2 - c.get(Calendar.DAY_OF_WEEK);
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
String s = "";
if (dayNo >= 1 && dayNo <= maxDayNo)
s = String.valueOf(dayNo);
daysButton[i][j].setText(s);
dayNo++;
}
}
dayColorUpdate(false);
}

public void stateChanged(ChangeEvent e) {
JSpinner source = (JSpinner) e.getSource();
Calendar c = getCalendar();
if (source.getName().equals("Hour")) {
c.set(Calendar.HOUR_OF_DAY, getSelectedHour());
setDate(c.getTime());
return;
}

dayColorUpdate(true);

if (source.getName().equals("Year"))
c.set(Calendar.YEAR, getSelectedYear());
else
// (source.getName().equals("Month"))
c.set(Calendar.MONTH, getSelectedMonth() - 1);
setDate(c.getTime());
flushWeekAndDay();
}

public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
if (source.getText().length() == 0)
return;
dayColorUpdate(true);
source.setForeground(todayBackColor);
int newDay = Integer.parseInt(source.getText());
Calendar c = getCalendar();
c.set(Calendar.DAY_OF_MONTH, newDay);
setDate(c.getTime());
}

}

}

这是一个专门的选日期的类 ,你看看完了调用就行了

阅读全文

与java日历控件相关的资料

热点内容
android环信图片 浏览:134
ees算法具有什么特点 浏览:768
安卓手机如何弄恋爱日记 浏览:624
android连接webservice 浏览:221
对于简单的加密和编码方法 浏览:334
XC8编译结果不同 浏览:217
气体隔膜压缩机 浏览:587
新的电脑文件夹打开为什么是空的 浏览:821
制冷压缩机寿命 浏览:734
androidlistview按下颜色 浏览:194
单片机实验报告蜂鸣器 浏览:10
农业量地用什么app 浏览:11
python构造时间 浏览:843
页面加密不能复制 浏览:680
什么app可以看美剧无删美剧 浏览:845
mom解压包下载 浏览:270
什么手机app可以学习数学 浏览:496
pdf文档制作 浏览:781
修改文件夹显示电影图标 浏览:133
王者荣耀视频app叫什么 浏览:900