标签:ict integer for label 这一 -- class ext tla
一、类图
二、代码
Date类:
package examOne; import java.util.Scanner; import snippet.Snippet; public class Date { String ia, ib, ic; int y, m, d; private boolean Read(){ Scanner sc = new Scanner(System.in); System.out.println("请输入年份"); ia = sc.nextLine(); if( "-1".equals(ia) ){ sc.close(); return false; } System.out.println("请输入月份"); ib = sc.nextLine(); System.out.println("请输入日期"); ic = sc.nextLine(); return true; } public void getInputString( String a, String b, String c ){ ia = a ; ib = b ; ic = c ; } public boolean isLeagal(){ if( isInputInteger() && isInRange() ){ return true; } return false; } private boolean isInputInteger(){ try{ y = Integer.valueOf(ia).intValue(); m = Integer.valueOf(ib).intValue(); d = Integer.valueOf(ic).intValue(); return true; } catch( Exception e){ return false; } } private boolean isInRange(){ if( m < 1 || m > 12 ){ //System.out.println("月份超出范围"); return false; } if( d < 1 || d > 31 ){ //System.out.println("日期超出范围"); return false; } if( y < 1912 || y > 2050 ){ //System.out.println("年份超出范围"); return false; } return true; } private int isLeapYear(){ if( ( y % 4 == 0 && y % 100 != 0 ) || ( y % 400 == 0 ) ){ return 1; } return 0; } private boolean EndOfMouth( int flg ){ if( ( m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12 ) && d == 31 ){ return true; } if( m == 2 && d == (28 + flg) ){ return true; } if( ( m == 4 || m == 6 || m == 9 || m == 11 ) && d == 30 ){ return true; } return false; } private int getMouthDay( int x ){ if( ( x == 1 || x == 3 || x == 5 || x == 7 || x == 8 || x == 10 || x == 12 ) ){ return 31 ; } else if(( x == 4 || x == 6 || x == 9 || x == 11 )){ return 30 ; } else if( x == 2 && isLeapYear() == 1 ){ return 29 ; } else{ return 28 ; } } private String NextDate(){ int ny, nm, nd ; ny = y; nm = m; nd = d; int tag = 0; if( EndOfMouth( isLeapYear() ) ){ tag = 1; } if( nm == 12 && tag == 1 ){ ny++; nm = 1; nd = 1; } else if( tag == 1 ){ nm++; nd = 1; } else{ nd++; } String ret = ny + "年" + nm + "月" + nd + "日"; return ret; } private String PreDate(){ int ny, nm, nd ; ny = y; nm = m; nd = d; int tag = 0 ; if( nd == 1 ){ tag = 1 ; } if( tag == 1 && nm == 1 ){ ny-- ; nm = 12 ; nd = 31 ; } else if( tag == 1 ){ nm--; nd = getMouthDay( nm ); } else { --nd ; } String ret = ny + "年" + nm + "月" + nd + "日"; return ret; } private String Week(){ if( m == 1 || m == 2 ){ m += 12 ; y--; } int ans = ( d + 2*m + 3*( m + 1 )/5 + y + y/4 - y/100 + y/400 ) % 7; ans++ ; if( ans == 1 ){ return "一"; } else if( ans == 2 ){ return "二" ; } else if( ans == 3 ){ return "三"; } else if( ans == 4 ){ return "四"; } else if( ans == 5 ){ return "五"; } else if( ans == 6 ){ return "六"; } else{ return "七"; } } public String GetNextDate(){ return NextDate(); } public String GetPreDate(){ return PreDate(); } public String GetWeek(){ return Week() ; } public static void main( String args[]){ Snippet snt = new Snippet(); snt.drawPicture(); } }
图形类:
package snippet; import java.awt.Font; 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; import examOne.Date; public class Snippet { private JFrame frame = new JFrame("日期计算程序"); private JLabel title = new JLabel("请输入需要计算的年月日(1912-2050之间)"); private JLabel year = new JLabel("年:"); private JLabel month = new JLabel("月:"); private JLabel day = new JLabel("日:"); private JLabel thisWeek = new JLabel("这一天是星期"); private JLabel nextDay = new JLabel("下一天是:"); private JLabel prevDay = new JLabel("上一天是:"); private JTextField t_year = new JTextField(); private JTextField t_month = new JTextField(); private JTextField t_day = new JTextField(); private JTextField t_thisWeek = new JTextField(); private JTextField t_nextDay = new JTextField(); private JTextField t_prevDay = new JTextField(); private JButton submit = new JButton("确定"); private JButton cancel = new JButton("清空"); private Date date = new Date(); public void drawPicture() { frame.setLayout(null); submit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == submit) { String y = t_year.getText(); String m = t_month.getText(); String d = t_day.getText(); date.getInputString( y,m,d ); if( date.isLeagal() ){ t_nextDay.setText(date.GetNextDate()); t_prevDay.setText(date.GetPreDate()); t_thisWeek.setText(date.GetWeek()); } else{ t_nextDay.setText("输入非法!"); t_prevDay.setText(""); t_thisWeek.setText(""); } } } }); //使用匿名匿名内部类为cancel按钮添加监听事件 cancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //清空输入的数据 if(e.getSource() == cancel) { t_year.setText(""); t_month.setText(""); t_day.setText(""); t_nextDay.setText(""); t_prevDay.setText(""); t_thisWeek.setText(""); } } }); /*设置标签位置及大小*/ Font ft = new Font("黑体",Font.BOLD,24);//设置显示字体 title.setFont(ft); title.setBounds(62, 62, 500, 25); year.setFont(ft); year.setBounds(80, 124, 50, 25); month.setFont(ft); month.setBounds(230, 124, 50, 25); day.setFont(ft); day.setBounds(380, 124, 50, 25); thisWeek.setFont(ft); thisWeek.setBounds(62, 248, 200, 25); prevDay.setFont(ft); prevDay.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(230, 248, 50, 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_prevDay.setBounds(180, 372, 200, 25); t_prevDay.setFont(ft); t_prevDay.setEnabled(false);//设置为无法输入 /*设置按钮位置及大小*/ submit.setBounds(180, 187, 62, 31); cancel.setBounds(320, 187, 62, 31); //把所有组件添加到frame中 frame.add(title);frame.add(year);frame.add(month);frame.add(day); frame.add(thisWeek);frame.add(nextDay);frame.add(prevDay); frame.add(t_year);frame.add(t_month);frame.add(t_day); frame.add(t_thisWeek);frame.add(t_nextDay);frame.add(t_prevDay); frame.add(submit);frame.add(cancel); /*设置显示画布大小及显示的位置*/ frame.setSize(600, 490); frame.setLocation(300,300); frame.setVisible(true); } }
三、结果
标签:ict integer for label 这一 -- class ext tla
原文地址:http://www.cnblogs.com/Tichocan/p/6791870.html