问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

编程制作,用java制作万年历

发布网友 发布时间:2022-04-29 06:35

我来回答

1个回答

热心网友 时间:2022-06-20 17:28

package com;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;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;public class Calender2 extends JFrame implements ActionListener, ItemListener { private static final long serialVersionUID = 1L; public static void main(String 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", "WED", "THU", "FRI", "SAT" }; private String[] stringWeekCn = new String[] { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; private String[] stringMonth = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" }; private String[] strSysTime = new String[6]; // 存储当前日期信息 private String[] strSysNowTime = new String[6]; // 存储运行时日期信息 private JButton[] buttonDay = new JButton[42]; private JButton[] buttonWeek = new JButton[7]; private JLabel labelMonth = new JLabel(); private JButton buttonToday = new JButton(); private JButton buttonLastMonth = new JButton(); private JButton buttonNextMonth = new JButton(); private JComboBox comboYear = new JComboBox(); private JComboBox comboMonth = new JComboBox(); public Calender2() { super("万年历"); 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.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.setFont(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(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); buttonLastMonth.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("新宋体", Font.PLAIN, 14)); buttonToday.setText("今天"); buttonToday.addActionListener(this); getContentPane().add(buttonNextMonth); buttonNextMonth.setForeground(Color.BLUE); buttonNextMonth.setFont(new Font("新宋体", Font.PLAIN, 14)); buttonNextMonth.setText("下月"); buttonNextMonth.addActionListener(this); for (int i = 0; i < 7; i++) { buttonWeek[i] = new JButton(); if (i == 0 || i == 6) { buttonWeek[i].setForeground(Color.RED); } else { buttonWeek[i].setForeground(Color.BLUE); } buttonWeek[i].setFont(new Font("新宋体", Font.PLAIN, 12)); buttonWeek[i].setText(stringWeekCn[i]); getContentPane().add(buttonWeek[i]); } for (int i = 0; i < 42; i++) { buttonDay[i] = new JButton(); buttonDay[i].setText(""); getContentPane().add(buttonDay[i]); } this.setResizable(false); getSysNowTimeInfo(); setNowDate(); setNowDate(); } public void setSysDate(int year, int month) { // 将日期设置为year年month月1日 gregorianCalendar.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 getSysNowTimeInfo() { // 得到程序运行时的时间信息并存储在字符串数组strSysNowTime中 date = gregorianCalendar.getTime(); strSysNowTime = (date + "").split(" "); } public void getSysTimeInfo() { // 得到系统当前的时间信息并存储在字符串数组strSysTime中 date = gregorianCalendar.getTime(); strSysTime = (date + "").split(" "); } public int getNowMonth() { int month = 0; for (int i = 0; i < 12; i++) { if (strSysNowTime[1].equalsIgnoreCase(stringMonth[i])) { 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(stringWeek[i])) { strat = i; break; } } return strat; } public void setNowDate() { // 将时间设置为程序运行时的时间 setSysTime(getNowYear(), getNowMonth()); getSysTimeInfo(); setDayNull(); getDay(getMonthDays(getNowYear(), getNowMonth() - 1), getMonthDays( getNowYear(), getNowMonth()), weekStrat(strSysTime[0]), getNowDay()); comboYear.setSelectedIndex(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() + move), weekStrat(strSysTime[0]), -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(getYear() - 1900); } comboMonth.setSelectedIndex(getMonth() + move); } } public void setSysTime(int year, int month) { gregorianCalendar.set(year, month, 1); } public int getNowYear() { return Integer.parseInt(strSysNowTime[5]); } public int getNowDay() { return Integer.parseInt(strSysNowTime[2]); } public int getYear() { return comboYear.getSelectedIndex() + 1900; } public int getMonth() { return comboMonth.getSelectedIndex(); } public void setDayNull() { for (int d = 0; d < 42; d++) { buttonDay[d].setText(""); } } public void getDay(int lastMonDays, int monthDays, int startWeek, int day) { // 设置日期颜色并打印 for (int d = 0; d < startWeek + 1; d++) { buttonDay[d].setForeground(Color.GRAY); buttonDay[d].setText((lastMonDays - startWeek) + d + 1 + ""); } for (int d = startWeek; d < startWeek + monthDays; d++) { if ((d - startWeek + 1) == day) { buttonDay[d].setForeground(Color.blue); } else if (d % 7 == 0 || d % 7 == 6) { buttonDay[d].setForeground(Color.RED); } else { buttonDay[d].setForeground(Color.BLACK); } buttonDay[d].setText(d - startWeek + 1 + ""); } for (int d = monthDays + startWeek; d < 42; d++) { buttonDay[d].setForeground(Color.GRAY); buttonDay[d].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; } }}
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
沙尘暴属于自然灾害吗 星配什么字好听男孩 女孩带星字叫什么名字好听又洋气 带星字优美人名 星字如何取名 带星字的名活泼的 非常潮流的星字名 成华区社保局地址 有什么平价又好用的防晒霜值得安利? 有哪些性价比比较高的物理防晒霜可以安利? 用JAVA编程万年历 《僧侣之夜》类似的还有吗? 与僧侣的jh之夜谁有 4月新番《与僧侣交合的之夜》只有五分钟会变搞笑番吗 求僧侣之夜的漫画,漫画! 求求僧侣之夜的漫画 !漫画不是动漫 谁有僧侣之夜的百度云,谁有啊,帮帮忙,十分感谢 跪求憎侣之夜漫画!漫画!不是动漫!要漫画!有没有中字都可以 日剧《朝五晚九帅气和尚爱上我》的原作是僧侣之夜吗? 僧侣之夜的漫画资源,谁有?&#x0295;&#x0E51;&#x2022;ω&#x2022;&#xF338;&#x0294; 2021《僧侣档》顺序是什么? 求《僧侣之夜》漫画资源!!! 不锈钢管的材质分类 什么是不锈钢管,好用吗 结构用不锈钢无缝钢管的结构用不锈钢管介绍 不锈钢管和无缝钢管的区别是什么 不锈钢管有哪些分类? 什么是不锈钢机械结构用管? 己婚女孩梦见花蛇在追赶自己意味着什么 梦见一条好大的花蛇追着我,要说怀孕的话也不可能,我老公都不在家 java程序的万年历编程代码? java编程一个简单的万年历 关于用 java 语言编写的万年历的解读 关于这个路由器TP-LINK TL-WDR6300 1200M 11AC双频无线路由器 我想用VB做一个拼图的简单游戏,分成9个就够了,用键盘上下左右键移动的,图片希望和星座有关,谢谢啊 哪个姿势最诱人?美女搔首弄姿各显风骚 Java Web项目开发案例精粹的介绍 电脑装系统前怎么设置 如何利用visual c++6.0制作一个拼图游戏 除了闯王和八大王之外明朝的首领还有什么绰号请我写出三个? 汝州之战,本是大明最后一次机会,为何孙传庭被逼着走进深渊? 孙传庭为什么打不过李自成? 郏县之战,明军孙传庭是如何失败的? 明末三十六营都有谁 汝州之战的详细分析 孙传庭的《孙传庭传》 明末农民军有32营,李自成叫闯王,其他人叫什么 三十六营的三十六营 “方”字开头的词语有哪些? 方字开头的词语