java写万年历的代码-文档.docx

上传人:scccc 文档编号:12053824 上传时间:2021-12-01 格式:DOCX 页数:7 大小:31.49KB
返回 下载 相关 举报
java写万年历的代码-文档.docx_第1页
第1页 / 共7页
java写万年历的代码-文档.docx_第2页
第2页 / 共7页
java写万年历的代码-文档.docx_第3页
第3页 / 共7页
java写万年历的代码-文档.docx_第4页
第4页 / 共7页
java写万年历的代码-文档.docx_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《java写万年历的代码-文档.docx》由会员分享,可在线阅读,更多相关《java写万年历的代码-文档.docx(7页珍藏版)》请在三一文库上搜索。

1、import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.Date; import java.util.GregorianCalendar; import javax.swing.JButton

2、; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; public class Calender2 extends JFrame implements ActionListener, ItemListener /* * 实现简单的日历功能 * * author Jadie version 1.0 2007/12/29 * */ private static final long serialVersionUID = 1L; public static void main(Str

3、ing args) try Calender2 frame = new Calender2(); frame.setVisible(true); catch (Exception e) e.printStackTrace(); private Date date = new Date(); private GregorianCalendar gregorianCalendar = new GregorianCalendar(); private String stringWeek = new String "SUN", "MON", "TUE&

4、quot;, "WED", "THU", "FRI", "SAT" ; private String stringWeekCn = new String "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" ; private String stringMonth = new String "Jan", "

5、Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" ; private String strSysTime = new String6; / 存储当前日期信息 推荐精选private String strSysNowTime = new String6; / 存储运行时日期信息 priva

6、te JButton buttonDay = new JButton42; private JButton buttonWeek = new JButton7; private JLabel labelMonth = new JLabel(); private JButton buttonToday = new JButton(); private JButton buttonLastMonth = new JButton(); private JButton buttonNextMonth = new JButton(); private JComboBox comboYear = new

