java课程设计万年历Word格式文档下载.docx
《java课程设计万年历Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《java课程设计万年历Word格式文档下载.docx(24页珍藏版)》请在冰点文库上搜索。
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万年历;
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.length;
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
month=(Integer)boxMonth.getSelectedItem();
//上一年的按钮监听
buttonPreYear.addActionListener(newActionListener(){
intresertYear=(Integer)boxYear.getSelectedItem();
boxYear.setSelectedItem(resertYear-1);
//下一年的按钮监听
buttonNextYear.addActionListener(newActionListener()
boxYear.setSelectedItem(resertYear+1);
//上一月的监听
buttonPreMonth.addActionListener(newActionListener()
intresertMonth=(Integer)boxMonth.getSelectedItem();
if(resertMonth==1)
{
if(resertYear==1901)
{
boxMonth.setSelectedItem(resertMonth);
boxYear.setSelectedItem(resertYear);
}
else{
resertMonth=12;
boxYear.setSelectedItem(resertYear-1);
}else
boxMonth.setSelectedItem(resertMonth-1);
}
//下一月的监听
buttonNextMonth.addActionListener(newActionListener()
if(resertMonth==12)
resertMonth=1;
boxMonth.setSelectedItem(resertMonth);
intresertYear=(Integer)boxYear.getSelectedItem();
boxYear.setSelectedItem(resertYear+1);
boxMonth.setSelectedItem(resertMonth+1);
//封装给年份下拉列表赋值的操作
privatevoidinitBoxYear()
{
for(in