- //判断是否为闰年。
- import javax.swing.JOptionPane;
- public class LeapYear
- {
- public static void main(String args[])
- {
- int year = Integer.parseInt(JOptionPane.showInputDialog(null,"请输入一个年份"));
- if((year % 4 == 0 && year % 100 != 0) || (year % 4 == 0 && year % 400 == 0))
- {
- JOptionPane.showMessageDialog(null,year + " 年是闰年!");
- } else
- { JOptionPane.showMessageDialog(null,year + " 年不是闰年!");
- }
- }
- }
- //图形界面学生签到程序。
- import java.awt.Button;
- import java.awt.FlowLayout;
- import java.awt.Frame;
- import java.awt.Label;
- import java.awt.TextArea;
- import java.awt.TextField;
- import java.awt.event.*;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Scanner;
- import javax.swing.JFrame;
- public class RegisterDemo extends Frame implements ActionListener
- {
- static String[][] stringArray ;
- static JFrame jf = new JFrame("GUI example");//JFrame容器
- static Label lb = new Label("学生姓名");
- static TextField tf = new TextField(10);
- static Button b1 = new Button("到");
- static Button b2 = new Button("缺席");
- static Label b = new Label("缺到名单");
- static TextArea ta = new TextArea(15,30);
- static int count2 = 0;//记录学生人数
- static int count = 0;//记录学生人数
- static String strAbsent = " ";//记录缺席的学生姓名
- public static void main(String[] args) throws Exception {
- File fileinput=new File("E:\\wl121.txt"); //输入文件
- Scanner sc = new Scanner(fileinput); //构造新的Scanner,指定fileinput为其生成值
- Scanner st = new Scanner(fileinput); //同上
- while (sc.hasNext()) //确定人数
- {
- count++;
- sc.nextLine();
- }
- stringArray = new String[count + 1][2];//给数组stringArray加多一位,后面用得上
- for (int a = 0; a < count ; a++)//读入数组
- {
- stringArray[a][0] = st .nextLine();
- }
- sc.close();
- st.close();
- stringArray[count][0] = "点名结束";
- tf.setText(stringArray[count2][0]);//在文本框中加入数组的第一行内容
- RegisterDemo tu = new RegisterDemo();//构造方法
- jf.setLayout(new FlowLayout(FlowLayout.LEFT,20,40));//布局管理器
- jf.add(lb);
- jf.add(tf);
- jf.add(b1);
- jf.add(b2);
- jf.add(b);
- jf.add(ta);
- jf.setSize(350,450);//设定大小
- jf.setVisible(true);//容器可见
- b1.addActionListener(tu);//监听按钮
- b2.addActionListener(tu);//监听按钮
- }
- public void actionPerformed(ActionEvent e)//监听事件
- {
- if(e.getSource()==b1 && count2 < count)//点击“到”按钮并且总点击数少于人数时,往下执行
- {
- count2++;
- tf.setText(stringArray[count2][0]);//在文本框中加入数组的内容
- }
- else if(e.getSource()==b2 && count2 < count)//点击“缺席”按钮并且总点击数少于人数时,往下执行
- {
- strAbsent = strAbsent + stringArray[count2][0] + "\n";//把缺席的名字记录到strAbsent中
- count2++;//
- tf.setText(stringArray[count2][0]);//在文本框中加入数组的内容
- }
- if(count2 == count)//当读完了所有名字后,把缺席名单显示在文本域中
- {
- ta.setText(strAbsent);
- ta.getSelectedText();
- }
- Date now = new Date();//获得当前日期,以便在每天考勤后自动生成不同名字的考勤结果文件
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHMM");//方便修改日期格式
- String strDate = dateFormat.format(now);//把时间类型改为字符串
- File fileoutput = new File("E:\\wl121.txt"+"\\考勤结果" + strDate);
- PrintWriter output = null;//准备输出考勤结果
- try
- {
- output = new PrintWriter(fileoutput);//构造新的PrintWriter,指定fileoutput为其生成值
- }
- catch (FileNotFoundException e1)
- {
- }
- if(count2 == count)
- {
- output.print("缺课名单: ");
- output.print(strAbsent);//输出考勤结果
- output.close();
- }
- }
- }