软件测试作业之下一天

2017, Apr 11    

同样,还是一个作业。
软件测试与质量保证的作业。

效果图

程序入口:

package guoyang;
public class Main {

	public static void main(String[] args) {
		MainFrame m = new MainFrame("NextDate");
		m.setVisible(true);
	}

}


package guoyang;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

/**
 *  @author guoyang
 *
 */

public class MainFrame extends JFrame {

	private JPanel sunPane;// 阳历面板
	private JPanel moonPane;// 阴历面板
	private JLabel[] labels;// 日期小格子
	private int[] months = new int[12];// 阳历每月天数
	private int day = 1;// 阳历几号
	private int month = 1;// 阳历几月
	private int year = 1900;// 阳历年
	private int weekDay = 0;// 阳历周几
	private int sumDayToStart = 0;// 距离1900年1月1日过去了多少天
	
	private String moonCalendar = "";// 农历几月初几
	private String ganzhiCalendar = "";// 农历天干地支纪年纪月纪日
	private String shengxiaoYear = "";// 农历生肖
	
	private JComboBox<String> yearBox;// 年份选择
	private JComboBox<String> monthBox;// 月份选择
	
	private JLabel dateLabel;// 日期标签
	private JLabel moonLabel = new JLabel("");
	private JLabel ganzhiLabel = new JLabel("");
	private JLabel shengxiaoLabel = new JLabel("");
	
	private int[] moon = new int[] { 0x04bd8, 0x04ae0, 0x0a570, 0x054d5,
			0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, 0x04ae0,
			0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2,
			0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40,
			0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566, 0x0d4a0,
			0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7,
			0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0,
			0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550, 0x15355,
			0x04da0, 0x0a5b0, 0x14573, 0x052b0, 0x0a9a8, 0x0e950, 0x06aa0,
			0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263,
			0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0,
			0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0, 0x195a6, 0x095b0,
			0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46,
			0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50,
			0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960, 0x0d954,
			0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0,
			0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0,
			0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0, 0x0ad50,
			0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,
			0x05aa0, 0x076a3, 0x096d0, 0x04afb, 0x04ad0, 0x0a4d0, 0x1d0b6,
			0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0,
			0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0, 0x14b63,
			0x09370, 0x049f8, 0x04970, 0x064b0, 0x168a6, 0x0ea50, 0x06b20,
			0x1a6c4, 0x0aae0, 0x0a2e0, 0x0d2e3, 0x0c960, 0x0d557, 0x0d4a0,
			0x0da50, 0x05d55, 0x056a0, 0x0a6d0, 0x055d4, 0x052d0, 0x0a9b8,
			0x0a950, 0x0b4a0, 0x0b6a6, 0x0ad50, 0x055a0, 0x0aba4, 0x0a5b0,
			0x052b0, 0x0b273, 0x06930, 0x07337, 0x06aa0, 0x0ad50, 0x14b55,
			0x04b60, 0x0a570, 0x054e4, 0x0d160, 0x0e968, 0x0d520, 0x0daa0,
			0x16aa6, 0x056d0, 0x04ae0, 0x0a9d4, 0x0a2d0, 0x0d150, 0x0f252,
			0x0d520 };
	private String[] gan = new String[] { "甲", "乙", "丙", "丁", "戊", "己", "庚",
			"辛", "壬", "癸" };
	private String[] zhi = new String[] { "子", "丑", "寅", "卯", "辰", "巳", "午",
			"未", "申", "酉", "戌", "亥" };
	private String[] shengxiao = new String[] { "鼠", "牛", "虎", "兔", "龙", "蛇",
			"马", "羊", "猴", "鸡", "狗", "猪" };
	
	public MainFrame(String name) {
		super(name);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		// System.out.println(moon.length);
		init();
	}
	
