package com.itheima.homework;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class MainUI extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 5176903471749734286L;
private static JButton searchBtn;
private static JTextField filePathJText;
private static JTextField nameJText;
private static JTextField showNameJText;
private int WIDTH = (int) Toolkit.getDefaultToolkit().getScreenSize()
.getWidth();
private int HEIGTH = (int) Toolkit.getDefaultToolkit().getScreenSize()
.getHeight();
private int FRAMEWIDTH = 600;
private int FRAMEHEIGTH = 400;
public MainUI() {
}
public void intiUI() {
JFrame mainFrame = new JFrame();
mainFrame.setBounds((WIDTH - FRAMEWIDTH) / 2,
(HEIGTH - FRAMEHEIGTH) / 2, FRAMEWIDTH, FRAMEHEIGTH);
mainFrame.setVisible(true);
mainFrame.setTitle("学生作业查询系统");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setResizable(false);
filePathJText = new JTextField("path");
searchBtn = new JButton("查询");
nameJText = new JTextField("输入姓名");
showNameJText = new JTextField("显示姓名");
filePathJText.setBounds(50, 220, 200, 20);
searchBtn.setBounds(50, 250, 100, 20);
nameJText.setBounds(50, 20, 200, 180);
showNameJText.setBounds(300, 20, 200, 200);
mainFrame.add(filePathJText);
mainFrame.add(searchBtn);
mainFrame.add(nameJText);
mainFrame.add(showNameJText);
initBtnListern();
}
private void initBtnListern() {
searchBtn.addActionListener(this);
}
@SuppressWarnings("static-access")
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == searchBtn) {
FilesOperation filesOperation = new FilesOperation();
ArrayList<String> fileNameStrings = new ArrayList<String>();
fileNameStrings = filesOperation.refreshFiles(filePathJText
.getText().trim());
outPutNames(fileNameStrings);
}
}
private void outPutNames(ArrayList<String> fileNameStrings) {
// fileNameStrings为已交作业的人的姓名
// 所有人的姓名
String allName = nameJText.getText().trim();
// 所有人的姓名转化成字符串数组
String[] nameStrings = allName.split(",");
StringBuffer sBuffer = new StringBuffer();
for (int i = 0; i < nameStrings.length; i++) {
if (!fileNameStrings.contains(nameStrings[i])) {
sBuffer.append(nameStrings[i]);
}
}
String dealedNames = sBuffer.toString();
showNameJText.setText(dealedNames);
}
public static void main(String[] args) {
MainUI mainUI = new MainUI();
mainUI.intiUI();
}
}
package com.itheima.homework;
import java.io.File;
import java.util.ArrayList;
public class FilesOperation {
// 获取所给文件夹中的文件名,并获取相应的姓名
public static ArrayList<String> refreshFiles(String path) {
// 1、创建ArrayList数组,用于接收文件名
ArrayList<String> list = new ArrayList<String>();
// 2、 根据所给地址创建文件
File file = new File(path);
// 3、将文件夹中的所有文件获取
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
// 如果获取的是文件夹,
if (files[i].isDirectory()) {
list.add(files[i].getName());
}
}
return getName(list);
}
// 将文件名中的姓名解析出来
public static ArrayList<String> getName(ArrayList<String> list) {
ArrayList<String> listName = new ArrayList<String>();
for (String s : list) {
String[] name = s.split("_");
listName.add(name[1]);
}
return listName;
}
// 测试此类
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list = FilesOperation.refreshFiles("F:" + File.separator + "文件夹");
for (String string : list) {
System.out.println(string);
}
}
}
|
|