类测试代码的具体要求如下:
界面操作说明补充:
点击OK,如果输入有效,进行相应的数值计算;如果数值不满足约束,则弹出错误说明,统一为“输入有误,请重新输入”,然后回到初始输入状态。
点击Cancle,表示重置,清空前面的数据,回到初始状态。
NextDate函数问题
String nextdate(int m,int d,int y)
建立界面,至少包含以下元素,但不限于此:
完成上一天方法:String lastDay(int m,int d,int y) ,完成周几的方法:int weekDay(int m,int d,int y)
类图:
主程序:
package theday; import java.util.Calendar; import java.util.Scanner; public class Date { public static void main(String[] args){ System.out.print("请输入日期"); Scanner input = new Scanner(System.in); int year = input.nextInt(); int month = input.nextInt(); int day = input.nextInt(); System.out.println(NextDay(year,month,day)); System.out.println(WeekDay(month,day, year)); System.out.println(LastDay(month,day, year)); } public static boolean LeapYear(int year) { if((year%4==0&&year%100!=0)||(year%400==0)){ //是闰年 return true; } return false; } public static String NextDay(int year, int month, int day){ if(1912 <= year && year <= 2050) { //判断月份是否超出范围 if(1 <= month && month <= 12) { //大月判断,每个月只有31天 if(month == 1||month == 3||month == 5||month == 7||month == 8||month ==10||month == 12) { if(1 <= day && day < 30) return year+"年"+month+"月"+(day+1)+"日"; else if(day == 31) { if(month == 12) return (year+1)+"年1月1日"; else return year+"年"+(month+1)+"月1日"; } else return "日期超出范围"; } //小月判断,每个月只有30天 else if(month == 4||month == 6||month == 9||month == 11) { if(1 <= day && day < 29) return year+"年"+month+"月"+(day+1)+"日"; if(day == 30) return year+"年"+(month+1)+"月"+"1日"; else return "日期超出范围"; } else { if(LeapYear(year)) { if(1 <= day && day < 28) return year+"年"+month+"月"+(day+1)+"日"; if(day == 29) return year+"年"+(month+1)+"月"+"1日"; else return "日期超出范围"; } else { if(1 <= day && day < 27) return year+"年"+month+"月"+(day+1)+"日"; if(day == 28) return year+"年"+(month+1)+"月"+"1日"; else return "日期超出范围"; } } } else return "日期超出范围"; } else { return "日期超出范围"; } } public static int WeekDay(int month,int day,int year){ if(month==1){ month=13; year=year-1; } if(month==2){ month=14; year=year-1; } int h = (int) (day+2*month+3*(month+1)/5+year+year/4-year/100+year/400+1)%7; switch (h) { case 0: return 7; case 1: return 1; case 2: return 2; case 3: return 3; case 4: return 4; case 5: return 5; case 6: return 6; } return 0; } public static String LastDay(int year,int month,int day){ boolean leapYear = LeapYear(year); String r = ""; if(day < 1 || day > 31) { r = "日期超出范围"; } else { if(month != 1 && month != 3){ if(day == 1) { r = year + "年" + (month-1) + "月30日"; } else { r = year + "年" + month + "月" + (day-1) + "日"; } } else { switch(month) { case 1: r = (year-1) + "年12月31日"; break; case 3: if(leapYear) { r = year + "年" + (month-1) + "月29日"; } else { r = year + "年" + (month-1) + "月28日"; } break; } } } if(month != 2){ if(day < 1 || day > 30) { r = "日期超出范围"; } else { if(day == 1) { r = year + "年" + (month-1) + "月31日"; } else { r = year + "年" + month + "月" + (day-1) + "日"; } } } else if (month == 2 && LeapYear(year)){ if(day < 1 || day > 29) { r = "日期超出范围"; } else { if(day == 1) { r = year + "年" + (month-1) + "月31日"; } else { r = year + "年" + month + "月" + (day-1) + "日"; } } } else { if(day < 1 || day > 28) { r = "日期超出范围"; } else { if(day == 1) { r = year + "年" + (month-1) + "月31日"; } else { r = year + "年" + month + "月" + (day-1) + "日"; } } } return r; } }
程序结果:
界面设计代码:
package theday; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.LayoutStyle.ComponentPlacement; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JLabel; import theday.Date; public class frame extends JFrame { private JPanel contentPane; private JTextField year; private JTextField month; private JTextField day; private JTextField textField_3; private JTextField textField_4; private JTextField textField_5; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { frame frame = new frame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public frame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 510, 417); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); JButton btnOk = new JButton("OK"); btnOk.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String Year = year.getText(); String Month = month.getText(); String Day = day.getText(); int month = 0, year = 0, day = 0; year = Integer.parseInt(Year); month = Integer.parseInt(Month); day = Integer.parseInt(Day); int week = Date.WeekDay(month, day, year); String Next = Date.NextDay(year, month, day); String last = Date.LastDay(year, month, day); textField_3.setText(Integer.toString(week)); textField_4.setText(last); textField_5.setText(Next); } }); JButton btnNewButton = new JButton("Cancle"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { year.setText(""); month.setText(""); day.setText(""); textField_3.setText(""); textField_4.setText(""); textField_5.setText(""); } }); JLabel label = new JLabel("\u5E74"); JLabel label_1 = new JLabel("\u6708"); JLabel label_2 = new JLabel("\u65E5"); JLabel label_3 = new JLabel("\u8FD9\u5929\u662F\u661F\u671F\uFF1A"); JLabel label_4 = new JLabel("\u4E0A\u4E00\u5929\u662F\uFF1A"); JLabel label_5 = new JLabel("\u4E0B\u4E00\u5929\u662F\uFF1A"); year = new JTextField(); year.setColumns(10); month = new JTextField(); month.setColumns(10); day = new JTextField(); day.setColumns(10); textField_3 = new JTextField(); textField_3.setColumns(10); textField_4 = new JTextField(); textField_4.setColumns(10); textField_5 = new JTextField(); textField_5.setColumns(10); GroupLayout gl_contentPane = new GroupLayout(contentPane); gl_contentPane.setHorizontalGroup( gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING) .addGroup(gl_contentPane.createSequentialGroup() .addGap(38) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addPreferredGap(ComponentPlacement.RELATED) .addComponent(year, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(label) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(month, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(label_1) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(day, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.UNRELATED) .addComponent(label_2)) .addGroup(gl_contentPane.createSequentialGroup() .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(label_4) .addComponent(label_3) .addComponent(label_5)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false) .addComponent(textField_3, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE) .addComponent(textField_4, 162, 162, Short.MAX_VALUE) .addComponent(textField_5))))) .addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup() .addGap(79) .addComponent(btnOk, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE) .addGap(40) .addComponent(btnNewButton))) .addGap(197)) ); gl_contentPane.setVerticalGroup( gl_contentPane.createParallelGroup(Alignment.TRAILING) .addGroup(gl_contentPane.createSequentialGroup() .addGap(46) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(year, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(label) .addComponent(month, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(label_1) .addComponent(day, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(label_2)) .addGap(33) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(label_3) .addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.UNRELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(label_4) .addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.UNRELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(label_5) .addComponent(textField_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(18) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(btnOk) .addComponent(btnNewButton)) .addContainerGap()) ); contentPane.setLayout(gl_contentPane); } }
总结
第一次接触JAVA的swing,艰难的编写完了这个界面。并按照要求增加了个别方法。过程是痛苦的,收获是巨大的。
时间: 2024-10-13 00:12:27