45fan.com - 路饭网
首页
路由器设置
无线路由器
路由器密码
网络频道
手机频道
电脑频道
图库频道
问答中心
搜索:
智能搜索
搜索标题
您的位置
:
主页
>
电脑频道
>
电脑教程
> 阅读资讯:怎么样使用java日历控件?
怎么样使用java日历控件?
2016-08-28 13:40:51 来源:www.45fan.com 【
大
中
小
】
怎么样使用java日历控件?
import
java.awt.BorderLayout;
import
java.awt.Color;
import
java.awt.Component;
import
java.awt.Container;
import
java.awt.FlowLayout;
import
java.awt.GridLayout;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
java.util.Calendar;
import
java.util.GregorianCalendar;
import
javax.swing.BorderFactory;
import
javax.swing.JButton;
import
javax.swing.JComboBox;
import
javax.swing.JFrame;
import
javax.swing.JPanel;
/***/
/**
*BeantodisplayamonthcalendarinaJPanel.OnlyworksfortheWestern
*calendar.
*
*
@author
IanF.Darwin,
http://www.darwinsys.com/
*
@version
$Id:Cal.java,v1.52004/02/0903:33:45ianExp$
*/
public
class
Cal
extends
JPanel
...
{
/***/
/**
Thecurrently-interestingyear(notmodulo1900!)
*/
protected
int
yy;
/***/
/**
Currently-interestingmonthandday
*/
protected
int
mm,dd;
/***/
/**
Thebuttonstobedisplayed
*/
protected
JButtonlabs[][];
/***/
/**
Thenumberofdaysquarestoleaveblankatthestartofthismonth
*/
protected
int
leadGap
=
0
;
/***/
/**
ACalendarobjectusedthroughout
*/
Calendarcalendar
=
new
GregorianCalendar();
/***/
/**
Today'syear
*/
protected
final
int
thisYear
=
calendar.get(Calendar.YEAR);
/***/
/**
Today'smonth
*/
protected
final
int
thisMonth
=
calendar.get(Calendar.MONTH);
/***/
/**
Oneofthebuttons.WejustkeepitsreferenceforgetBackground().
*/
private
JButtonb0;
/***/
/**
Themonthchoice
*/
private
JComboBoxmonthChoice;
/***/
/**
Theyearchoice
*/
private
JComboBoxyearChoice;
/***/
/**
*ConstructaCal,startingwithtoday.
*/
Cal()
...
{
super
();
setYYMMDD(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
buildGUI();
recompute();
}
/***/
/**
*ConstructaCal,giventheleadingdaysandthetotaldays
*
*
@exception
IllegalArgumentException
*Ifyearoutofrange
*/
Cal(
int
year,
int
month,
int
today)
...
{
super
();
setYYMMDD(year,month,today);
buildGUI();
recompute();
}
private
void
setYYMMDD(
int
year,
int
month,
int
today)
...
{
yy
=
year;
mm
=
month;
dd
=
today;
}
String[]months
=
...
{
"
January
"
,
"
February
"
,
"
March
"
,
"
April
"
,
"
May
"
,
"
June
"
,
"
July
"
,
"
August
"
,
"
September
"
,
"
October
"
,
"
November
"
,
"
December
"
}
;
/***/
/**
BuildtheGUI.AssumesthatsetYYMMDDhasbeencalled.
*/
private
void
buildGUI()
...
{
getAccessibleContext().setAccessibleDescription(
"
Calendarnotaccessibleyet.Sorry!
"
);
setBorder(BorderFactory.createEtchedBorder());
setLayout(
new
BorderLayout());
JPaneltp
=
new
JPanel();
tp.add(monthChoice
=
new
JComboBox());
for
(
int
i
=
0
;i
<
months.length;i
++
)
monthChoice.addItem(months[i]);
monthChoice.setSelectedItem(months[mm]);
monthChoice.addActionListener(
new
ActionListener()
...
{
public
void
actionPerformed(ActionEventae)
...
{
int
i
=
monthChoice.getSelectedIndex();
if
(i
>=
0
)
...
{
mm
=
i;
//
System.out.println("Month="+mm);
recompute();
}
}
}
);
monthChoice.getAccessibleContext().setAccessibleName(
"
Months
"
);
monthChoice.getAccessibleContext().setAccessibleDescription(
"
Chooseamonthoftheyear
"
);
tp.add(yearChoice
=
new
JComboBox());
yearChoice.setEditable(
true
);
for
(
int
i
=
yy
-
5
;i
<
yy
+
5
;i
++
)
yearChoice.addItem(Integer.toString(i));
yearChoice.setSelectedItem(Integer.toString(yy));
yearChoice.addActionListener(
new
ActionListener()
...
{
public
void
actionPerformed(ActionEventae)
...
{
int
i
=
yearChoice.getSelectedIndex();
if
(i
>=
0
)
...
{
yy
=
Integer.parseInt(yearChoice.getSelectedItem()
.toString());
//
System.out.println("Year="+yy);
recompute();
}
}
}
);
add(BorderLayout.CENTER,tp);
JPanelbp
=
new
JPanel();
bp.setLayout(
new
GridLayout(
7
,
7
));
labs
=
new
JButton[
6
][
7
];
//
firstrowisdays
bp.add(b0
=
new
JButton(
"
S
"
));
bp.add(
new
JButton(
"
M
"
));
bp.add(
new
JButton(
"
T
"
));
bp.add(
new
JButton(
"
W
"
));
bp.add(
new
JButton(
"
R
"
));
bp.add(
new
JButton(
"
F
"
));
bp.add(
new
JButton(
"
S
"
));
ActionListenerdateSetter
=
new
ActionListener()
...
{
public
void
actionPerformed(ActionEvente)
...
{
Stringnum
=
e.getActionCommand();
if
(
!
num.equals(
""
))
...
{
//
setthecurrentdayhighlighted
setDayActive(Integer.parseInt(num));
//
WhenthisbecomesaBean,youcan
//
firesomekindofDateChangedeventhere.
//
Also,buildasimilardaySetterforday-of-weekbtns.
}
}
}
;
//
Constructallthebuttons,andaddthem.
for
(
int
i
=
0
;i
<
6
;i
++
)
for
(
int
j
=
0
;j
<
7
;j
++
)
...
{
bp.add(labs[i][j]
=
new
JButton(
""
));
labs[i][j].addActionListener(dateSetter);
}
add(BorderLayout.SOUTH,bp);
}
public
final
static
int
dom[]
=
...
{
31
,
28
,
31
,
30
,
/**/
/*
janfebmarapr
*/
31
,
30
,
31
,
31
,
/**/
/*
mayjunjulaug
*/
30
,
31
,
30
,
31
/**/
/*
sepoctnovdec
*/
}
;
/***/
/**
Computewhichdaystoputwhere,intheCalpanel
*/
protected
void
recompute()
...
{
//
System.out.println("Cal::recompute:"+yy+":"+mm+":"+dd);
if
(mm
<
0
||
mm
>
11
)
throw
new
IllegalArgumentException(
"
Month
"
+
mm
+
"
bad,mustbe0-11
"
);
clearDayActive();
calendar
=
new
GregorianCalendar(yy,mm,dd);
//
Computehowmuchtoleavebeforethefirst.
//
getDay()returns0forSunday,whichisjustright.
leadGap
=
new
GregorianCalendar(yy,mm,
1
).get(Calendar.DAY_OF_WEEK)
-
1
;
//
System.out.println("leadGap="+leadGap);
int
daysInMonth
=
dom[mm];
if
(isLeap(calendar.get(Calendar.YEAR))
&&
mm
>
1
)
++
daysInMonth;
//
Blankoutthelabelsbefore1stdayofmonth
for
(
int
i
=
0
;i
<
leadGap;i
++
)
...
{
labs[
0
][i].setText(
""
);
}
//
Fillinnumbersforthedayofmonth.
for
(
int
i
=
1
;i
<=
daysInMonth;i
++
)
...
{
JButtonb
=
labs[(leadGap
+
i
-
1
)
/
7
][(leadGap
+
i
-
1
)
%
7
];
b.setText(Integer.toString(i));
}
//
7days/week*upto6rows
for
(
int
i
=
leadGap
+
1
+
daysInMonth;i
<
6
*
7
;i
++
)
...
{
labs[(i)
/
7
][(i)
%
7
].setText(
""
);
}
//
Shadecurrentday,onlyifcurrentmonth
if
(thisYear
==
yy
&&
mm
==
thisMonth)
setDayActive(dd);
//
shadetheboxfortoday
//
Sayweneedtobedrawnonthescreen
repaint();
}
/***/
/**
*isLeap()returnstrueifthegivenyearisaLeapYear.
*
*"ayearisaleapyearifitisdivisibleby4butnotby100,except
*thatyearsdivisibleby400*are*leapyears."--Kernighan&Ritchie,
*_TheCProgrammingLanguage_,p37.
*/
public
boolean
isLeap(
int
year)
...
{
if
(year
%
4
==
0
&&
year
%
100
!=
0
||
year
%
400
==
0
)
return
true
;
return
false
;
}
/***/
/**
Settheyear,month,andday
*/
public
void
setDate(
int
yy,
int
mm,
int
dd)
...
{
//
System.out.println("Cal::setDate");
this
.yy
=
yy;
this
.mm
=
mm;
//
startsat0,likeDate
this
.dd
=
dd;
recompute();
}
/***/
/**
Unsetanypreviouslyhighlightedday
*/
private
void
clearDayActive()
...
{
JButtonb;
//
Firstun-shadethepreviously-selectedsquare,ifany
if
(activeDay
>
0
)
...
{
b
=
labs[(leadGap
+
activeDay
-
1
)
/
7
][(leadGap
+
activeDay
-
1
)
%
7
];
b.setBackground(b0.getBackground());
b.repaint();
activeDay
=
-
1
;
}
}
private
int
activeDay
=
-
1
;
/***/
/**
Setjusttheday,onthecurrentmonth
*/
public
void
setDayActive(
int
newDay)
...
{
clearDayActive();
//
Setthenewone
if
(newDay
<=
0
)
dd
=
new
GregorianCalendar().get(Calendar.DAY_OF_MONTH);
else
dd
=
newDay;
//
Nowshadethecorrectsquare
Componentsquare
=
labs[(leadGap
+
newDay
-
1
)
/
7
][(leadGap
+
newDay
-
1
)
%
7
];
square.setBackground(Color.red);
square.repaint();
activeDay
=
newDay;
}
/***/
/**
Fortesting,amainprogram
*/
public
static
void
main(String[]av)
...
{
JFramef
=
new
JFrame(
"
Cal
"
);
Containerc
=
f.getContentPane();
c.setLayout(
new
FlowLayout());
//
forthistestdriver,hardcode1995/02/10.
c.add(
new
Cal(
1995
,
2
-
1
,
10
));
//
andbesideit,thecurrentmonth.
c.add(
new
Cal());
f.pack();
f.setVisible(
true
);
}
}
本文地址:
http://www.45fan.com/dnjc/68892.html
Tags:
控件
Java
日历
编辑:路饭网
上一篇:
怎么样注册序列号?
下一篇:
如何在Linux下使用perl编程读取rss文件?
相关文章列表
php和java的区别
在Word2016中制作可爱的动物日历的方法
在Excel中使用滚动条控件计算动态面积的
使用人生日历抢春运车票的方法
使用Word2016内容控件的步骤
在Excel中制作带有多个图表控件的动态的
在eclipse中批量修改java文件编码的方法
在IE浏览器中调试JavaScript程序的方法
win10中Outlook邮件和日历的快捷键有哪些
在PPT幻灯片中利用Windows media player
推广内容
推荐阅读
热门推荐
推荐文章
·
如何为Excel2010表格中的图表添加说明文
·
Win7系统看电影时,如果有QQ消息,电影声
·
WPS文字文档中,如何给指定的文字添加边
·
盘点制作PPT时常用的一些快捷键大全
·
分享一组Win8系统不太为人所知的小技巧
·
系统遇到特殊故障时,如何进入Win10系统
·
电脑系统损坏要如何修复?修复win10系统
·
想找回关闭的网页该怎么办?怎样查看360
·
实现电脑每天定时关机,借助Win7任务计划
·
什么是Telnet协议?Win7如何开启Telnet服
·
电脑时间出现差错时如何进行北京时间校准
·
电脑运行速度慢,C盘空间越来越小,如何
关于我们
|
联系我们
|
友情链接
|
网站地图
|
Sitemap
|
App
|
返回顶部