之前写的程序,大都是命令行来执行,对于研发测试人员来说非常方便,但是对于其他普通客户应用可能就不是很易用。
java可以使用jframe来实现界面的框架布局。
程序一般分为3个部分
一是界面布局
二是操作监听
三是程序逻辑
先看代码把。
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.util.LinkedHashMap; import java.util.Map;
public class SearchPic implements ActionListener {
//这部分是注册一些界面上的控件,需要注册哪些控件,取决于产品设计效果 JFrame frame = new JFrame("查找照片并复制");// 框架布局 JTabbedPane tabPane = new JTabbedPane();// 选项卡布局 Container con = new Container();// JLabel label1 = new JLabel("结果文件存放目录"); JLabel label2 = new JLabel("选择照片列表文件"); JLabel label3 = new JLabel("选择源数据的目录"); JTextField text1 = new JTextField();// TextField 目录的路径 JTextField text2 = new JTextField();// 文件的路径 JTextField text3 = new JTextField();// 文件的路径 JButton button1 = new JButton("...");// 选择 JButton button2 = new JButton("...");// 选择 JButton button3 = new JButton("...");// 选择 JButton button4 = new JButton("开始执行");// JFileChooser jfc = new JFileChooser();// 文件选择器
//开始第一部分界面布局
SearchPic() { jfc.setCurrentDirectory(new File("c://"));// 文件选择器的初始目录定为c盘 double lx = Toolkit.getDefaultToolkit().getScreenSize().getWidth(); double ly = Toolkit.getDefaultToolkit().getScreenSize().getHeight(); frame.setLocation(new Point((int) (lx / 2) - 150, (int) (ly / 2) - 150));// 设定窗口出现位置 frame.setSize(420, 250);// 设定窗口大小 frame.setContentPane(tabPane);// 设置布局 label1.setBounds(10, 10, 120, 30); text1.setBounds(120, 10, 200, 30); button1.setBounds(330, 10, 50, 30); label2.setBounds(10, 50, 120, 30); text2.setBounds(120, 50, 200, 30); button2.setBounds(330, 50, 50, 30); label3.setBounds(10, 90, 120, 30); text3.setBounds(120, 90, 200, 30); button3.setBounds(330, 90, 50, 30); button4.setBounds(140, 130, 100, 30); button1.addActionListener(this); // 添加事件处理 button2.addActionListener(this); // 添加事件处理 button3.addActionListener(this); // 添加事件处理 button4.addActionListener(this); // 添加事件处理 con.add(label1); con.add(text1); con.add(button1); con.add(label2); con.add(text2); con.add(button2); con.add(label3); con.add(text3); con.add(button3); con.add(button4); frame.setVisible(true);// 窗口可见 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 使能关闭窗口,结束程序 tabPane.add("照片", con);// 添加布局1 }
//第2部分设置监听,这里有个问题,就是把监听启动的程序逻辑写在一起了,应该分开写会更清晰些。
public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource().equals(button1)) {// 判断触发方法的按钮是哪个 jfc.setFileSelectionMode(1);// 设定只能选择到文件夹 int state = jfc.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句 if (state == 1) { return; } else { File f = jfc.getSelectedFile();// f为选择到的目录 text1.setText(f.getAbsolutePath()); } } // 绑定到选择文件,先择文件事件 if (e.getSource().equals(button2)) { jfc.setFileSelectionMode(0);// 设定只能选择到文件 int state = jfc.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句 if (state == 1) { return;// 撤销则返回 } else { File f = jfc.getSelectedFile();// f为选择到的文件 text2.setText(f.getAbsolutePath()); } } if (e.getSource().equals(button3)) {// 判断触发方法的按钮是哪个 jfc.setFileSelectionMode(1);// 设定只能选择到文件夹 int state = jfc.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句 if (state == 1) { return; } else { File f = jfc.getSelectedFile();// f为选择到的目录 text3.setText(f.getAbsolutePath()); } } if (e.getSource().equals(button4)) { String desDir = text1.getText(); String srcDir = text3.getText(); String targetFilePath = text2.getText(); File f = new File(targetFilePath); File f2 = new File(srcDir); File f3 = new File(desDir); if (!f.exists()){ //报错,停止程序 JOptionPane.showMessageDialog(null, "查询文件列表不存在!", "提示", 2); //System.exit(0); } if (!f2.exists()){ //报错,停止程序 JOptionPane.showMessageDialog(null, "源文件目录不存在!", "提示", 2); //System.exit(0); } if (desDir.equals("")){ JOptionPane.showMessageDialog(null, "未指定结果文件目录!", "提示", 2); //System.exit(0); //报错,停止程序 } if (!f3.exists()){ f3.mkdirs(); } Map<String,String> fileMap = new LinkedHashMap<String,String>(); try { OutputStreamWriter err = new OutputStreamWriter(new FileOutputStream(desDir+File.separator+"err.log")); BufferedReader reader = new BufferedReader(new FileReader(f)); String tempString = null; while ((tempString = reader.readLine()) != null) { if(fileMap.get(tempString)==null){ fileMap.put(tempString,""); }else{ System.out.println("文件名称列表有重复名称:"+tempString); err.write("文件名称列表有重复名称:"+tempString); err.write("\r\n"); } } err.close(); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(desDir+File.separator+"common.log")); writer.write("文件名,文件来源路径"); writer.write("\r\n"); for(Map.Entry<String,String> entry: fileMap.entrySet()){ String filename = entry.getKey(); String filePath = findFilePath(srcDir,filename); String value = ""; if (filePath.equals("error1 : 文件目录不存在!")){ //报错停止程序,在这里不会有,因为程序开头检查过输入了 }else{ if (filePath.equals("")){ filePath = "文件不存在!"; } writer.write(filename+","+filePath); writer.write("\r\n"); } fileMap.put(filename,filePath); copyFile(filePath,desDir); } writer.close(); reader.close(); } catch (Exception e1) { e1.printStackTrace(); } } }
public static void main(String[] args) { new SearchPic(); }//copy文件的方法 private String copyFile(String srcPath ,String desDirPath){ if(!srcPath.substring(srcPath.length()-1,srcPath.length()).equals("\\") && !srcPath.substring(srcPath.length()-1,srcPath.length()).equals("/")){ srcPath+="\\"; } File srcFile = new File(srcPath); File desDir = new File(desDirPath); if(!desDirPath.substring(desDirPath.length()-1,desDirPath.length()).equals("\\") && !desDirPath.substring(desDirPath.length()-1,desDirPath.length()).equals("/")){ desDirPath+="\\"; } String fileName = srcFile.getName(); File desFile = new File(desDirPath+fileName); if (!srcFile.exists()){ return "需要复制的源文件不存在!"; } if (!desDir.exists()){ desDir.mkdirs(); } try { BufferedReader reader = new BufferedReader(new FileReader(srcFile)); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(desFile)); String strLine = ""; while((strLine=reader.readLine())!=null){ writer.write(strLine); writer.write("\r\n"); } reader.close(); writer.close(); return "ok"; } catch (Exception e) { e.printStackTrace(); return e.toString(); } }//查找路径的方法 private String findFilePath(String srcDir,String targetFileName){ File des = new File(srcDir); String targetPath = ""; if (!des.exists()){ return "error1 : 文件目录不存在!"; } File[] files = des.listFiles(); for(File file : files){ if (!file.isDirectory()){ String filename = file.getName(); if (filename.equals(targetFileName)){ targetPath = file.getAbsolutePath(); return targetPath; } }else{ String filePath = file.getAbsolutePath(); targetPath = findFilePath(filePath,targetFileName); if (!targetPath.equals("")){ return targetPath; } } } return targetPath; } }