7、JComboBox(); private JComboBox comboMonth = new JComboBox(); public Calender2() super("万年历-156制作"); getContentPane().setLayout(new GridLayout(8, 7, 3, 5); setBounds(250, 200, 530, 360); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); comboYear.setForeground(new Color(0, 0, 255); comboYear.

8、setFont(new Font("", Font.PLAIN, 14); for (int y = 1900; y < 2101; y+) comboYear.addItem(" " + new Integer(y).toString(); getContentPane().add(comboYear); comboYear.addItemListener(this); final JLabel labelYear = new JLabel(); labelYear.setForeground(Color.BLUE); labelYear.set

9、Font(new Font("新宋体", Font.PLAIN, 14); getContentPane().add(labelYear); labelYear.setText(" 年"); comboMonth.setForeground(new Color(0, 0, 255); comboMonth.setFont(new Font("", Font.PLAIN, 14); for (int m = 1; m < 13; m+) comboMonth.addItem(" " + new Integer(

10、m).toString(); getContentPane().add(comboMonth); comboMonth.addItemListener(this); getContentPane().add(labelMonth); labelMonth.setForeground(Color.BLUE); labelMonth.setFont(new Font("新宋体", Font.PLAIN, 14); labelMonth.setText(" 月"); getContentPane().add(buttonLastMonth); 推荐精选butt

11、onLastMonth.setForeground(Color.BLUE); buttonLastMonth.setFont(new Font("新宋体", Font.PLAIN, 14); buttonLastMonth.setText("上月"); buttonLastMonth.addActionListener(this); getContentPane().add(buttonToday); buttonToday.setForeground(Color.BLUE); buttonToday.setFont(new Font("新宋体

12、", Font.PLAIN, 14); buttonToday.setText("今天"); buttonToday.addActionListener(this); getContentPane().add(buttonNextMonth); buttonNextMonth.setForeground(Color.BLUE); buttonNextMonth.setFont(new Font("新宋体", Font.PLAIN, 14); buttonNextMonth.setText("下月"); buttonNextM

13、onth.addActionListener(this); for (int i = 0; i < 7; i+) buttonWeeki = new JButton(); if (i = 0 | i = 6) buttonWeeki.setForeground(Color.RED); else buttonWeeki.setForeground(Color.BLUE); buttonWeeki.setFont(new Font("新宋体", Font.PLAIN, 12); buttonWeeki.setText(stringWeekCni); getContentP

14、ane().add(buttonWeeki); for (int i = 0; i < 42; i+) buttonDayi = new JButton(); buttonDayi.setText(""); getContentPane().add(buttonDayi); this.setResizable(false); getSysNowTimeInfo(); setNowDate(); setNowDate(); public void setSysDate(int year, int month) / 将日期设置为year年month月1日 gregoria

15、nCalendar.set(year, month, 1); 推荐精选 public void actionPerformed(ActionEvent ae) if (ae.getSource() = buttonToday) setNowDate(); setNowDate(); else if (ae.getSource() = buttonLastMonth) setDate(-1); else setDate(1); public void itemStateChanged(ItemEvent arg0) setDate(0); public void getSysNowTimeInf

16、o() / 得到程序运行时的时间信息并存储在字符串数组strSysNowTime中 date = gregorianCalendar.getTime(); strSysNowTime = (date + "").split(" "); public void getSysTimeInfo() / 得到系统当前的时间信息并存储在字符串数组strSysTime中 date = gregorianCalendar.getTime(); strSysTime = (date + "").split(" "); public

17、 int getNowMonth() int month = 0; for (int i = 0; i < 12; i+) if (strSysNowTime1.equalsIgnoreCase(stringMonthi) month = i; break; return month; public int weekStrat(String strWeek) / 返回字符串strWeek与星期中的第几天匹配,SUN为第一天 int strat = 0; for (int i = 0; i < 7; i+) if (strWeek.equalsIgnoreCase(stringWee

18、ki) 推荐精选strat = i; break; return strat; public void setNowDate() / 将时间设置为程序运行时的时间 setSysTime(getNowYear(), getNowMonth(); getSysTimeInfo(); setDayNull(); getDay(getMonthDays(getNowYear(), getNowMonth() - 1), getMonthDays( getNowYear(), getNowMonth(), weekStrat(strSysTime0), getNowDay(); comboYear.se

19、tSelectedIndex(getNowYear() - 1900); comboMonth.setSelectedIndex(getNowMonth(); public void setDate(int move) / 将时间设置为选中的年月增加move个月之后的时间 setSysTime(getYear(), getMonth() + move); getSysTimeInfo(); setDayNull(); getDay(getMonthDays(getYear(), getMonth() + move - 1), getMonthDays( getYear(), getMonth(

20、) + move), weekStrat(strSysTime0), -1); if (move != 0) if (getMonth() = 0 && move < 0) move = 11; comboYear.setSelectedIndex(getYear() - 1901); else if (getMonth() = 11 && move > 0) move = -11; comboYear.setSelectedIndex(getYear() - 1899); else comboYear.setSelectedIndex(getYea

21、r() - 1900); comboMonth.setSelectedIndex(getMonth() + move); public void setSysTime(int year, int month) gregorianCalendar.set(year, month, 1); 推荐精选public int getNowYear() return Integer.parseInt(strSysNowTime5); public int getNowDay() return Integer.parseInt(strSysNowTime2); public int getYear() re

22、turn comboYear.getSelectedIndex() + 1900; public int getMonth() return comboMonth.getSelectedIndex(); public void setDayNull() for (int d = 0; d < 42; d+) buttonDayd.setText(""); public void getDay(int lastMonDays, int monthDays, int startWeek, int day) / 设置日期颜色并打印 for (int d = 0; d <

23、; startWeek + 1; d+) buttonDayd.setForeground(Color.GRAY); buttonDayd.setText(lastMonDays - startWeek) + d + 1 + ""); for (int d = startWeek; d < startWeek + monthDays; d+) if (d - startWeek + 1) = day) buttonDayd.setForeground(Color.blue); else if (d % 7 = 0 | d % 7 = 6) buttonDayd.set

24、Foreground(Color.RED); else buttonDayd.setForeground(Color.BLACK); buttonDayd.setText(d - startWeek + 1 + ""); for (int d = monthDays + startWeek; d < 42; d+) buttonDayd.setForeground(Color.GRAY); buttonDayd.setText(d - (monthDays + startWeek) + 1 + ""); 推荐精选 public int getMonthDays(int year, int month) / 返回year年month月的天数 switch (month) case 3: case 5: case 8: case 10: return 30; case 1: if (gregorianCalendar.isLeapYear(year) return 29; else return 28; default: return 31; (注:可编辑下载,若有不当之处,请指正,谢谢!) 推荐精选

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1