java课程设计万年历.docx
《java课程设计万年历.docx》由会员分享,可在线阅读,更多相关《java课程设计万年历.docx(24页珍藏版)》请在冰豆网上搜索。
java课程设计万年历
1.设计分析:
1.1需求分析
对于万年历系统,其主要功能是:
查看当日日期(年、月、日)、查看当时时间、查看其他年月的月历、对选中的日期进行标识。
主要使用流程是:
查看万年历系统,查看其他月份或年份日历,选中其他日期查看,查看当日日期。
1.2功能设计
本程序要构建的万年历程序,其功能有以下几个方面:
(1)通过JDK环境运行,实现图像界面。
(2)能以月历的形式显示日期与星期。
(3)支持用户自己用下拉形式来选择年份和月份。
(4)通过点击“更新时间”按钮来刷新当前时间。
2.程序结构:
3.各模块功能及程序说明:
3.1各种包的导入:
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Dimension;
importjava.awt.GridLayout;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.util.Calendar;
importjava.util.GregorianCalendar;
importjavax.swing.JButton;
importjavax.swing.JComboBox;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
3.2控件的初始化
contentPane=newJPanel(newBorderLayout());//主面板
northPane=newJPanel(newBorderLayout());//放按钮面板的面板
centerPane=newJPanel(newGridLayout(7,7));//中间的日期(七行七列)面板buttonPane=newJPanel();//按钮面板
labelYear=newJButton("年份:
");//显示年份文字
labelMonth=newJButton("月份:
");//显示月份文字
buttonPreYear=newJButton("<<");//上一年按钮
buttonNextYear=newJButton(">>");//下一年的按钮
buttonPreMonth=newJButton("<<");//上一月按钮
buttonNextMonth=newJButton(">>");//下一月按钮
newTime=newJButton("更新时间");
boxYear=newJComboBox();//年的下拉列表
boxMonth=newJComboBox();//月的下拉列表
ca=newGregorianCalendar();
Jhour=newJTextField
(2);
Jminute=newJTextField
(2);
Jsecond=newJTextField
(2);
date=newJPanel();
dateTime1=newJLabel("时");
dateTime2=newJLabel("分");
dateTime3=newJLabel("秒");
3.3绘制万年历的界面
publicWanNianLi(){
this.setTitle("万年历");
this.setSize(500,400);
//将面板居中处理
Dimensiond=Toolkit.getDefaultToolkit().getScreenSize();
intscreenWidth=d.width;
intscreenHeight=d.height;
this.setLocation((screenWidth-this.getWidth())/2,
(screenHeight-this.getHeight())/2);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(true);
}
3.4增加控件
//把按钮放到了按钮面板里
buttonPane.add(labelYear);
buttonPane.add(buttonPreYear);
buttonPane.add(boxYear);
buttonPane.add(buttonNextYear);
buttonPane.add(labelMonth);
buttonPane.add(buttonPreMonth);
buttonPane.add(boxMonth);
buttonPane.add(buttonNextMonth);
//把按钮面板放到了northPane中
northPane.add(buttonPane,BorderLayout.CENTER);
//把northPane和centerPane放到主面板中
contentPane.add(northPane,BorderLayout.NORTH);
contentPane.add(centerPane,BorderLayout.CENTER);
contentPane.add(date,BorderLayout.SOUTH);
3.5增加监听
//给年份下拉列表添加事件监听器
boxYear.addActionListener(newActionListener())
//给月份下拉列表添加事件监听器
boxMonth.addActionListener(newActionListener())
//上一年的按钮监听
buttonPreYear.addActionListener(newActionListener())
//下一年的按钮监听
buttonNextYear.addActionListener(newActionListener()
//上一月的监听
buttonPreMonth.addActionListener(newActionListener()
//下一月的监听
buttonNextMonth.addActionListener(newActionListener()
//更新时间按钮的监听
newTime.addActionListener(newActionListener())
3.6封装操作
//封装给年份下拉列表赋值的操作
privatevoidinitBoxYear()
//封装给月份下拉列表赋值的操作
privatevoidinitBoxMonth()
4.源程序
package万年历;
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Dimension;
importjava.awt.GridLayout;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.util.Calendar;
importjava.util.GregorianCalendar;
importjavax.swing.JButton;
importjavax.swing.JComboBox;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
publicclassWanNianLiextendsJFrame
{
privateinttemp0=0;
privateintyear=0;
privateintmonth=0;
privateinthour=0;
privateintminute=0;
privateintsecond=0;
Calendarca=null;
privateJTextFieldJhour=null;
privateJTextFieldJminute=null;
privateJTextFieldJsecond=null;
privateJPanelcontentPane=null;//主面板
privateJPanelnorthPane=null;//放按钮面板的面板
privateJPanelcenterPane=null;//中间的日期面板
privateJPanelbuttonPane=null;//按钮面板
privateJPaneltime=null;//时钟面板
privateJPaneldate=null;
privateJButtonlabelYear=null;//显示年份文字
privateJButtonlabelMonth=null;//显示月份文字
privateJButtonbuttonPreYear=null;//上一年按钮
privateJButtonbuttonNextYear=null;//下一年的按钮
privateJButtonbuttonPreMonth=null;//上一月按钮
privateJButtonbuttonNextMonth=null;//下一月按钮
privateJButtonnewTime=null;
privateJComboBoxboxYear=null;//年的下拉列表
privateJComboBoxboxMonth=null;//月的下拉列表
privateJLabel[]labelDays=null;//存储天
privateJLabeldateTime1=null;
privateJLabeldateTime2=null;
privateJLabeldateTime3=null;
publicWanNianLi(){
this.setTitle("万年历");
this.setSize(500,400);
//将面板居中处理
Dimensiond=Toolkit.getDefaultToolkit().getScreenSize();
intscreenWidth=d.width;
intscreenHeight=d.height;
this.setLocation((screenWidth-this.getWidth())/2,
(screenHeight-this.getHeight())/2);
init();//封装初始化主控件的操作
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(true);
}
//初始化主控件
privatevoidinit(){
contentPane=newJPanel(newBorderLayout());//主面板
northPane=newJPanel(newBorderLayout());//放按钮面板的面板
centerPane=newJPanel(newGridLayout(7,7));//中间的日期(七行七列)面板
buttonPane=newJPanel();//按钮面板
labelYear=newJButton("年份:
");//显示年份文字
labelMonth=newJButton("月份:
");//显示月份文字
buttonPreYear=newJButton("<<");//上一年按钮
buttonNextYear=newJButton(">>");//下一年的按钮
buttonPreMonth=newJButton("<<");//上一月按钮
buttonNextMonth=newJButton(">>");//下一月按钮
newTime=newJButton("更新时间");
boxYear=newJComboBox();//年的下拉列表
initBoxYear();
boxMonth=newJComboBox();//月的下拉列表
initBoxMonth();
ca=newGregorianCalendar();
hour=ca.get(Calendar.HOUR_OF_DAY);
minute=ca.get(Calendar.MINUTE);
second=ca.get(Calendar.SECOND);
Jhour=newJTextField
(2);
Jhour.setText(hour+"");
Jminute=newJTextField
(2);
Jminute.setText(minute+"");
Jsecond=newJTextField
(2);
Jsecond.setText(second+"");
date=newJPanel();
dateTime1=newJLabel("时");
dateTime2=newJLabel("分");
dateTime3=newJLabel("秒");
date.add(Jhour);
date.add(dateTime1);
date.add(Jminute);
date.add(dateTime2);
date.add(Jsecond);
date.add(dateTime3);
date.add(newTime);
//把按钮放到了按钮面板里
buttonPane.add(labelYear);
buttonPane.add(buttonPreYear);
buttonPane.add(boxYear);
buttonPane.add(buttonNextYear);
buttonPane.add(labelMonth);
buttonPane.add(buttonPreMonth);
buttonPane.add(boxMonth);
buttonPane.add(buttonNextMonth);
//把按钮面板放到了northPane中
northPane.add(buttonPane,BorderLayout.CENTER);
//初始化放日期的label
labelDays=newJLabel[49];//存储天
for(inti=0;i{
labelDays[i]=newJLabel("",JLabel.CENTER);
centerPane.add(labelDays[i]);
}
boxYear.setSelectedItem(2013);
boxMonth.setSelectedItem(12);
YMD();//将第一界面设为系统当前事件
setDate();
//把northPane和centerPane放到主面板中
contentPane.add(northPane,BorderLayout.NORTH);
contentPane.add(centerPane,BorderLayout.CENTER);
contentPane.add(date,BorderLayout.SOUTH);
this.add(contentPane);
//更新时分秒
newTime.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
Calendarca=null;
ca=newGregorianCalendar();
inta=ca.get(Calendar.HOUR_OF_DAY);
intb=ca.get(Calendar.MINUTE);
intc=ca.get(Calendar.SECOND);
Jhour.setText(a+"");
Jminute.setText(b+"");
Jsecond.setText(c+"");
}
});
//给年份下拉列表添加事件监听器
boxYear.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
year=(Integer)boxYear.getSelectedItem();
setDate();
}
});
//给月份下拉列表添加事件监听器
boxMonth.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente)
{
month=(Integer)boxMonth.getSelectedItem();
setDate();
}
});
//上一年的按钮监听
buttonPreYear.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
intresertYear=(Integer)boxYear.getSelectedItem();
boxYear.setSelectedItem(resertYear-1);
}
});
//下一年的按钮监听
buttonNextYear.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente)
{
intresertYear=(Integer)boxYear.getSelectedItem();
boxYear.setSelectedItem(resertYear+1);
}
});
//上一月的监听
buttonPreMonth.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente)
{
intresertMonth=(Integer)boxMonth.getSelectedItem();
intresertYear=(Integer)boxYear.getSelectedItem();
if(resertMonth==1)
{
if(resertYear==1901)
{
boxMonth.setSelectedItem(resertMonth);
boxYear.setSelectedItem(resertYear);
}
else{
resertMonth=12;
boxMonth.setSelectedItem(resertMonth);
boxYear.setSelectedItem(resertYear-1);
}
}else
{
boxMonth.setSelectedItem(resertMonth-1);
}
}
});
//下一月的监听
buttonNextMonth.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente)
{
intresertMonth=(Integer)boxMonth.getSelectedItem();
if(resertMonth==12)
{
resertMonth=1;
boxMonth.setSelectedItem(resertMonth);
intresertYear=(Integer)boxYear.getSelectedItem();
boxYear.setSelectedItem(resertYear+1);
}else
{
boxMonth.setSelectedItem(resertMonth+1);
}
}
});
}
//封装给年份下拉列表赋值的操作
privatevoidinitBoxYear()
{
for(in