JAVA期末结课作业.doc_第1页
JAVA期末结课作业.doc_第2页
JAVA期末结课作业.doc_第3页
JAVA期末结课作业.doc_第4页
JAVA期末结课作业.doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

JAVA期末结课作业信管1108 强生娜 11301304591 题目JAVA日历小程序2 程序实现的功能JAVA日历小程序构建的简易日历程序,其功能有: (1).可以实现图形用户界面。图形用户界面GUI元素有:创建组建、确定其初始外观、事件处理(2).使用的主要布局有:边界布局、网格布局。事件监听器ActionListener,方法是actionPerformed,它的一般形式是void actionPerformed(ActionEvent e) (3).能以月历形式显示日期与星期。周一至周五、周六日以及当天分别用黑色、红色和蓝色显示。 (4).用户可以通过下拉菜单选择需要显示的年份和月份,只需要点击“跳转日期”按钮便可显示用户需要的那个年月的日历。默认显示的是程序运行时的年月日,并且可以通过点击“现在日期”来刷新日历。JFrame+setSelectedIndex+setLayout+Add+getSelectedItem:void+setText:void3 程序类图结构Calendar+Calendar+windowClose:void+setDay:void+actionPerformed:void+main:void派生类 基类4 方法间调用关系public void windowClose(WindowEvent e) (windowClose方法,用于关闭窗体)Public void actionPerformed(ActionEvent e)(用于事件监听)void setDay()(setDay方法,显示日期)public Calendar()(日历类) public final class Calendar extends JFrame implements ActionListener(主日历框架类)public static void main(String args)(主函数)程序从主函数开始,创建对象ct,调用构造函数public Calendar();在该函数内通过super();调用基类的构造函数。使用now_date.getYear()+1900;调用函数获取年份值,由调用now_date.getMonth();获取月份值。用Year.setSelectedIndex(10);方法设定年份下拉列表为当前年份,通过pane_ym.add(Year_l);调用增加年份标签。使用button_jump.addActionListener(this);方法添加事件监听按钮,通过pane_day.setLayout(new GridLayout(7, 7);设置窗体为7行7列,用this.setDay();调用类内函数方法。pane_parent.setLayout(new BorderLayout()设定布局管理器。year_int = Year.getSelectedItem().toString();调用基类的输出函数。button_dayi.setForeground(Color.blue);调用基类函数设置字体的显示颜色。5 程序代码package javaapplication32;import java.awt.BorderLayout;import java.awt.Color;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.util.Date;import java.util.GregorianCalendar;import javax.swing.*;public final class Calendar extends JFrame implements ActionListener JComboBox Month = new JComboBox(); /月份下拉列表框JComboBox Year = new JComboBox(); /年份下拉列表框JLabel Year_l = new JLabel(年份:); /定义标签JLabel Month_l = new JLabel(月份:); /定义标签Date now_date = new Date(); /获取今天的日期JButton button_day = new JButton49; /定义一个数组用来存放日期JButton button_jump = new JButton(日期跳转); /现实选择日期JButton button_today = new JButton(现在日期); /显示今天日期按钮int now_year = now_date.getYear() + 1900; /获取年份值int now_month = now_date.getMonth(); /获取月份值(当前月份-1)boolean bool = false;String year_int = null; /存放年份int month_int; /存放月份JPanel pane_ym = new JPanel(); /放置下拉列表框和控制按钮面板JPanel pane_day = new JPanel(); /放置日期面板JPanel pane_parent = new JPanel(); /放置以上两个面板/*定义方法绘制面板*/public Calendar() super(JAVA简单小日历); /设定面板标题/*以下几行使得关闭面板时退出程序*/setDefaultCloseOperation(DISPOSE_ON_CLOSE);addWindowListener(new WindowAdapter() public void windowClose(WindowEvent e) System.exit(0););setResizable(false); /面板的大小不能变化/*设定年月年份的区间是当前年份的过去10年到当前年份的未来20年月份正常1-12月*/for (int i = now_year - 10; i = now_year + 20; i+) Year.addItem(i + );for (int i = 1; i 13; i+) Month.addItem(i + );Year.setSelectedIndex(10); /设定年份下拉列表为当前年份pane_ym.add(Year_l); /添加年份标签pane_ym.add(Year); /添加年份下拉列表框Month.setSelectedIndex(now_month); /设定月份下拉列表为当前月份pane_ym.add(Month_l); /添加月份标签pane_ym.add(Month); /添加月份下拉列表框pane_ym.add(button_jump); /添加跳转按钮pane_ym.add(button_today); /添加“现在日期”按钮button_jump.addActionListener(this); /跳转按钮添加 监听事件button_today.addActionListener(this); /“现在日期”按钮添加 监听事件/*年月设定结束*/*初始化日期按钮并绘制*/pane_day.setLayout(new GridLayout(7, 7);for (int i = 0; i 49; i+) button_dayi = new JButton( );pane_day.add(button_dayi);this.setDay(); /调用setDay()方法pane_parent.setLayout(new BorderLayout(); /设定布局管理器setContentPane(pane_day);setContentPane(pane_ym);pane_parent.add(pane_day, BorderLayout.SOUTH);pane_parent.add(pane_ym, BorderLayout.NORTH);setContentPane(pane_parent);pack();show();/*SET DAY 方法,显示日期*/void setDay() if (bool) year_int = now_year + ;month_int = now_month; else year_int = Year.getSelectedItem().toString();month_int = Month.getSelectedIndex();int year_sel = Integer.parseInt(year_int) - 1900; /获得年份值Date dt = new Date(year_sel, month_int, 1); /构造一个日期GregorianCalendar cal = new GregorianCalendar(); /创建一个Calendar实例cal.setTime(dt);String week = 星期日, 星期一, 星期二, 星期三, 星期四, 星期五, 星期六 ;int day; /day中存放某个月份的天数int day_week; /用来存放某个月的第一天是星期几的数值/*将星期添加到前7个按钮中*/for (int i = 0; i 7; i+) button_dayi.setText(weeki);/-/*判断是几月份,根据它来设定day的值*其中二月份要判断是否是闰年*/if (month_int = 0| month_int = 2| month_int = 4| month_int = 6| month_int = 7| month_int = 9| month_int = 11) /这些月份为大月,每月31天day = 31; else if (month_int = 3| month_int = 5| month_int = 8| month_int = 10) day = 30; /小月每月30天 else if (cal.isLeapYear(year_sel) /判断二月是否为闰年day = 29; else day = 28; day_week = 7 + dt.getDay();int count = 1;/*绘制按钮*首先要根据选定的月份的第一天是星期几来确定我们绘制按钮的起始位置* 其中day_week就是我们要绘制的起始位置* 对于那些没有数值可以显示的按钮要置空*/for (int i = day_week; i day_week + day; count+, i+) if (i % 7 = 0| i = 13| i = 20| i = 27| i = 48| i = 34| i = 41) if (i = day_week + now_date.getDate() - 1) button_dayi.setForeground(Color.blue);button_dayi.setText(count + ); else button_dayi.setForeground(Color.red);button_dayi.setText(count + ); else if (i = day_week + now_date.getDate() - 1) button_dayi.setForeground(Color.blue);button_dayi.setText(count + ); else button_dayi.setForeground(Color.black);button_dayi.setText(count + );/*对于没有日期数值显示的按钮进行置空处理*/if (day_week = 0) for (int i = day; i 49; i+) button_dayi.setText( ); else /第一天前面的按钮置空for (int i = 7; i day_week; i+) button_dayi.setText( ); /最后一天后面的按钮置空for (int i = day_week + day; i 49; i+) button_dayi.setText( );/点击按钮产生的效果 Overridepublic void actionPerformed(ActionEvent e) if (e.getSource() = button_jump) bool = false;this.setDay(); /如果点击跳转按钮就调用setDay()方法重新绘制按钮 else if (e.getSource() = button_today) bool = true;this.setDay(); /如果点击现在日期按钮,得到今天的日期Month.setSelectedIndex(now_month); /将月份置为当前月份Year.setSelectedIndex(10); /将年份置为当前年份public static void main(String args) Calendar ct = new Calendar();6 主要程序说明在初始化模块中,首先定义一个公共类 Calendar,它继承自JFrame。ActionListener是一个接口,在实现此接口的类Calendar中,设置组件跳转按钮button_jump和现在日期按钮button_today添加监听器(addActionListener(this),之后在事件处理方法public void actionPerformed (ActionEvent e) 中,对每个事件进行不同处理。在这个类中,定义用到的标签、下拉列表框、按钮,放置面板。public class Calendar extends JFrame implements ActionListener JComboBox Month = new JComboBox(); JComboBox Year = new JComboBox(); JLabel Year_l = new JLabel(年份:); JLabel Month_l = new JLabel(月份:); Date now_date = new Date(); JButton button_day = new JButton49; JButton button_jump = new JButton(日期跳转); JButton button_today = new JButton(现在日期); int now_year = now_date.getYear() + 1900; int now_month = now_date.getMonth(); boolean bool = false;String year_int = null; int month_int; JPanel pane_ym = new JPanel(); JPanel pane_day = new JPanel();JPanel pane_parent = new JPanel(); 设定年月时,由算法设置是当前年份的过去10年到当前年份的未来20年,月份区间为正常1-12月。由于在年份区间中,今年是所有年份中的第十一个,所以Year.setSelectedIndex()的参数为10时,年份刚好是2013。Pane_ym是年月下拉列表框以及两个控制按钮的面板。给“跳转按钮”和“现在日期”按钮添加监听事件。for (int i = now_year - 10; i = now_year + 20; i+) Year.addItem(i + );for (int i = 1; i 13; i+) Month.addItem(i + );Year.setSelectedIndex(10); button_jump.addActionListener(this); button_today.addActionListener(this); 初始化日期按钮并绘制。因为每个月第一天不能确定,即所在列的位置(星期)不能确定,加上第一行的星期序号,画一个7*7网格布局管理器GridLayout,把容器分成指定行和列的若干个网格,每个组件占一个网格,从左向右、从上到下依次把组件添加进来。所有的网格都是同样大小的。GridLayout(7, 7)设置行列数为7*7。pane_day.setLayout(new GridLayout(7, 7);for (int i = 0; i 49; i+) button_dayi = new JButton( );pane_day.add(button_dayi); 根据bool的值,由if语句进行判断执行哪一显示日期的语句块,为true时,显示现在的时间;为false时,显示用户选定的时间。网格第一行,即0至6号格子,内容固定为星期序号,即“星期日”到“星期六”。void setDay() if (bool) year_int = now_year + ;month_int = now_month; else year_int = Year.getSelectedItem().toString();month_int = Month.getSelectedIndex();int year_sel = Integer.parseInt(year_int) - 1900; Date dt = new Date(year_sel, month_int, 1); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(dt);String week = 星期日, 星期一, 星期二, 星期三, 星期四, 星期五, 星期六 ;int day; int day_week; for (int i = 0; i 7; i+) button_dayi.setText(weeki); 判断当前月份的天数,根据它来设定day的值,其中二月份要判断是否是闰年。因为第一行的星期序号已经占用了7个格子,所以day_week的值要加上7,即day_week = 7 + dt.getDay()。if (month_int = 0| month_int = 2| month_int = 4| month_int = 6| month_int = 7| month_int = 9| month_int = 11) day = 31; else if (month_int = 3| month_int = 5| month_int = 8| month_int = 10) day = 30; else if (cal.isLeapYear(year_sel) day = 29; else day = 28;day_week = 7 + dt.getDay(); 绘制GridLayout网格的内容。首先要根据选定的月份的第一天是星期几来确定我们绘制按钮的起始位置。前段代码中的day_week就是我们要绘制的起始位置。对于那些没有数值可以显示的按钮要置空。星期六和星期日以红色字体显示,星期一至星期五以黑色字体显示,当天以蓝色字体显示。对于没有日期数值显示的按钮进行置空处理。Count的值即网格的内容,值从1一直到day(当月的总天数)。int count = 1;for (int i = day_week; i day_week + day; count+, i+) if (i % 7 = 0| i = 13| i = 20| i = 27| i = 48| i = 34| i = 41) if (i = day_week + now_date.getDate() - 1)button_dayi.setForeground(Color.blue);button_dayi.setText(count + ); else button_dayi.setForeground(Color.red);button_dayi.setText(count + ); elseif (i = day_week + now_date.getDate() - 1) button_dayi.setForeground(Color.blue);button_dayi.setText(count + ); else button_dayi.setForeg

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论