首页 > 代码库 > 第4次作业类测试代码+093+王坤煌

第4次作业类测试代码+093+王坤煌

1.类图:

技术分享技术分享

2.界面和相应功能:

新增weekDay()方法:

	public static String weekDay(int m,int d,int y){
		if(m == 1 || m == 2){
			y--;
			m+=12;
			y--;
		}
	
		int week=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7;
		String rtweek = null;
		switch(week){
		case 1:
			rtweek="星期一";break;
		case 2:
			rtweek="星期二";break;
		case 3:
			rtweek="星期三";break;
		case 4:
			rtweek="星期四";break;
		case 5:
			rtweek="星期五";break;
		case 6:
			rtweek="星期六";break;
		case 7:
			rtweek="星期天";break;
		}
		return rtweek;
	}

新增lastDay()方法:

	public static String lastDay(int m,int d,int y){
		String lastday = null;
		if(isLeapYear(y) == true){
			month[2]=29;
		};
		if(m == 1 && d==1){
			y--;
			m = 12;
			d = 31;
		}else if (d == 1){
			m = m-1;
			d = month[m];
		}else{
			d--;
		}
		lastday = y+"年"+m+"月"+d+"日";
		return lastday;		
		
	}

页面:

package com.test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class swing {
	JFrame frame = new JFrame("输出日期");

	public JLabel title = new JLabel("请输入需要计算的年月日");
	public JLabel year = new JLabel("年:");
	public JLabel month = new JLabel("月:");
	public JLabel day = new JLabel("日:");
	public JLabel thisDay = new JLabel("这天是星期:");
	public JLabel nextDay = new JLabel("下一天是:");
	public JLabel lastDay = new JLabel("剩一天是:");

	public JButton ok = new JButton("OK");
	public JButton cancle = new JButton("calcle");

	//文本框
	public JTextField input_year = new JTextField();
	public JTextField input_month = new JTextField();
	public JTextField input_day = new JTextField();
	public JTextField input_thisDay = new JTextField();
	public JTextField input_nextDay = new JTextField();
	public JTextField input_lastDay = new JTextField();

	public swing() {
		
		
		/*
		 * 点击ok按钮实现日期的计算
		 */
		ok.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if (e.getSource() == ok) {
					int iYear = Integer.parseInt(input_year.getText());
					int iMonth = Integer.parseInt(input_month.getText());
					int iDay = Integer.parseInt(input_day.getText());

					String weekday = TestPractice.weekDay(iMonth, iDay, iYear);
					input_thisDay.setText(weekday);

					String lastday = TestPractice.lastDay(iMonth, iDay, iYear);
					input_lastDay.setText(lastday);

					String nextday = TestPractice.NextDate(iYear, iMonth, iDay);
					input_nextDay.setText(nextday);

				}
			}

		});
		
		
		/*
         * 点击取消按钮清空文本框的内容
         */
		cancle.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				if (e.getSource() == cancle) {
					input_year.setText("");
					input_month.setText("");
					input_day.setText("");
					input_thisDay.setText("");
					input_lastDay.setText("");
					input_nextDay.setText("");

				}
			}

		});

		//控件的位置
		title.setBounds(5, 5, 220, 30);
		year.setBounds(5, 35, 30, 30);
		input_year.setBounds(25, 40, 50, 20);
		month.setBounds(80, 35, 30, 30);
		input_month.setBounds(105, 40, 30, 20);
		day.setBounds(140, 35, 30, 30);
		input_day.setBounds(165, 40, 30, 20);
		ok.setBounds(10, 70, 75, 20);
		cancle.setBounds(120, 70, 75, 20);
		thisDay.setBounds(5, 95, 100, 20);
		input_thisDay.setBounds(90, 95, 100, 20);
		nextDay.setBounds(5, 125, 100, 20);
		input_nextDay.setBounds(90, 125, 100, 20);
		lastDay.setBounds(5, 155, 100, 20);
		input_lastDay.setBounds(90, 155, 100, 20);

		//添加控件
		frame.setLayout(null);
		frame.add(title);
		frame.add(year);
		frame.add(month);
		frame.add(day);
		frame.add(thisDay);
		frame.add(lastDay);
		frame.add(nextDay);
		frame.add(input_year);
		frame.add(input_month);
		frame.add(input_day);
		frame.add(input_thisDay);
		frame.add(input_nextDay);
		frame.add(input_lastDay);
		frame.add(ok);
		frame.add(cancle);

		frame.setSize(230, 230);
		frame.setLocation(250, 250);
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		new swing();
	}

}

  

技术分享技术分享

 

第4次作业类测试代码+093+王坤煌