点名程序 好玩

package exercise2;

import java.awt.BorderLayout;  
import java.awt.GridLayout;  
import java.awt.Image;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.awt.event.WindowAdapter;  
import java.awt.event.WindowEvent;  
import java.io.*;  
import java.text.SimpleDateFormat;  
import java.util.Date;  
import java.util.Scanner;  
  
import javax.imageio.ImageIO;  
import javax.swing.*;  
  
public class RegisterApp2 extends JFrame implements ActionListener {  
  
    static int count2 = 0;// 读取到的行数  
    static JButton but1 = new JButton("到");// 两个按钮  
    static JButton but2 = new JButton("缺席");  
    static JLabel textfield = new JLabel("嗯哼", JLabel.CENTER);// 标签 用于显示名字和号数。  
    static JLabel textarea = new JLabel();// 文本域,用于最后的输入  
    static String namel[][];// 字符组 将花名册读入数组  
    static int count = 0;// 人数  
    static int nAbsent = 0;// 缺席人数  
    static String strAbsent;// 缺席人名  
    static String outname;// 输出文件名,也就是班级名称  
    static int name_partcount;// 部分名字数,默认为点到人数的三分之一  
    static int nameNo[];// 被点到的号码  
    static JFileChooser filechoose;// 文件选择框  
    static JFileChooser imgpathchooser;// 照片文件夹选择框  
    static JButton but_file = new JButton("选择文件");// 按钮打开文件选择框  
    static JButton but_img = new JButton("选择照片文件夹");// 按钮打开文件选择框  
    static File filein;// 文件输入  
    static JTextField text_outname;// 输出文件名  
    static JPanel panel;// 中间面板  
    static Image img;// 图片  
    static Pa p = new Pa();// 实例化面板  
    static String imgpath;// 照片文件夹  
  
    /** 
     * @param args 
     * @throws FileNotFoundException 
     */  
    public static void main(String[] args) {  
  
        /************************************ 图型部分 *********************************************/  
        textfield.setText("请选择文件");  
        RegisterApp2 g = new RegisterApp2();  
        g.setLayout(new BorderLayout());  
        filechoose = new JFileChooser();  
        imgpathchooser = new JFileChooser();  
        imgpathchooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);// 只能选择目录  
  
        panel = new JPanel(new GridLayout(5, 1));// 设置  
        panel.add(but1);  
        panel.add(but2);  
        panel.add(but_file);  
        panel.add(text_outname = new JTextField("classname"));  
        panel.add(but_img);  
  
        g.add("North", textfield);  
        g.add("East", panel);  
        g.add("Center", p);  
        g.add("South", textarea);  
        g.setSize(345, 250);  
        g.setVisible(true);  
        // TODO Auto-generated method stub  
  
        but1.addActionListener(g);  
        but2.addActionListener(g);  
        but_file.addActionListener(g);  
        but_img.addActionListener(g);  
  
