3b. 编写图形化的考勤签到程序。
介绍:采用java swing组件进行编程,其中用到的知识点主要有①、arraylist数组的使用;②txt文本的读取和写入;③字符串的处理;④java的简单GUI编程
源码链接地址:下载
效果图:
源码:
package suanfa.whp;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;
import javax.swing.*;
/**
* 简易签到程序
*
* @author wanghaiping
*
*/
public class check_suanfa implements ActionListener {
ArrayList list = new ArrayList(); // 用于取出txt内容的list数组
ArrayList list1 = new ArrayList(); // 用于存储内容到txt的list1数组
String path; // 选中文件的路劲
String information = ""; // 保存未到名单的信息
String a[]; //用于临时分割字符使用
String b[];// 未到人次;
int leng = -3;
int count = 0;//未到人次计数
JFrame jf;
JTextField jtxt1;
JTextField jtxt2;
JTextField jtxt3;
JButton jbtn1;
JButton jbtn2;
JPanel jp1, jp2, jp3;
JComboBox jcb;
JTextArea jtarea;
public void buju() {
jf = new JFrame("");
String[] jcb_string = { "计科1111", "计科1112", "计科1113", "计科1114" };
jcb = new JComboBox(jcb_string);
jtarea = new JTextArea(3, 4);
jtarea.setBackground(Color.gray);
jtarea.setEditable(false);
jtxt1 = new JTextField(10);
jtxt2 = new JTextField(10);
jtxt3 = new JTextField(10);
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jbtn1 = new JButton("是");
jbtn2 = new JButton("否");
jcb.addActionListener(this);
jbtn1.addActionListener(this);
jbtn2.addActionListener(this);
jp1.add(jcb);
jp2.add(jtxt1);
jp2.add(jtxt2);
jp2.add(jtxt3);
jp2.add(jbtn1);
jp2.add(jbtn2);
jp3.add(jtarea);
jf.add(jp1, BorderLayout.NORTH);
jf.add(jp2, BorderLayout.CENTER);
jf.add(jp3, BorderLayout.SOUTH);
jf.setSize(500, 400);
jf.setLocation(500, 200);
jf.setVisible(true);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
}
// 主函数
public static void main(String[] args) {
// TODO Auto-generated method stub
check_suanfa t = new check_suanfa();
t.buju();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if (e.getSource() == jcb) {
try{
path = "check/" + (String) jcb.getSelectedItem() + ".txt";
check_suanfa cs = new check_suanfa();
list = cs.read(path);
String gets = "";
for (int i = 0; i < list.size(); i++) {
gets = gets + (String) list.get(i) + " ";
}
String a[] = gets.split(" ");
for (int j = 0; j < a.length; j++) {
list1.add(a[j].toString());
}
jtxt1.setText(a[0]);
jtxt2.setText(a[1]);
jtxt3.setText(a[2]);
}catch (Exception e1) {
JOptionPane.showMessageDialog(null, "学生名单有错误!请更正名单后重启软件!");
}
}
if (leng < list1.size() - 3) {
if (e.getSource() == jbtn1) {
leng = leng + 3;
jtxt1.setText((String) list1.get(leng));
jtxt2.setText((String) list1.get(leng + 1));
jtxt3.setText((String) list1.get(leng + 2));
int newlist1 = Integer.parseInt(jtxt3.getText()) + 1;
String newlist2 = String.valueOf(newlist1);
Object asa = newlist2;
list1.set(leng + 2, asa);
}
if (e.getSource() == jbtn2) {
leng = leng + 3;
jtxt1.setText((String) list1.get(leng));
jtxt2.setText((String) list1.get(leng + 1));
jtxt3.setText((String) list1.get(leng + 2));
information = information + jtxt2.getText() + " ";
System.out.print(information);
count++;
jtarea.setText("未到人次:" + count + "\r\n" + "本次未到人名单:"
+ information);
}
} else {
JOptionPane.showMessageDialog(null, "签名完成!");
check_suanfa cs = new check_suanfa();
cs.write(list1, path);
}
}
public void write(ArrayList add_list1, String path) {
try {
FileOutputStream fou = null;
fou = new FileOutputStream(path);// true,设置可追加
String s = "";
for (int i = 0; i < add_list1.size() - 2; i = i + 3) {
s = s + (String) add_list1.get(i) + " "
+ (String) add_list1.get(i + 1) + " "
+ (String) add_list1.get(i + 2) + "\r\n";
}
PrintWriter pw = new PrintWriter(fou);
pw.write(s);
pw.close();
fou.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public ArrayList read(String class_name) {
ArrayList list = new ArrayList();
BufferedReader input = null;
try {
FileReader in = new FileReader(class_name);
input = new BufferedReader(in);
String ss;
try {
while ((ss = input.readLine()) != null) {
String[] s = ss.split("\r\n");
for (int i = 0; i < s.length; i++) {
list.add(s[i].toString()); // 将String s中的内容添加到动态数组中
}
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
in.close();
input.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return list;
}
}