import java.applet.Applet;
import java.awt.*;
import java.util.*;
public class CalendarApplet extends Applet{
static final int TOP = 70; //顶端距离
static final int CELLWIDTH=50,CELLHEIGHT = 30; //单元格尺寸
static final int MARGIN = 3; //边界距离
static final int FEBRUARY = 1;
TextField tfYear = new TextField("2004", 5); //显示年份的文本域
Choice monthChoice = new Choice(); //月份选择下拉框
Button btUpdate = new Button("更新"); //更新按钮
GregorianCalendar calendar=new GregorianCalendar(); //日历对象
Font smallFont = new Font("TimesRoman", Font.PLAIN, 15); //显示小字体
Font bigFont = new Font("TimesRoman", Font.BOLD, 50); //显示大字体
String days[] = {"星期日", "星期一", "星期二", "星期三","星期四", "星期五", "星期六"};
String months[] = {"一月", "二月", "三月", "四月","五月", "六月", "七月", "八月", "九月","十月", "十一月", "十二月"};
int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //每个月的天数
int searchMonth,searchYear; //查询的年份及月份
public void init(){
setBackground(Color.white); //设置背景颜色
searchMonth = calendar.get(Calendar.MONTH); //得到系统年份
searchYear = calendar.get(Calendar.YEAR); //得到系统月份
add(new Label(" 年:")); //增加组件到Applet
tfYear.setText(String.valueOf(searchYear)); //设置文本域文字
add(tfYear);
add(new Label(" 月:"));
monthChoice.setFont(smallFont); //设置月份选择下拉框的显示字体
for (int i = 0; i < 12; i++) {
monthChoice.addItem(months[i]); //增加下拉框选项
}
monthChoice.select(searchMonth); //设置下拉框当前选择项
add(monthChoice);
add(btUpdate);
int componentCount=this.getComponentCount(); //得到Applet中的组件数量
for (int i=0;i<componentCount;i++){
getComponent(i).setFont(smallFont); //设置所有组件的显示字体
}
}
public void paint(Graphics g){
FontMetrics fontMetric; //显示字体的FontMetrics对象
int fontAscent;
int dayPos;
int totalWidth, totalHeight; //总的宽度,高度
int numRows; //行数
int xNum, yNum; //水平和垂直方向单元格数量
int numDays;
String dayStr; //显示天数字符串
int margin;
g.setColor(Color.lightGray); //设置当前颜色
g.setFont(bigFont); //设置当前使用字体
g.drawString(searchYear+"年",60,TOP+70); //绘制字符串
g.drawString((searchMonth+1)+"月",200,TOP+130);
g.setColor(Color.black);
g.setFont(smallFont);
fontMetric = g.getFontMetrics(); //获取变量初值
fontAscent = fontMetric.getAscent();
dayPos = TOP + fontAscent / 2;
totalWidth = 7 * CELLWIDTH; //得到总的表格宽度
for (int i = 0; i < 7; i++) {
g.drawString(days[i], (CELLWIDTH-fontMetric.stringWidth(days[i]))/2 + i*CELLWIDTH,dayPos-20); //绘制表格标题栏
}
numRows = getNumberRows(searchYear, searchMonth); //计算需要的行的数量
totalHeight = numRows * CELLHEIGHT; //得到总的表格高度
for (int i = 0; i <= totalWidth; i += CELLWIDTH) {
g.drawLine(i, TOP , i, TOP+ totalHeight); //绘制表格线
}
for (int i = 0, j = TOP ; i <= numRows; i++, j += CELLHEIGHT) {
g.drawLine(0, j, totalWidth, j); //绘制表格线
}
xNum = (getFirstDayOfMonth(searchYear, searchMonth) + 1) * CELLWIDTH - MARGIN;
yNum = TOP + MARGIN + fontAscent;
numDays = daysInMonth[searchMonth] + ((calendar.isLeapYear(searchYear) && (searchMonth == FEBRUARY)) ? 1 : 0);
for (int day = 1; day <= numDays; day++) {
dayStr = Integer.toString(day);
g.drawString(dayStr, xNum - fontMetric.stringWidth(dayStr), yNum); //绘制字符串
xNum += CELLWIDTH;
if (xNum > totalWidth) {
xNum = CELLWIDTH - MARGIN;
yNum += CELLHEIGHT;
}
}
}
public boolean action(Event e, Object o){
int searchYearInt;
if (e.target==btUpdate){
searchMonth = monthChoice.getSelectedIndex(); //得到查询月份
searchYearInt = Integer.parseInt(tfYear.getText(), 10); //得到查询年份
if (searchYearInt > 1581) {
searchYear = searchYearInt;
}
repaint(); //重绘屏幕
return true;
}
return false;
}
private int getNumberRows(int year, int month) { //得到行数量
int firstDay;
int numCells;
if (year < 1582) { //年份小于1582年,则返回-1
return (-1);
}
if ((month < 0) || (month > 11)) {
return (-1);
}
firstDay = getFirstDayOfMonth(year, month); //计算月份的第一天
if ((month == FEBRUARY) && (firstDay == 0) && !calendar.isLeapYear(year)) {
return 4;
}
numCells = firstDay + daysInMonth[month];
if ((month == FEBRUARY) && (calendar.isLeapYear(year))) {
numCells++;
}
return ((numCells <= 35) ? 5 : 6); //返回行数
}
private int getFirstDayOfMonth(int year, int month) { //得到每月的第一天
int firstDay;
int i;
if (year < 1582) { //年份小于1582年,返回-1
return (-1);
}
if ((month < 0) || (month > 11)) { //月份数错误,返回-1
return (-1);
}
firstDay = getFirstDayOfYear(year); //得到每年的第一天
for (i = 0; i < month; i++) {
firstDay += daysInMonth[i]; //计算每月的第一天
}
if ((month > FEBRUARY) && calendar.isLeapYear(year)) {
firstDay++;
}
return (firstDay % 7);
}
private int getFirstDayOfYear(int year){ //计算每年的第一天
int leapYears;
int hundreds;
int fourHundreds;
int first;
if (year < 1582) { //如果年份小于1582年
return (-1); //返回-1
}
leapYears = (year - 1581) / 4;
hundreds = (year - 1501) / 100;
leapYears -= hundreds;
fourHundreds = (year - 1201) / 400;
leapYears += fourHundreds;
first=5 + (year - 1582) + leapYears % 7; //得到每年第一天
return first;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java实用程序设计100例(1-2)
共242个文件
class:77个
bmp:51个
java:47个
4星 · 超过85%的资源 需积分: 10 26 下载量 160 浏览量
2009-04-19
21:51:49
上传
评论 1
收藏 1.36MB RAR 举报
温馨提示
1-2章里有39个实例,都是培养对java的兴趣,以及对java的了解,全都是源码,很适合java初学者,和对java有兴趣的开发人员 不能下载请留言
资源推荐
资源详情
资源评论
收起资源包目录
java实用程序设计100例(1-2) (242个子文件)
28-2.bmp 525KB
27-2.bmp 507KB
43-1.bmp 464KB
23-1.bmp 401KB
23-2.bmp 401KB
45-1.bmp 344KB
30-1.bmp 288KB
42-1.bmp 277KB
41-2.bmp 264KB
41-1.bmp 264KB
7-1.bmp 262KB
35-2.bmp 254KB
35-1.bmp 254KB
37-1.bmp 238KB
34-1.bmp 237KB
34-2.bmp 237KB
9-2.bmp 229KB
39-1.bmp 215KB
38-1.bmp 214KB
44-1.bmp 212KB
14-1.bmp 195KB
18-1.bmp 194KB
40-1.bmp 185KB
13-1.bmp 176KB
21-1.bmp 176KB
10-1.bmp 171KB
11-1.bmp 171KB
19-1.bmp 169KB
8-1.bmp 164KB
31-1.bmp 163KB
33-1.bmp 159KB
32-1.bmp 149KB
17-1.bmp 147KB
27-1.bmp 147KB
29-1.bmp 141KB
36-1.bmp 140KB
36-2.bmp 140KB
26-1.bmp 139KB
20-1.bmp 132KB
25-1.bmp 129KB
15-1.bmp 126KB
15-2.bmp 126KB
15-3.bmp 126KB
8-2.bmp 124KB
16-1.bmp 104KB
12-1.bmp 103KB
12-2.bmp 97KB
22-1.bmp 82KB
24-1.bmp 59KB
24-2.bmp 59KB
28-1.bmp 55KB
CalendarApplet.class 4KB
ShutterApplet.class 4KB
JumpTextApplet.class 4KB
Calculator.class 3KB
ShadowTextApplet.class 3KB
ReflectionApplet.class 3KB
TurnApplet.class 3KB
AlarmApplet.class 3KB
WaveTextApplet.class 3KB
ExtendTextApplet.class 3KB
EditorDemo.class 3KB
LookAndFeelDemo.class 3KB
JWindowDemo.class 2KB
AlbumApplet.class 2KB
FirePicApplet.class 2KB
FlyTextApplet.class 2KB
AnimatorIcon.class 2KB
Text3DApplet.class 2KB
ClockDemo.class 2KB
ClipboardDemo.class 2KB
RoundButtonDemo.class 2KB
DragList.class 2KB
IconComboBoxDemo.class 2KB
JListDemo.class 2KB
ActionDemo.class 2KB
ShowImageApplet.class 2KB
JPopMenuDemo.class 2KB
JTreeDemo.class 2KB
FormDemo.class 2KB
JPasswordFieldDemo.class 2KB
JFileChooserDemo.class 2KB
HTTPBrowserDemo.class 2KB
GridBagLayoutDemo.class 2KB
JFileChooserDemo$1.class 2KB
DragPictureDemo$DragPicListener.class 1KB
JSliderDemo.class 1KB
FontDemo.class 1KB
DragLabel.class 1KB
CursorDemo.class 1KB
DragPictureDemo.class 1KB
DragTextDemo.class 1KB
LineDemo.class 1KB
ClipboardDemo$PasteListener.class 1KB
JListDemo$1.class 1KB
JPasswordFieldDemo$1.class 1KB
HTTPBrowserDemo$ShowHTMLListener.class 1KB
EditorDemo$SaveAction.class 1KB
ActionDemo$2.class 1KB
EditorDemo$OpenAction.class 1KB
共 242 条
- 1
- 2
- 3
资源评论
- xrixvrui12014-03-18学习java编程的好例子 多谢分享
withchenfang
- 粉丝: 3
- 资源: 63
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功