第4次作业类测试代码+105032014164+张增进
1、类图。
2、代码及界面。
NextDate.java
public class NextDate { public static String print(Calendar cal) { Date resultDate = cal.getTime(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); return sdf.format(resultDate); } public static void nextDate(Calendar cal) { cal.add(Calendar.DATE, +1); } public static String weekDay(Calendar cal) { String week[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; cal.add(Calendar.DATE, +1); int tmp = cal.get(Calendar.DAY_OF_WEEK); return week[tmp - 1]; } public static void lastDate(Calendar cal) { cal.add(Calendar.DATE, -2); } public int isTrueDate(int year, int month, int day) { if (year > 2050 || year < 1912) { return 1; } else if (month > 12 || month < 1) { return 2; } else if (day > 31 || day < 1) { return 3; } else { return 0; } } }
Frame.java
public class Frame { public static void main(String[] args) { final NextDate nextDate = new NextDate(); /* * 界面设计 */ JFrame frame = new JFrame("日期计算"); frame.setLayout(null); // label 初始化 JLabel title = new JLabel("请输入需要计算的年月日"); JLabel f_year = new JLabel("年:"); JLabel f_month = new JLabel("月:"); JLabel f_day = new JLabel("日:"); JLabel thisWeek = new JLabel("这一天是:"); JLabel nextDay = new JLabel("下一天是:"); JLabel lastDay = new JLabel("上一天是:"); // textfield 初始化 final JTextField t_year = new JTextField(); final JTextField t_month = new JTextField(); final JTextField t_day = new JTextField(); final JTextField t_thisWeek = new JTextField(); final JTextField t_nextDay = new JTextField(); final JTextField t_lastDay = new JTextField(); // button 初始化以及设置监听事件 final JButton submit = new JButton("确定"); final JButton clear = new JButton("清空"); submit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 if (e.getSource() == submit) { String Year = t_year.getText(); String Month = t_month.getText(); String Day = t_day.getText(); int month = 0, year = 0, day = 0; year = Integer.parseInt(Year); month = Integer.parseInt(Month); day = Integer.parseInt(Day); int tmp = nextDate.isTrueDate(year, month, day); switch (tmp) { case 0: Calendar cal = Calendar.getInstance(); cal.set(year, month - 1, day); nextDate.nextDate(cal); t_nextDay.setText(nextDate.print(cal)); nextDate.lastDate(cal); t_lastDay.setText(nextDate.print(cal)); t_thisWeek.setText(nextDate.weekDay(cal)); break; case 1: JOptionPane.showMessageDialog(null, "年份超过范围 "); break; case 2: JOptionPane.showMessageDialog(null, "月份超过范围 "); break; case 3: JOptionPane.showMessageDialog(null, "日期超过范围 "); break; } } } }); clear.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 if (e.getSource() == clear) { t_day.setText(""); t_month.setText(""); t_year.setText(""); t_nextDay.setText(""); t_lastDay.setText(""); t_thisWeek.setText(""); } } }); // 布局和字体设置 Font ft = new Font("宋体", Font.PLAIN, 20);// 设置显示字体 title.setFont(ft); title.setBounds(62, 62, 500, 25); f_year.setFont(ft); f_year.setBounds(80, 124, 50, 25); f_month.setFont(ft); f_month.setBounds(230, 124, 50, 25); f_day.setFont(ft); f_day.setBounds(380, 124, 50, 25); thisWeek.setFont(ft); thisWeek.setBounds(62, 248, 200, 25); lastDay.setFont(ft); lastDay.setBounds(62, 372, 150, 25); nextDay.setFont(ft); nextDay.setBounds(62, 310, 150, 25); t_year.setBounds(130, 124, 50, 25); t_month.setBounds(280, 124, 50, 25); t_day.setBounds(430, 124, 50, 25); t_thisWeek.setBounds(180, 245, 100, 25); t_thisWeek.setFont(ft); t_thisWeek.setEnabled(false); t_nextDay.setBounds(180, 310, 200, 25); t_nextDay.setFont(ft); t_nextDay.setEnabled(false); t_lastDay.setBounds(180, 372, 200, 25); t_lastDay.setFont(ft); t_lastDay.setEnabled(false); submit.setBounds(180, 187, 62, 31); clear.setBounds(320, 187, 62, 31); frame.add(title); frame.add(f_year); frame.add(f_month); frame.add(f_day); frame.add(thisWeek); frame.add(nextDay); frame.add(lastDay); frame.add(t_year); frame.add(t_month); frame.add(t_day); frame.add(t_thisWeek); frame.add(t_nextDay); frame.add(t_lastDay); frame.add(submit); frame.add(clear); frame.setSize(550, 500); frame.setLocation(250, 250); frame.setVisible(true); } }
3.程序运行图