java实现点名器exe版,用poi进行数据的导入导出和复用操作

Shams ·
更新时间:2024-11-13
· 957 次阅读

接上文 除了图形化和其中所需要的逻辑后台读取和操作数据
或者用这个地址https://blog.csdn.net/weixin_45127611/article/details/104598798

这个类也就是完成的图片上的组件已经通过上文类方法获取到读取过的我们想要的数据:
在这里插入图片描述

package JieMian; import java.awt.EventQueue; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JLayeredPane; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingConstants; import javax.swing.border.EmptyBorder; import javax.swing.filechooser.FileNameExtensionFilter; import javax.xml.crypto.Data; import org.apache.poi.ss.usermodel.Workbook; import Server.Server; import Server.Utils; import entity.Student; import entity.Tchoose; public class JieMian extends JFrame { private JPanel contentPane; private String ipath; private String opath; private ArrayList start ; private ArrayList result; private ArrayList students = new ArrayList(); public boolean flag = true; private Thread thread; private int removeid;//点中人的索引 private Student student;//这个实体对象对应着最近点名的学生方便老师修改评价 public boolean can = false;//能不能重复点名 private volatile boolean running;//用于判断点名的线程是不是在start状态 private int size;//导入的数据的个数 private String defaultpath;//默认的上此操作的从配置文件中读取的地址 private Utils utils = new Utils(); //上文的工具类 public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { JieMian frame = new JieMian(); frame.setLocationRelativeTo(null);//启动位置居中 frame.setTitle("点名器 -Version2.0");//设置标题 frame.setResizable(false);//设置不可更改大小 frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public JieMian() { //设置JFrame页面 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 670, 400); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); //点名区 JLabel lblNewLabel = new JLabel(); lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);//文字居中 lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 80));//设置文字格式 lblNewLabel.setBorder(BorderFactory.createEtchedBorder());//创建边框 lblNewLabel.setBounds(10, 104, 368, 198); contentPane.add(lblNewLabel); //数据源 JLabel label = new JLabel("当前数据源为空,请导入数据源!"); label.setBounds(10, 71, 368, 23); contentPane.add(label); try{ //项目初始化的时候读取一下配置文件有没有值有就用上次的数据 defaultpath = Utils.init(); }catch(Exception e){ switch(e.getMessage()){ case "配置文件读取异常":JOptionPane.showMessageDialog(null, "配置文件读取异常!");break; case "文件读取异常": JOptionPane.showMessageDialog(null, "文件读取异常!");break; } } if(defaultpath!=null) { File file = new File(defaultpath); if(file.exists()) { label.setText("当前数据源为:"+file.getName()); start = (ArrayList)utils.readStartName(file); size=start.size(); } } if(start!=null) lblNewLabel.setText(start.get(0)); //导入数据按钮 JButton btnNewButton = new JButton("导入数据");//新建按钮 JFileChooser jfc = new JFileChooser();//创建Chooser FileNameExtensionFilter filter = new FileNameExtensionFilter ("Excel文件(*xlsx)", "xlsx","xls");//选择文件的类型 jfc.setFileFilter(filter);//文件过滤器 btnNewButton.addActionListener(new ActionListener() {//按钮单击事件 public void actionPerformed(ActionEvent e) { InputButton(jfc, btnNewButton, label, lblNewLabel); } }); btnNewButton.setBounds(10, 10, 115, 51); contentPane.add(btnNewButton); //已点名区 JTextArea textArea = new JTextArea("姓名:\t时间:\n"); textArea.setEditable(false);//设置禁止编辑 textArea.setLineWrap(true);//设置自动换行 textArea.setBounds(459, 109, 198, 199); JScrollPane scrollPane = new JScrollPane (textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scrollPane.setBounds(404, 104, 240, 198); contentPane.add(scrollPane); //开始按钮 JButton button = new JButton("开始"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { StartButton(lblNewLabel); } }); button.setBounds(58, 312, 105, 45); contentPane.add(button); //停止按钮 JButton button_1 = new JButton("停止"); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { StopButton(textArea, lblNewLabel); } }); button_1.setBounds(211, 312, 105, 45); contentPane.add(button_1); //导出数据按钮 JButton button_2 = new JButton("导出数据"); button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { OutputButton(jfc, btnNewButton, textArea); } }); button_2.setBounds(472, 312, 105, 45); contentPane.add(button_2); //评价 JPanel panel = new JPanel(); panel.setBorder(BorderFactory.createTitledBorder("评价")); panel.setBounds(393, 28, 251, 55); contentPane.add(panel); panel.setLayout(null); JButton btnNewButton_1 = new JButton("优秀"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(student!=null) student.setChoose(Tchoose.优秀); } }); btnNewButton_1.setBounds(17, 22, 68, 23); panel.add(btnNewButton_1); JButton button_3 = new JButton("一般"); button_3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(student!=null) student.setChoose(Tchoose.一般); } }); button_3.setBounds(95, 22, 68, 23); panel.add(button_3); JButton button_4 = new JButton("缺勤"); button_4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(student!=null) student.setChoose(Tchoose.缺勤); } }); button_4.setBounds(173, 22, 68, 23); panel.add(button_4); //是否重复 ButtonGroup bg = new ButtonGroup(); JRadioButton radioButton = new JRadioButton("可重复"); radioButton.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(radioButton.isSelected()) can = true; } }); radioButton.setBounds(161, 10, 121, 23); contentPane.add(radioButton); JRadioButton radioButton_1 = new JRadioButton("不可重复"); radioButton_1.setSelected(true); radioButton_1.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(radioButton_1.isSelected()) can = false; } }); radioButton_1.setBounds(161, 39, 121, 23); contentPane.add(radioButton_1); bg.add(radioButton); bg.add(radioButton_1); } public void InputButton(JFileChooser jfc,JButton btnNewButton,JLabel label,JLabel lblNewLabel){ jfc.setFileSelectionMode(jfc.FILES_ONLY);//仅选择文件 jfc.setAcceptAllFileFilterUsed(false);//去掉显示所有文件 jfc.setMultiSelectionEnabled(false);//禁止多选 jfc.showOpenDialog(btnNewButton);//打开选择页面 if(jfc.getSelectedFile()!=null){ ipath = jfc.getSelectedFile().getPath();//获取文件的路径 label.setText("当前数据源为:"+jfc.getSelectedFile().getName()); try { start = (ArrayList) utils.readStartName(new File(ipath)); size = start.size(); lblNewLabel.setText(start.get(0));//设置点名区显示的名字 } catch (Exception e1) { switch(e1.getMessage()) { case "文件已经被打开": JOptionPane.showMessageDialog(null, "文件被打开,请关闭!");break; case "所给数据中没有姓名列":JOptionPane.showMessageDialog(null, "Excel表数据无效,无法读取到学生信息!");break; case "所给表格为空":JOptionPane.showMessageDialog(null, "表格中没有任何数据!");break; case "文件读取异常":JOptionPane.showMessageDialog(null, "文件读取异常!");break; default:JOptionPane.showMessageDialog(null, "文件读取时发生未知错误!");break; }; } } } public void StartButton(JLabel lblNewLabel){ if(size==0){ JOptionPane.showMessageDialog(null, "数据未导入或者数据中所有人已经点名完毕,请重新导入数据!"); return;//判断为空时,禁止运行软件 } if(flag){//设置仅允许单击一次 thread = new Thread() {//创建线程 public void run() { flag = false;//设置flag,用于暂停线程 running = true;//用于使线程停止 while(running){ removeid = (int) (Math.random()*start.size());//获取随机数 String name = start.get(removeid);//随机获得名字 lblNewLabel.setText(name);//打印名字 try { Thread.sleep(100);//线程休眠 } catch (InterruptedException e) { e.printStackTrace(); } } } }; thread.start();//线程开始 } } public void StopButton(JTextArea textArea,JLabel lblNewLabel){ if(start==null&&size==0) return;//为空点击无效 if(flag) return; Date date = new Date();//获取本地时间 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置时间格式 running = false; flag = true; student = new Student(start.get(removeid));//将显示过的名单加入新表中 if(can==false){//是否重复点名 start.remove(removeid);size--; } students.add(student); textArea.setText(textArea.getText()+lblNewLabel.getText()+"\t"+formatter.format(date)+"\n"); } public void OutputButton(JFileChooser jfc,JButton btnNewButton,JTextArea textArea){ jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);//设置仅选择文件 jfc.setMultiSelectionEnabled(false);//设置禁止多选 int value = jfc.showSaveDialog(btnNewButton);//打开保存按钮 if(jfc.getSelectedFile()!=null&&value==jfc.APPROVE_OPTION){ opath = jfc.getSelectedFile().getPath();//获取文件的路径 try { File ofile = new File(opath); if(ofile.length()==0) { JOptionPane.showMessageDialog(null, "所给Excel表格无效,请新建或重新选择!"); return; } result = (ArrayList) utils.readResultName(ofile); Server server = new Server(ofile); server.adds(result,students); JOptionPane.showMessageDialog(null, "数据已成功导出!"); textArea.setText("姓名:\t时间:\n"); result.clear(); students.clear(); } catch (Exception e1) { switch(e1.getMessage()){ case "文件读取异常":JOptionPane.showMessageDialog(null, "文件读取异常!");break; case "文件关闭异常":JOptionPane.showMessageDialog(null, "关闭异常!");break; case "不支持对于xls文件的重写":JOptionPane.showMessageDialog(null, "不支持对于xls文件的重写,请新建一个Excel文件(任意格式)!");break; default : JOptionPane.showMessageDialog(null, "文件已经被打开,请关闭后导入!");break; } } } } }

结合着上文 点击这里和这个类就可以将整个 项目跑起来了;
整个项目所用到的所有jar包,下载地址:https://download.csdn.net/download/weixin_45127611/12203875或者 点击这里


作者:西交大吴彦祖



JAVA 数据 poi

需要 登录 后方可回复, 如果你还没有账号请 注册新账号