	private void init() {
		sunPane = new JPanel(new BorderLayout());
		moonPane = new JPanel(new FlowLayout());
	
		JPanel contentPane = new JPanel(new BorderLayout());
		contentPane.add(sunPane, BorderLayout.CENTER);
		contentPane.add(moonPane, BorderLayout.EAST);
	
		this.add(contentPane);
	
		JLabel nongli = new JLabel("农历");
		nongli.setFont(new Font("微软雅黑", 1, 48));
	
		moonPane.setLayout(new BoxLayout(moonPane, BoxLayout.Y_AXIS));
	
		moonPane.add(nongli);
		moonPane.setPreferredSize(new Dimension(150,30));
		moonPane.add(moonLabel);
		moonPane.add(ganzhiLabel);
		moonPane.add(shengxiaoLabel);
	
		moonLabel.setFont(new Font("微软雅黑", 1, 24));
		ganzhiLabel.setFont(new Font("微软雅黑", 1, 24));
		shengxiaoLabel.setFont(new Font("微软雅黑", 1, 24));
	
		JPanel upPane = new JPanel();
		sunPane.add(upPane, BorderLayout.NORTH);
	
		yearBox = new JComboBox<String>();
		monthBox = new JComboBox<String>();
	
		JButton nextDate = new JButton("下一天");
	
		for (int i = 1900; i <= 2100; i++) {
			yearBox.addItem(i + "");
		}
		for (int i = 1; i <= 12; i++) {
			monthBox.addItem(i + "月");
		}
	
		dateLabel = new JLabel(year + "-" + month + "-" + day);
	
		upPane.add(yearBox);
		upPane.add(monthBox);
		upPane.add(nextDate);
		upPane.add(dateLabel);
	
		JPanel display = new JPanel(new GridLayout(7, 7));
		sunPane.add(display, BorderLayout.CENTER);
		labels = new JLabel[49];
		for (int i = 0; i < 49; i++) {
			labels[i] = new JLabel(" ", JLabel.CENTER);
			if (i > 6) {
				labels[i].addMouseListener(new MyLabelMouseAdapter());
			}
			display.add(labels[i]);
		}
		labels[0].setText("日");
		labels[1].setText("一");
		labels[2].setText("二");
		labels[3].setText("三");
		labels[4].setText("四");
		labels[5].setText("五");
		labels[6].setText("六");
	
		for (int i = 0; i < 7; i++) {
			labels[i].setForeground(Color.blue);
			labels[i].setBackground(Color.lightGray);
			labels[i].setOpaque(true);
		}
		moonPane.setBackground(Color.ORANGE);
		//JLabel moonLabel = new JLabel(" 农历 ");


		months[0] = 31; // 一月
		months[1] = 28; // 二月
		months[2] = 31; // 三月
		months[3] = 30; // 四月
		months[4] = 31; // 五月
		months[5] = 30; // 六月
		months[6] = 31; // 七月
		months[7] = 31; // 八月
		months[8] = 30; // 九月
		months[9] = 31; // 十月
		months[10] = 30; // 十一月
		months[11] = 31; // 十二月
	
		yearBox.addActionListener(new ActionListener() {
	
			@Override
			public void actionPerformed(ActionEvent e) {
	
				refresh();
	
			}
		});
		monthBox.addActionListener(new ActionListener() {
	
			@Override
			public void actionPerformed(ActionEvent e) {
	
				refresh();
	
			}
		});
		nextDate.addActionListener(new ActionListener() {
	
			@Override
			public void actionPerformed(ActionEvent e) {
				// 下一天
				if (month == 12 && day == 31) {
					year += 1;
					yearBox.setSelectedIndex(year - 1900);
					month = 1;
					monthBox.setSelectedIndex(month - 1);
					day = 1;
	
				} else if (day == months[month - 1])// 每月的最后一天
				{
					month += 1;
					monthBox.setSelectedIndex(month - 1);
					day = 1;
				} else {
					day += 1;
				}
				refresh();
	
			}
		});
	
		setCalendar(year, month);
	
	}
	
