<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="ProgId" content="Word.Document">
<meta name="Generator" content="Microsoft Word 12">
<meta name="Originator" content="Microsoft Word 12">
<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5Cphoenix%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml">
<link rel="Edit-Time-Data" href="file:///C:%5CDOCUME%7E1%5Cphoenix%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_editdata.mso">
<!--[if !mso]>
<style>
v":* {behavior:url(#default#VML);}
o":* {behavior:url(#default#VML);}
w":* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]--><link rel="themeData" href="file:///C:%5CDOCUME%7E1%5Cphoenix%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx">
<link rel="colorSchemeMapping" href="file:///C:%5CDOCUME%7E1%5Cphoenix%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">
<!--[if gte mso 9]><xml>
Normal
0
false
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
</xml><![endif]--><!--[if gte mso 9]><![endif]--><style>
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:0 0 0 0 0 0 0 0 0 0;
mso-font-alt:"Times New Roman";}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{
mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"Calibri","serif";
mso-bidi-font-family:"Times New Roman";}
.MsoChpDefault
{
mso-bidi-font-family:"Times New Roman";}
/* Page Definitions */
@page
{}
@page Section1
{size:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
layout-grid:15.6pt;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{
mso-style-parent:"";
font-size:10.5pt;
font-family:"Calibri","serif";}
</style>
<![endif]-->
Java的日历是一个很常用的控件,实现界面布局从简单到复杂就有很多种,在加上公历和阴历的实现就更多了.这儿从最简单的做起,先做一个只能选择当前月的某一天的公历的简单日历,然后再做一个可以调整年月的日历,最后做一个加入阴历和节日的日历.
Java的日历实现也不复杂,不明白Sun为什么不加到JDK里面,主要是依靠java.util.Calendar类,依靠Calendar可以取得某一天是星期几,是一个月的第几天,也可以得出某个月第一天是星期几,某个月有多少天,这样就可以把日历的基本画出来了;再加上颜色设置和选择后的设置就可以实现基本的日历了.
先看最简单的效果,只实现一个月的选择:

<!--[if gte vml 1]>
<![endif]-->
再看一个可以选择年月日的,从一个JTextField里面点出下拉,这个是用JTable实现的,星期是JTable的Header,显示是JTable的单元格,只需要设置JTable的值和Renderer就可以显示组件了,值的计算是通过Calender实现的.


<!--[if gte vml 1]>
<![endif]-->
<!--[if gte vml 1]>
<![endif]-->
工程目录如下:

<!--[if gte vml 1]>
<![endif]-->
然后是一个用控件的形式来显示的,在JPanel上使用GridLayout,放置显示的组件,然后根据Canlendar设置值和显示:


<!--[if gte vml 1]>
<![endif]-->
<!--[if gte vml 1]>
<![endif]-->
工程目录如下:

<!--[if gte vml 1]>
<![endif]-->
最后是在上一个的基础上添加阴历,阴历实现是参考BlogJava的一个高手的:



<!--[if gte vml 1]>
<![endif]-->
<!--[if gte vml 1]>
<![endif]-->
<!--[if gte vml 1]>
<![endif]-->
工程目录如下:

<!--[if gte vml 1]>
<![endif]-->
本来是要详细写下日历的,然后在写其它的比如JtreeTable和2D绘制等东西呢,可惜公司现在基本不怎么使用Swing了,而且突然忙起来了,而我比较悲剧的是这个工作的上班地点在北五环,而我住在南三环,每天基本跨北京,一忙根本没时间写东西了,抽个星期天把以前的程序放在Google Code上了,也没时间整理了(好不容易周末,也得Dota几把呀),需要的自己去Down吧,大部分例子的XXXTest都是测试的方法.
Google
Code 地址: https://code.google.com/p/swingsource/