windows日历时钟.docx
《windows日历时钟.docx》由会员分享,可在线阅读,更多相关《windows日历时钟.docx(12页珍藏版)》请在冰豆网上搜索。
windows日历时钟
综合考查
题目:
Windows日历时钟软件
姓名:
学号:
成绩:
日期:
2011-01-5
源代码
文件名:
Main0.java
importjava.awt.*;
importjavax.swing.*;
publicclassmain0extendsJFrame{
privatestaticfinallongserialVersionUID=1L;
publicmain0(){
super("时间日期");//时间和日期
JPaneldt=newJPanel();//创建面板
dt.setLayout(newGridLayout());//网格布局管理器
rilix=newrili();
x.init();
dt.add(x);
dt.add(newClock());//时区
JPaneltimeArea=newJPanel();//Internet时间
JPanelinternetTime=newJPanel();
//创建属性页tabbedPane
JTabbedPanetabbedPane=newJTabbedPane();
tabbedPane.addTab("时间和日期",dt);
tabbedPane.addTab("时区",timeArea);
tabbedPane.addTab("Internet时间",internetTime);
tabbedPane.setSelectedIndex(0);//设置默认版位第一版
getContentPane().add(tabbedPane,BorderLayout.CENTER);//把属性页加到frame中去
}
publicstaticvoidmain(String[]args){
JFrame.setDefaultLookAndFeelDecorated(true);
JFrameframe=newmain0();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setSize(400,250);
}
}
文件名:
Rili.java
importjava.awt.*;
importjava.awt.event.*;
importjava.util.*;
importjavax.swing.*;
importjavax.swing.event.*;
importjavax.swing.table.*;
publicclassriliextendsJApplet{
/*定义全局变量*/
publicstaticfinalColorbackground=Color.white;//背景色
publicstaticfinalColorforeground=Color.black;//前景色
publicstaticfinalColorheaderBackground=Color.DARK_GRAY;//星期
publicstaticfinalColorheaderForeground=Color.white;//星期前景色
publicstaticfinalColorselectedBackground=Color.blue;//选中背景色
publicstaticfinalColorselectedForeground=Color.white;//选中前景色
publicstaticfinalStringWeekSun="日";//星期标签名称
publicstaticfinalStringWeekMon="一";
publicstaticfinalStringWeekTue="二";
publicstaticfinalStringWeekWed="三";
publicstaticfinalStringWeekThu="四";
publicstaticfinalStringWeekFri="五";
publicstaticfinalStringWeekSat="六";
privateJPanelMainPanel;//日历面板
privateJLabelyearsLabel;//“年份”标签
privateJSpinneryearsSpinner;//年份组合框
privateJLabelmonthsLabel;//“月份”标签
privateJComboBoxmonthsComboBox;//12月份下拉框
privateJLabelInfoLabel;//信息显示标签
privateJTabledaysTable;//日表格
privateAbstractTableModeldaysModel;//天单元表格
privateCalendarcalendar;//日历对象
/*函数定义*/
publicrili(){//构造函数
MainPanel=(JPanel)getContentPane();
}
publicvoidinit(){//初始化面板界面函数
MainPanel.setLayout(newBorderLayout());
calendar=Calendar.getInstance();//默认方式,以本地的时区和地区来构造Calendar
yearsLabel=newJLabel("年");//设置年份标签显示
yearsSpinner=newJSpinner();//构造年份spinner组合框
yearsSpinner.setEditor(newJSpinner.NumberEditor(yearsSpinner,"0000"));
yearsSpinner.setValue(newInteger(calendar.get(Calendar.YEAR)));
yearsSpinner.addChangeListener(newChangeListener(){//注册该组合框的事件监听器
publicvoidstateChanged(ChangeEventchangeEvent){
intday=calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH,1);
calendar.set(Calendar.YEAR,((Integer)yearsSpinner.getValue()).intValue());
intmaxDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH,day>maxDay?
maxDay:
day);
updateView();//更新显示
}
}
);
//--------------------------------------
monthsComboBox=newJComboBox();//月份下拉框
monthsLabel=newJLabel("月");//设置月份标签显示
for(inti=1;i<=12;i++){//构造下拉框的12个月份
monthsComboBox.addItem(newInteger(i));
}
monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));//下拉框当前月份为选中状态
monthsComboBox.addActionListener(newActionListener(){//注册月份下拉框的事件监听器
publicvoidactionPerformed(ActionEventactionEvent){
intday=calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH,1);
calendar.set(Calendar.MONTH,monthsComboBox.getSelectedIndex());
intmaxDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH,day>maxDay?
maxDay:
day);
updateView();//更新面板显示
}
});
//--------------------------------------
InfoLabel=newJLabel("当前时区:
中国标准时间");
JPanelyearMonthPanel=newJPanel();//定义年月面板
MainPanel.add(yearMonthPanel,BorderLayout.NORTH);//添加年月面板到日历面板的南面(最上方)
MainPanel.add(InfoLabel,BorderLayout.SOUTH);
yearMonthPanel.setLayout(newBorderLayout());//边布局模式
JPanelyearPanel=newJPanel();//构建年份面板
yearMonthPanel.add(yearPanel,BorderLayout.WEST);//年份面板添加到年月面板西部(左边)
yearPanel.setLayout(newBorderLayout());//设置年份面板为边布局并添加年份标签和组合框
yearPanel.add(yearsLabel,BorderLayout.EAST);
yearPanel.add(yearsSpinner,BorderLayout.CENTER);
//--------------------------------------
JPanelmonthPanel=newJPanel();//定义月份面板
yearMonthPanel.add(monthPanel,BorderLayout.EAST);//添加月份面板到年月面板的东面(右边)
monthPanel.setLayout(newBorderLayout());//月份面板设为边布局方式
monthPanel.add(monthsLabel,BorderLayout.EAST);//添加月份名称标签到月份面板西面(左边)
monthPanel.add(monthsComboBox,BorderLayout.CENTER);//添加月份下拉框到月份面板中间
//--------------------------------------
JPaneltxetPanel=newJPanel();//定义标题文本显示面板
yearMonthPanel.add(txetPanel,BorderLayout.CENTER);//添加标题文本显示面板到年月面板中间
daysModel=newAbstractTableModel(){//设置7行7列
publicintgetRowCount(){
return7;
}
publicintgetColumnCount(){
return7;
}
publicObjectgetValueAt(introw,intcolumn){
if(row==0){//第一行显示星期
returngetHeader(column);
}
row--;
Calendarcalendar=(Calendar)rili.this.calendar.clone();
calendar.set(Calendar.DAY_OF_MONTH,1);
intdayCount=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
intmoreDayCount=calendar.get(Calendar.DAY_OF_WEEK)-1;
intindex=row*7+column;
intdayIndex=index-moreDayCount+1;
if(indexdayCount){
returnnull;
}else{
returnnewInteger(dayIndex);
}
}
};
daysTable=newCalendarTable(daysModel,calendar);//构造日表格
daysTable.setCellSelectionEnabled(true);//设置表格单元格可选择
daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
daysTable.setDefaultRenderer(daysTable.getColumnClass(0),newTableCellRenderer(){
publicComponentgetTableCellRendererComponent(JTabletable,Objectvalue,booleanisSelected,
booleanhasFocus,introw,intcolumn){
Stringtext=(value==null)?
"":
value.toString();
JLabelcell=newJLabel(text);
cell.setOpaque(true);//绘制边界内的所有像素
if(row==0){//第一行显示星期,设置为星期的前景色和背景色
cell.setForeground(headerForeground);
cell.setBackground(headerBackground);
}else{
if(isSelected){//日期单元格如果选中,则设置为日期选中的前、背景色
cell.setForeground(selectedForeground);
cell.setBackground(selectedBackground);
}else{//设置日期单元格的普通前、背景色
cell.setForeground(foreground);
cell.setBackground(background);
}
}
returncell;
}
});
updateView();
MainPanel.add(daysTable,BorderLayout.CENTER);//添加日面板到日历面板中间
}
//--------------------------------------
publicstaticStringgetHeader(intindex){//设置第一行星期的显示
switch(index){
case0:
returnWeekSun;
case1:
returnWeekMon;
case2:
returnWeekTue;
case3:
returnWeekWed;
case4:
returnWeekThu;
case5:
returnWeekFri;
case6:
returnWeekSat;
default:
returnnull;
}
}
publicvoidupdateView(){//更新面板显示方法
daysModel.fireTableDataChanged();
daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH),
calendar.get(Calendar.WEEK_OF_MONTH));
daysTable.setColumnSelectionInterval(calendar.get(Calendar.DAY_OF_WEEK)-1,
calendar.get(Calendar.DAY_OF_WEEK)-1);
}
publicstaticclassCalendarTableextendsJTable{//表格类
privateCalendarcalendar;
publicCalendarTable(TableModelmodel,Calendarcalendar){//构造方法
super(model);
this.calendar=calendar;
}
publicvoidchangeSelection(introw,intcolumn,booleantoggle,booleanextend){//选择表格单元格时
super.changeSelection(row,column,toggle,extend);
if(row==0){//选择为第一行(星期)时不改变单元格
return;
}
Objectobj=getValueAt(row,column);
if(obj!
=null){
calendar.set(Calendar.DAY_OF_MONTH,((Integer)obj).intValue());
}
}
}
}
文件名:
Clock.java
importjava.awt.*;
importjavax.swing.*;
importjava.util.*;
importjava.text.*;
publicclassClockextendsJPanelimplementsRunnable{
privateJLabeljl;
privateDateFormatdf;
publicClock(){
jl=newJLabel();
jl.setHorizontalAlignment(JLabel.CENTER);
df=DateFormat.getDateTimeInstance();
newThread(this).start();
this.setLayout(newBorderLayout());
this.add(jl,BorderLayout.SOUTH);
}
publicvoidrun(){
while(true){
try{
Thread.sleep(1000);
}
catch(InterruptedExceptionie){
ie.printStackTrace();
}
jl.setText(df.format(newDate()));
repaint();
}
}
publicvoidpaintComponent(Graphicsg){
super.paintComponent(g);
Calendarcal=Calendar.getInstance();
//得到当前的时间信息
inthour=cal.get(Calendar.HOUR);
intminute=cal.get(Calendar.MINUTE);
intsecond=cal.get(Calendar.SECOND);
//得到当前的面板的大小信息
intwidth=this.getWidth();
intheight=this.getHeight();
//钟的那个圆盘取两个之中小的那个
intsmall=widthwidth:
height;
intdiameter=(int)(small*0.8);
intradius=diameter/2;
//确定中心点
Pointcenter=newPoint(width/2,height/2);
//确定时针,分钟,秒针的长度
intsecondLength=(int)(radius*0.8);
intminuteLength=(int)(secondLength*0.8);
inthourLength=(int)(minuteLength*0.8);
//确定时针,分针,秒针的另一端坐标
intsecondX=center.x+(int)(secondLength*Math.sin(second*2*Math.PI/60.0));
intsecondY=center.y-(int)(secondLength*Math.cos(second*2*Math.PI/60.0));
intminuteX=center.x+(int)(minuteLength*Math.sin(minute*2*Math.PI/60.0));
intminuteY=center.y-(int)(minuteLength*Math.cos(minute*2*Math.PI/60.0));
inthourX=center.x+(int)(hourLength*Math.sin((minute/60.0+hour)*Math.PI/6.0));
inthourY=center.y-(int)(hourLength*Math.cos((minute/60.0+hour)*Math.PI/6.0));
Graphics2Dg2d=(Graphics2D)g;
//画表盘和刻度
g.drawOval(center.x-radius,center.y-radius,dia