        g.addWindowListener(new WindowAdapter() {  
            public void windowClosing(WindowEvent e) {  
                System.exit(0);  
            }  
        });  
        /**************************************************************************************/  
  
    }  
  
    public static String ifexitimg(String x) {  
        String imgname = null;  
        File imgf = new File(imgpath + x + ".jpg");  
        if (imgf.exists()) {  
            imgname = imgf.getName();  
            try {  
                img = ImageIO.read(imgf);  
                p.repaint();  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
  
        } else {  
            imgname = "1";  
        }  
  
        return imgname;  
    }  
  
    public static void getclassname() {  
        outname = text_outname.getText();  
  
    }  
  
    public void onchooserfile() throws FileNotFoundException {  
        filein = filechoose.getSelectedFile();  
        Scanner fin = new Scanner(filein);  
        Scanner fin1 = new Scanner(filein);  
  
        while (fin.hasNext()) {// 确定人数  
            count++;  
            fin.nextLine();  
        }  
        // System.out.println(count);  
        fin.close();  
        namel = new String[count][3];  
        for (int a = 0; a < count; a++) {// 读入数组  
            namel[a][0] = fin1.next();  
            namel[a][1] = fin1.next();  
  
        }  
        fin1.close();  
  
        randomname();// 调用随机生成名字的一个方法。  
        textfield.setText(namel[nameNo[0]][0] + "    " + namel[nameNo[0]][1]);  
        ifexitimg(namel[nameNo[count2]][0]);  
  
    }  
  
    public void actionPerformed(ActionEvent e) {  
  
        if (e.getActionCommand().equals("到") && count2 < name_partcount) {  
  
            // 到时  
            selectis();  
  
        } else if (e.getActionCommand().equals("缺席") && count2 < name_partcount) {  
  
            // 缺席时  
  
            selectpass();  
  
        } else if (count2 == name_partcount  
                && !e.getActionCommand().equals("选择文件")  
                && !e.getActionCommand().equals("选择照片文件夹")) {  
  
            // 点完时  
            over();  
  
        } else if (e.getActionCommand().equals("选择文件")) {  
  
            // 点击选择文件时  
            int result = filechoose.showOpenDialog(filechoose);  
            if (result == 0) {  
                try {  
                    onchooserfile();  
                } catch (FileNotFoundException e1) {  
                    // TODO Auto-generated catch block  
                    e1.printStackTrace();  
                }  
  
            } else {  
                textarea.setText("请输入文件");  
            }  
  
        } else if (e.getActionCommand().equals("选择照片文件夹")) {  
  
            // 选择文件夹  
            int result = imgpathchooser.showOpenDialog(imgpathchooser);  
            if (result == 0) {  
                imgpath = imgpathchooser.getSelectedFile().getPath() + "\\";  
            }  
        }  
  
    }  
  
    public static String redate() {// 返回日期  
        Date now = new Date();  
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHMM");// 可以方便地修改日期格式  
        String strDate = dateFormat.format(now);  
        // System.out.println("当前时间:"+strDate);  
        return strDate;  
  
    }  
  
    public static void randomname() {// 随机生成点名人名  
  
        name_partcount = count / 3;// 三分之一人数,用于确认需要随机生成的人数  
        nameNo = new int[name_partcount];  
        for (int a = 0; a < name_partcount; a++) {// 随即生成人名  
  
            int randomNo = (int) (Math.random() * count);  
  
            for (int i = 0; i < name_partcount; i++) {// 验证是否有重复部分  
  
                if (randomNo == nameNo[i]) {// 有重复部分时重新生成,一直到不重复  
                    i = 0;  
                    randomNo = (int) (Math.random() * count);  
                }  
  
            }  
            nameNo[a] = randomNo;  
  
        }  
    }  
  
    public static void selectis() {// 到时  
        namel[nameNo[count2]][2] = "1";  
        count2++;  
        if (count2 < name_partcount) {  
            textfield.setText(namel[nameNo[count2]][0] + "    "  
                    + namel[nameNo[count2]][1]);  
            ifexitimg(namel[nameNo[count2]][0]);  
  
        }  
        // }  
  
    }  
  
    public static void selectpass() {// 缺课时  
  
        namel[nameNo[count2]][2] = "0";  
        strAbsent = strAbsent + namel[nameNo[count2]][0] + "    "  
                + namel[nameNo[count2]][1] + "    " + "\r\n";  
        count2++;  
        nAbsent++;  
        if (count2 < name_partcount) {  
            textfield.setText(namel[nameNo[count2]][0] + "    "  
                    + namel[nameNo[count2]][1]);  
            ifexitimg(namel[nameNo[count2]][0]);  
        }  
  
    }  
  
    public static void over() {// 点完时  
  
        getclassname();  
        File fileout = new File(outname + redate() + ".txt");  
        try {  
            @SuppressWarnings("resource")  
            PrintWriter fout = new PrintWriter(fileout);  
  
            for (int a = 0; a < name_partcount; a++) {  
                String s = namel[nameNo[a]][0] + "    " + namel[nameNo[a]][1]  
                        + "     " + namel[nameNo[a]][2];  
                fout.println(s);  
            }  
            fout.close();  
            textarea.setText("考勤结束." + "一共点名" + name_partcount + "人" + "一共有"  
                    + nAbsent + "个同学缺课" + "分别是" + strAbsent + "点击右上角关闭");  
        } catch (IOException e1) {  
            // TODO Auto-generated catch block  
            e1.printStackTrace();  
        }  
  
    }  
  
}  



import java.io.File;

public class ReName {  
	  
    public static void main(String[] args) {  
        String path = "f:\\头像文件\\";  
        File f= new File("f:\\","头像文件");  
        System.out.println(f.isDirectory());  
        long a = 201336615101L;  
        String filename[] =f.list();  
        for(int i=0;i<filename.length;i++){  
            File img = new File(path,filename[i]);  
            System.out.println(filename[i]);  
            File newname = new File(path,a+".jpg");  
            img.renameTo(newname);  
            a++;  
            System.out.println(i);  
        }  
        // TODO Auto-generated method stub  
  
    }  
  
}  



import java.awt.Graphics;  

import javax.swing.JPanel;  
  
  
public  class Pa extends JPanel  
{  
    Pa()  
    {  
        this.setSize(200, 200);  
        //this.paint(null);  
    }  
    public void paint(Graphics g){  
          
        g.drawImage(RegisterApp2.img, 0, 0, this);  
        //super.paint(g);  
          
    }  
      
}  



非本人写的代码,有不懂得请不要问我,我也不懂。 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值