	/**
	 * 设置日历,返回当月的第一天是周几,从0开始计数
	 *
	 * @param year
	 * @param month
	 */
	public void setCalendar(int year, int month) {
	
		// int weekDay = 0;//该月份的第一天是周几,从0开始计数,0表示周一
		sumDayToStart = 0;
		for (int i = 1900; i < year; i++) {
			if (isLeepYear(i)) {
				sumDayToStart += 366;
			} else {
				sumDayToStart += 365;
			}
	
		}
	
		if (isLeepYear(year)) {
			months[1] = 29;
		} else {
			months[1] = 28;
		}
		for (int i = 0; i < month - 1; i++) {
			sumDayToStart += months[i];
		}
		weekDay = sumDayToStart % 7;
		// 清空日历
		for (int i = 0; i < 42; i++) {
			labels[i + 7].setText(" ");
			labels[i + 7].setForeground(Color.BLACK);
			labels[i + 7].setBackground(this.getBackground());
	
		}
	
		for (int i = 0; i < months[month - 1]; i++) {
			labels[weekDay + 7 + i + 1].setText(i + 1 + "");
		}
	
		labels[weekDay + day + 7].setForeground(Color.RED);
		labels[weekDay + day + 7].setBackground(Color.WHITE);
		labels[weekDay + day + 7].setOpaque(true);
	
		sumDayToStart += day;
	
	}
	
	/**
	 * 判断是否为闰年
	 *
	 * @param year
	 * @return
	 */
	public boolean isLeepYear(int year) {
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
			return true;
		}
		return false;
	
	}
	
	public void refresh() {
		year = yearBox.getSelectedIndex() + 1900;
		month = monthBox.getSelectedIndex() + 1;
		setCalendar(year, month);
		dateLabel.setText(year + "-" + month + "-" + day);
	
		getMoonCalendar();
		/*this.ganzhiLabel.setHorizontalAlignment(SwingConstants.CENTER);
		this.moonLabel.setHorizontalAlignment(SwingConstants.CENTER);
		this.ganzhiLabel.setHorizontalAlignment(SwingConstants.CENTER);*/
		this.ganzhiLabel.setText(ganzhiCalendar);
		this.moonLabel.setText(moonCalendar);
		this.shengxiaoLabel.setText(shengxiaoYear+"年");
	
		System.out.println(moonCalendar+" "+ganzhiCalendar+" "+shengxiaoYear);


	}
	
	private class MyLabelMouseAdapter extends MouseAdapter {
		public void mouseClicked(MouseEvent e) {
	
			JLabel clickedLabel = (JLabel) e.getSource();
	
			for (int i = 7; i < 49; i++) {
				if (labels[i].equals(clickedLabel)) {
					day = i - 7 - weekDay;
				}
			}
			refresh();
		}
	}
	
	/**
	 * 计算是否为农历闰年
	 *
	 * @param yearIndex
	 *            ,年份索引,从0到200
	 * @return
	 */
	private boolean isMoonLeapYear(int yearIndex) {
		return (0x10000 & moon[year]) == 0x10000;
	}
	
	/**
	 * 计算农历每年每月有多少天
	 *
	 * @param year
	 *            ,年份索引,从0到200
	 * @return
	 */
	private int[] getMoonMonthDay(int year) {
		int[] months = new int[12];
	
		for (int i = 0; i < 12; i++) {
			if ((moon[year] & (0x10000 >>> (i + 1))) == (0x10000 >>> (i + 1))) {
				months[i] = 30;
			} else {
				months[i] = 29;
			}
		}
	
		return months;
	}
	
	/**
	 * 获得农历日期,第多少年,第几月,第几天
	 */
	private void getMoonCalendar() {
		int year = 0;
		int month = 0;
		int day = 0;
		int sum = 0;
		if (this.sumDayToStart < 31) {
			year = -1;
			month = 11;
			day = this.sumDayToStart;
		} else {
			sumDayToStart -= 30;
			while (sum + getMoonYearDays(year) < this.sumDayToStart) {
				sum += getMoonYearDays(year);
				year++;
			}
			int leapMonth = 0;
			int leapMonthDay = 0;
			int i = 0;
			int[] months = getMoonMonthDay(year);
			if (moon[year] % 16 != 0) { // 该年是阴历闰年
				if ((moon[year] & 0x10000) == 0x10000) { // 该年闰大月
					leapMonthDay = 30;
				} else { // 该年闰小月
					leapMonthDay = 29;
				}
				leapMonth = moon[year] % 16; // 闰几月
				for (i = 0; i < leapMonth; i++) {
					if (sum + months[i] < sumDayToStart) {
						sum += months[i];
					} else {
						break;
					}
				}
				if (i == leapMonth) { // 超过闰月了
					if (sum + leapMonthDay < sumDayToStart) {
						sum += leapMonthDay;
						for (i = leapMonth; i < 12; i++) {
							if (sum + months[i] < sumDayToStart) {
								sum += months[i];
							} else {
								break;
							}
						}
						month = i;
						day = sumDayToStart - sum;
					} else { // 在闰几月的时候停下
						day = sumDayToStart - sum;
					}
				} else {
					month = i;
					day = sumDayToStart - sum;
				}
			} else {
				for (i = 0; i < 12; i++) {
					if (sum + months[i] < sumDayToStart) {
						sum += months[i];
					} else {
						break;
					}
				}
				month = i;
				day = sumDayToStart - sum;
			}
		}
		// System.out.println("阴历一年多少天"+getMoonYearDays(year));
		this.moonCalendar=getYinliMonth(month)+getYinliDay(day);
		this.shengxiaoYear=shengxiao[(year+12)%12];
		/*System.out.println(this.moonCalendar);
		System.out.println(shengxiao[(year+12)%12]);
		System.out.println("阳历:" + this.year + "-" + this.month + "-"
				+ this.day + "阴历:" + year + "-" + month + "-" + day);// 测试农历几月初几
*/		this.ganzhiCalendar=getGanZhi(year, month, day);
		/*System.out.println(ganzhiCalendar);*/
	}

	/**
	 * 计算农历每年共有多少天
	 *
	 * @param yearIndex
	 *            ,年份索引,从0到200
	 * @return
	 */
	private int getMoonYearDays(int yearIndex) {
		int sumDay = 0;
		int[] months = getMoonMonthDay(yearIndex);
		for (int i = 0; i < 12; i++) {
			sumDay += months[i];
		}
		if (moon[yearIndex] % 16 != 0) {
			if ((moon[yearIndex] & 0x10000) == 0x10000) {
				sumDay += 30;
			} else {
				sumDay += 29;
			}
		}
		return sumDay;
	
	}
	private String getYinliMonth(int month){
	
		switch(month){
		case 0:
			return "正月";
		case 1:
			return "二月";
		case 2:
			return "三月";
		case 3:
			return "四月";
		case 4:
			return "五月";
		case 5:
			return "六月";
		case 6:
			return "七月";
		case 7:
			return "八月";
		case 8:
			return "九月";
		case 9:
			return "十月";
		case 10:
			return "十一月";
		case 11:
			return "腊月";
		}
		return "";


	}
	private String getYinliDay(int day){
	
		switch(day){
		case 1:
			return "初一";
		case 2:
			return "初二";
		case 3:
			return "初三";
		case 4:
			return "初四";
		case 5:
			return "初五";
		case 6:
			return "初六";
		case 7:
			return "初七";
		case 8:
			return "初八";
		case 9:
			return "初九";
		case 10:
			return "初十";
		case 11:
			return "十一";
		case 12:
			return "十二";
		case 13:
			return "十三";
		case 14:
			return "十四";
		case 15:
			return "十五";
		case 16:
			return "十六";
		case 17:
			return "十七";
		case 18:
			return "二十八";
		case 19:
			return "十九";
		case 20:
			return "二十";
		case 21:
			return "二十一";
		case 22:
			return "二十二";
		case 23:
			return "二十三";
		case 24:
			return "二十四";
		case 25:
			return "二十五";
		case 26:
			return "二十六";
		case 27:
			return "二十七";
		case 28:
			return "二十八";
		case 29:
			return "二十九";
		case 30:
			return "三十";
	
		}
		return "";
	}
	private String getGanZhi(int year,int month,int day){
		year=year+36;
		String ganzhiYear="";
		String ganzhiMonth="";
		String[] ganzhi = new String[60];
		for(int i=0,j=0,k=0;k<60;i=(++i)%10,j=(++j)%12,k++){
			ganzhi[k] = gan[i]+zhi[j];
		}
		ganzhiYear=ganzhi[year%60]+"年";
		ganzhiMonth=ganzhi[(year*12+month+1)%60]+"月";
		System.out.println((year*12+month+1)%60);
		return ganzhiYear+ganzhiMonth;
	}
}