Swing小例子

 

 

  1 /**
  2  * 
  3  */
  4 package com.alon.FileMake;
  5 
  6 import java.awt.BorderLayout;
  7 import java.awt.Color;
  8 import java.awt.Container;
  9 import java.awt.Dimension;
 10 import java.awt.FlowLayout;
 11 import java.awt.Font;
 12 import java.awt.GridLayout;
 13 import java.awt.Toolkit;
 14 import java.awt.event.ActionEvent;
 15 import java.awt.event.ActionListener;
 16 import java.io.BufferedInputStream;
 17 import java.io.BufferedReader;
 18 import java.io.File;
 19 import java.io.FileInputStream;
 20 import java.io.FileNotFoundException;
 21 import java.io.FileOutputStream;
 22 import java.io.FileReader;
 23 import java.io.FileWriter;
 24 import java.io.IOException;
 25 import java.io.InputStream;
 26 import java.io.InputStreamReader;
 27 import java.io.OutputStreamWriter;
 28 import java.util.ArrayList;
 29 import java.util.Enumeration;
 30 import java.util.HashMap;
 31 import java.util.Iterator;
 32 import java.util.Map;
 33 import java.util.Map.Entry;
 34 import java.util.Properties;
 35 import java.util.function.Predicate;
 36 import java.util.regex.Matcher;
 37 import java.util.regex.Pattern;
 38 
 39 import javax.management.loading.PrivateClassLoader;
 40 import javax.print.DocFlavor.STRING;
 41 import javax.swing.BoundedRangeModel;
 42 import javax.swing.ButtonGroup;
 43 import javax.swing.JButton;
 44 import javax.swing.JCheckBox;
 45 import javax.swing.JFileChooser;
 46 import javax.swing.JFrame;
 47 import javax.swing.JLabel;
 48 import javax.swing.JOptionPane;
 49 import javax.swing.JPanel;
 50 import javax.swing.JRadioButton;
 51 import javax.swing.JScrollBar;
 52 import javax.swing.JScrollPane;
 53 import javax.swing.JTabbedPane;
 54 import javax.swing.JTextArea;
 55 import javax.swing.JTextField;
 56 import javax.swing.ScrollPaneConstants;
 57 import javax.swing.SwingConstants;
 58 import javax.swing.UIManager;
 59 import javax.swing.plaf.FontUIResource;
 60 import javax.swing.text.AsyncBoxView.ChildLocator;
 61 
 62 /**
 63  * @author Administrator
 64  *
 65  */
 66 /**
 67  * @author Administrator
 68  *
 69  */
 70 /**
 71  * @author Administrator
 72  *
 73  */
 74 /**
 75  * @author Administrator
 76  *
 77  */
 78 public class FileCheackFrame extends JFrame implements ActionListener {
 79     // 日志界面
 80     private JTextArea logArea;
 81     //日志滚动条
 82     private JScrollBar textAreaScroll;
 83     // 配置界面
 84     private JTextArea configArea;
 85     // 根路径
 86     private JTextField rootPathFiled;
 87     // 项目路径
 88     private JTextField childPathFiled;
 89     // 需求路径
 90     private JTextField secondPathFiled;
 91 
 92     // 生成按钮
 93     private JButton okButton;
 94     // 检查按钮
 95     private JButton checkBtn;
 96 
 97     // 存取遍历路径结果
 98     private ArrayList<String> pathList;
 99 
100     // 存取RM配置文件
101     private HashMap<String, String> rmMap;
102 
103     private final String CONFIGPATH = System.getProperty("user.dir") + File.separator + "config.properties";
104 
105     private JCheckBox checkBox1;
106     private JCheckBox checkBox2;
107     private JCheckBox checkBox3;
108     private JCheckBox checkBox4;
109     private JCheckBox checkBox5;
110 
111     private JFileChooser rootPathChooser = new JFileChooser();
112     private JFileChooser childPathChooser = new JFileChooser();
113     private JFileChooser secondPathChooser = new JFileChooser();
114 
115     private JButton rootSelectbtn;
116     private JButton childSelectbtn;
117     private JButton secondSelectbtn;
118 
119     // 迭代一单选框
120     private JRadioButton iterOneRadio;
121     // 迭代二单选框
122     private JRadioButton iterTwoRadio;
123 
124     // 前后代码
125     private JCheckBox newOldCheckBox;
126     // 测试报告
127     private JCheckBox testCheckBox;
128     // 设计文档
129     private JCheckBox designCheckBox;
130     // 反讲纪要
131     private JCheckBox sumaryCheckBox;
132     // 代码评审记录
133     private JCheckBox reviewCheckBox;
134 
135     // 刷新配置文件
136     private JButton refreshBtn;
137     // 保存配置文件
138     private JButton saveBtn;
139 
140     public FileCheackFrame() {
141         initConfig();
142         initUi();
143         initDefaultValue();
144         this.setVisible(true);
145     }
146 
147     private void initConfig() {
148         System.setProperty("sun.jnu.encoding", "utf-8");
149         // 设置界面使用字体
150         FontUIResource fontUIResource = new FontUIResource(new Font("宋体", Font.PLAIN, 12));
151         for (Enumeration keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
152             Object key = keys.nextElement();
153             Object value = UIManager.get(key);
154             if (value instanceof FontUIResource) {
155                 UIManager.put(key, fontUIResource);
156             }
157         }
158 
159         this.setSize(800, 900);
160         this.setTitle("FileCreateTool");
161         this.setResizable(false);
162         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
163 
164         // 设置位于屏幕中间
165         int windowWidth = this.getWidth(); // 获得窗口宽
166         int windowHeight = this.getHeight(); // 获得窗口高
167         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // 获取屏幕的尺寸
168         int screenWidth = screenSize.width; // 获取屏幕的宽
169         int screenHeight = screenSize.height; // 获取屏幕的高
170         this.setLocation(screenWidth / 2 - windowWidth / 2, screenHeight / 2 - windowHeight / 2);// 设置窗口居中显示
171     }
172 
173     public void initUi() {
174         JTabbedPane maintab = new JTabbedPane();
175         Container container = new Container();
176 
177         // 初始化控件
178         JLabel rootPathLable = new JLabel("根目录");
179         rootPathFiled = new JTextField();
180         JLabel childPathLable = new JLabel("子目录");
181         childPathFiled = new JTextField();
182         JLabel secondPathLable = new JLabel("文件夹");
183         secondPathFiled = new JTextField();
184         JLabel chooseIterLable = new JLabel("迭代");
185 
186         // 路径选择区域
187         JPanel northPanle = new JPanel();
188         northPanle.setLayout(null);
189 
190         rootPathLable.setBounds(10, 20, 166, 25);
191         container.add(rootPathLable);
192         rootPathFiled.setBounds(80, 20, 560, 35);
193         container.add(rootPathFiled);
194 
195         childPathLable.setBounds(10, 60, 166, 25);
196         container.add(childPathLable);
197         childPathFiled.setBounds(80, 60, 560, 35);
198         container.add(childPathFiled);
199 
200         secondPathLable.setBounds(10, 100, 166, 25);
201         container.add(secondPathLable);
202         secondPathFiled.setBounds(80, 100, 560, 35);
203         container.add(secondPathFiled);
204 
205         rootSelectbtn = new JButton("...");
206         rootSelectbtn.setBounds(640, 25, 30, 25);
207         rootSelectbtn.addActionListener(this);
208         container.add(rootSelectbtn);
209 
210         iterOneRadio = new JRadioButton("迭代一");
211         iterOneRadio.setBounds(100, 150, 100, 25);
212         iterOneRadio.setBackground(container.getBackground());
213         container.add(iterOneRadio);
214         iterTwoRadio = new JRadioButton("迭代二", true);
215         iterTwoRadio.setBounds(250, 150, 100, 25);
216         iterTwoRadio.setBackground(container.getBackground());
217         container.add(iterTwoRadio);
218 
219         ButtonGroup bGroup = new ButtonGroup();
220         bGroup.add(iterOneRadio);
221         bGroup.add(iterTwoRadio);
222 
223         newOldCheckBox = new JCheckBox("前后文件", true);
224         newOldCheckBox.setBounds(50, 200, 80, 25);
225         newOldCheckBox.setBackground(container.getBackground());
226         container.add(newOldCheckBox);
227 
228         testCheckBox = new JCheckBox("测试报告", true);
229         testCheckBox.setBounds(140, 200, 80, 25);
230         testCheckBox.setBackground(container.getBackground());
231         container.add(testCheckBox);
232 
233         designCheckBox = new JCheckBox("设计文档", true);
234         designCheckBox.setBackground(container.getBackground());
235         designCheckBox.setBounds(230, 200, 80, 25);
236         container.add(designCheckBox);
237 
238         sumaryCheckBox = new JCheckBox("反讲纪要", true);
239         sumaryCheckBox.setBackground(container.getBackground());
240         sumaryCheckBox.setBounds(320, 200, 80, 25);
241         container.add(sumaryCheckBox);
242 
243         reviewCheckBox = new JCheckBox("代码评审", true);
244         reviewCheckBox.setBackground(container.getBackground());
245         reviewCheckBox.setBounds(410, 200, 80, 25);
246         container.add(reviewCheckBox);
247 
248         okButton = new JButton("创建");
249         okButton.setBounds(600, 195, 60, 30);
250         okButton.addActionListener(this);
251         container.add(okButton);
252 
253         checkBtn = new JButton("检查");
254         checkBtn.setBounds(680, 195, 60, 30);
255         checkBtn.addActionListener(this);
256         container.add(checkBtn);
257 
258         logArea = new JTextArea("Program Start...\n");
259         logArea.setForeground(Color.white);
260         logArea.setBackground(Color.BLACK);
261 //        logArea.setBounds(0, 300, 800, 600);
262         //启动自动换行
263         logArea.setLineWrap(true);
264         //换行不断字
265         logArea.setWrapStyleWord(true);
266 //        logArea.setBounds(0, 300, 770, 600);
267         
268         JScrollPane js = new JScrollPane(logArea);
269         js.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
270         JScrollBar jb = js.getVerticalScrollBar();
271         jb.setSize(100,100);
272         js.setBounds(0, 300, 800, 600);
273         container.add(js);
274         
275 //        textAreaScroll = new JScrollBar(JScrollBar.VERTICAL, 10, 10, 0, 100); 
276 //        textAreaScroll.setBounds(770,300,30,600);
277 //        container.add(textAreaScroll);
278 
279         maintab.add("主界面", container);
280 
281         Container configContainer = new Container();
282         refreshBtn = new JButton("刷新");
283         refreshBtn.addActionListener(this);
284         refreshBtn.setBounds(250, 10, 100, 30);
285         configContainer.add(refreshBtn);
286 
287         saveBtn = new JButton("保存");
288         saveBtn.addActionListener(this);
289         saveBtn.setBounds(400, 10, 100, 30);
290         configContainer.add(saveBtn);
291 
292         configArea = new JTextArea();
293         //自动换行
294         configArea.setLineWrap(false);
295         //换行不断字
296         configArea.setWrapStyleWord(true);
297         configArea.setBackground(Color.WHITE);
298         
299         JScrollPane configScroll=new JScrollPane(configArea);
300         configScroll.setBounds(0, 100, 800, 750);
301         
302         configContainer.add(configScroll);
303 
304         maintab.add("配置界面", configContainer);
305 
306         // 配置界面
307 
308         this.setContentPane(maintab);
309     }
310 
311     public void actionPerformed(ActionEvent e) {
312         if (e.getSource() == rootSelectbtn) {
313             rootPathChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
314             int intRetVal = rootPathChooser.showOpenDialog(this);
315             if (intRetVal == JFileChooser.APPROVE_OPTION) {
316                 rootPathFiled.setText(rootPathChooser.getSelectedFile().getPath());
317             }
318         }
319 
320         if (e.getSource() == childSelectbtn) {
321             childPathChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
322             int intRetVal = childPathChooser.showOpenDialog(this);
323             if (intRetVal == JFileChooser.APPROVE_OPTION) {
324                 childPathFiled.setText(childPathChooser.getSelectedFile().getPath());
325             }
326         }
327 
328         if (e.getSource() == secondSelectbtn) {
329             secondPathChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
330             int intRetVal = secondPathChooser.showOpenDialog(this);
331             if (intRetVal == JFileChooser.APPROVE_OPTION) {
332                 secondPathFiled.setText(secondPathChooser.getSelectedFile().getPath());
333             }
334         }
335 
336         if (e.getSource() == okButton) {
337             if (makeDir()) {
338                 wirteConifg("childPath", childPathFiled.getText());
339                 wirteConifg("secondPath", secondPathFiled.getText());
340                 try {
341                     java.awt.Desktop.getDesktop().open(new File(rootPathFiled.getText()));
342                 } catch (IOException e1) {
343                     // TODO Auto-generated catch block
344                 }
345             } else {
346                 JOptionPane.showMessageDialog(null, "创建文件夹失败", "Error", JOptionPane.ERROR_MESSAGE);
347             }
348         }
349 
350         if (e.getSource() == checkBtn) {
351             HashMap<String, String> map = getConfigMap();
352             String seq = File.separator;
353             ArrayList<String> inDirRmList;
354             HashMap<String, String> pathMap = new HashMap<String, String>();
355 
356             String rootPath = rootPathFiled.getText();
357             // 前后代码
358             pathMap.put("newOld", rootPath + seq + map.get("newOld"));
359             // 测试报告
360             pathMap.put("test", rootPath + map.get("test"));
361             // 设计文档
362             pathMap.put("design", rootPath + map.get("design"));
363             // 反讲纪要
364             pathMap.put("sumary", rootPath + map.get("sumary"));
365             // 代码评审记录
366             pathMap.put("review", rootPath + map.get("review"));
367 
368             logArea.setText("开始检查...\n");
369             // pathList=new ArrayList<String>();
370             // getAllFileInDir(newOld);
371             // for(String str:pathList){
372             // logArea.append(str+"\n");
373             // }
374             getRMConfig();
375             for (Entry<String, String> entry : pathMap.entrySet()) {
376                 switch (entry.getKey()) {
377                 case "newOld":
378                     logArea.append("前后文件\n");
379                     break;
380                 case "test":
381                     logArea.append("测试报告\n");
382                     break;
383                 case "design":
384                     logArea.append("设计文档\n");
385                     break;
386                 case "sumary":
387                     logArea.append("反讲纪要\n");
388                     break;
389                 case "review":
390                     logArea.append("代码检视\n");
391                     break;
392                 default:
393                     break;
394                 }
395                 inDirRmList = getRmInPath(entry.getValue());
396                 for (Entry<String, String> rmEntry : rmMap.entrySet()) {
397                     if (!inDirRmList.contains(rmEntry.getKey())) {
398                         logArea.append(rmEntry.getKey() + ": " + rmEntry.getValue() + "\n");
399                     }
400                 }
401             }
402         }
403 
404         if (e.getSource() == refreshBtn) {
405             configArea.setText(getConfigStr());
406             logArea.append("刷新配置文件成功\n");
407         }
408 
409         if (e.getSource() == saveBtn) {
410             wirteConifg();
411             JOptionPane.showMessageDialog(null, "写入配置成功", "Info", JOptionPane.INFORMATION_MESSAGE);
412             logArea.append("写入配置文件成功");
413         }
414 
415         if (e.getSource() == rootSelectbtn) {
416             boolean isOk = wirteConifg("rootPath", rootPathChooser.getSelectedFile().getAbsolutePath());
417             if (!isOk) {
418                 logArea.append("路径保存失败");
419             }
420         }
421         
422         if (e.getSource() == textAreaScroll) {
423 //            this.logArea.set
424         }
425 
426     }
427 
428     private ArrayList<String> getRmInPath(String path) {
429         ArrayList<String> result = new ArrayList<String>();
430         this.pathList = new ArrayList<String>();
431         String regEx = "RM\\d{3}_\\d{3}";
432         getAllFileInDir(path);
433         for (String str : this.pathList) {
434             Matcher matcher = Pattern.compile(regEx).matcher(str);
435             if (matcher.find()) {
436                 result.add(matcher.group());
437             }
438         }
439         return result;
440 
441     }
442 
443     private void initDefaultValue() {
444         // 设置初始路径
445         HashMap<String, String> map = getConfigMap();
446         rootPathFiled.setText(map.get("rootPath"));
447         childPathFiled.setText(map.get("childPath"));
448         secondPathFiled.setText(map.get("secondPath"));
449 
450         // 设置初始配置文件
451         String configStr = getConfigStr();
452         configArea.setText(configStr);
453     }
454 
455     private HashMap<String, String> getConfigMap() {
456         HashMap<String, String> map = new HashMap<String, String>();
457         Properties prop = new Properties();// 属性集合对象
458         FileInputStream in;
459         try {
460             in = new FileInputStream(CONFIGPATH);
461             InputStreamReader reader = new InputStreamReader(in, "utf-8");
462             prop.load(reader);// 将属性文件流装载到Properties对象中
463             in.close();// 关闭流
464         } catch (FileNotFoundException e) {
465             // TODO Auto-generated catch block
466             e.printStackTrace();
467             return map;
468         } catch (IOException e) {
469             // TODO Auto-generated catch block
470             e.printStackTrace();
471             return map;
472         }
473         Iterator<String> it = prop.stringPropertyNames().iterator();
474         while (it.hasNext()) {
475             String key = it.next();
476             map.put(key, prop.getProperty(key));
477         }
478         return map;
479     }
480 
481     private boolean makeDir() {
482 
483         HashMap<String, String> map = getConfigMap();
484         String rootPath = rootPathFiled.getText();
485         String childPath = childPathFiled.getText();
486         String secondPath = secondPathFiled.getText();
487 
488         // 前后代码
489         String newOld = map.get("newOld");
490         // 测试报告
491         String test = map.get("test");
492         // 设计文档
493         String design = map.get("design");
494         // 反讲纪要
495         String sumary = map.get("sumary");
496         // 代码评审记录
497         String review = map.get("review");
498         // 迭代
499         String iter = iterOneRadio.isSelected() ? "迭代一" : "迭代二";
500 
501         String seq = File.separator;
502         ArrayList<String> filePath = new ArrayList<String>();
503         if (newOldCheckBox.isSelected()) {
504             String path = rootPath + seq + newOld + seq + iter + seq + childPath + seq + secondPath;
505             filePath.add(path);
506         }
507         if (testCheckBox.isSelected()) {
508             String path = rootPath + seq + test + seq + iter + seq + childPath + seq + secondPath;
509             filePath.add(path);
510         }
511         if (designCheckBox.isSelected()) {
512             String path = rootPath + seq + design + seq + iter + seq + childPath + seq + secondPath;
513             filePath.add(path);
514         }
515         if (sumaryCheckBox.isSelected()) {
516             String path = rootPath + seq + sumary + seq + iter + seq + childPath + seq + secondPath;
517             filePath.add(path);
518         }
519         if (reviewCheckBox.isSelected()) {
520             String path = rootPath + seq + review + seq + iter + seq + childPath + seq + secondPath;
521             filePath.add(path);
522         }
523         int i = 0;
524         logArea.setText("开始创建文件夹...\n");
525         for (String path : filePath) {
526             File file = new File(path);
527             try {
528                 i++;
529                 file.mkdirs();
530                 logArea.append(i + ". " + path + "\n");
531             } catch (Exception e) {
532                 return false;
533             }
534 
535         }
536         return true;
537     }
538 
539     // 递归遍历获取路径下的所有文件
540     private void getAllFileInDir(String path) {
541         File dir = new File(path);
542         if (!dir.exists()) {
543             return;
544         }
545         File[] files = dir.listFiles();
546         if (files.length == 0) {
547             return;
548         }
549         for (File file : files) {
550             if (file.isDirectory()) {
551                 getAllFileInDir(file.getAbsolutePath());
552             } else {
553                 pathList.add(file.getAbsolutePath());
554             }
555         }
556     }
557 
558     /**
559      * 从配置文件读取内容,返回字符串
560      * 
561      * @return
562      */
563     private String getConfigStr() {
564         BufferedReader br = null;
565         StringBuilder sb = new StringBuilder();
566         File configFile = new File(CONFIGPATH);
567 
568         try {
569             br = new BufferedReader(new InputStreamReader(new FileInputStream(configFile), "utf-8"));
570             String line;
571             while ((line = br.readLine()) != null) {
572                 sb.append(line);
573                 sb.append("\n");
574             }
575             br.close();
576 
577         } catch (Exception e) {
578             // TODO Auto-generated catch block
579             logArea.append(e.toString());
580         }
581 
582         return sb.toString();
583     }
584 
585     private void wirteConifg() {
586         try {
587             FileWriter fileWriter = new FileWriter(CONFIGPATH);
588             String content = configArea.getText();
589             fileWriter.write(content);
590             fileWriter.close();
591         } catch (Exception e) {
592             // TODO Auto-generated catch block
593             logArea.append(e.toString());
594         }
595     }
596 
597     private Boolean wirteConifg(String key, String value) {
598 
599         Properties prop = new Properties();// 属性集合对象
600         FileInputStream fis;
601         try {
602             fis = new FileInputStream(CONFIGPATH);
603             InputStreamReader reader = new InputStreamReader(fis, "utf-8");
604             prop.load(reader);// 将属性文件流装载到Properties对象中
605             fis.close();// 关闭流
606         } catch (FileNotFoundException e) {
607             // TODO Auto-generated catch block
608             e.printStackTrace();
609             return false;
610         } catch (IOException e) {
611             // TODO Auto-generated catch block
612             e.printStackTrace();
613             return false;
614         }
615         prop.setProperty(key, value);
616         // 文件输出流
617         try {
618             FileOutputStream fos = new FileOutputStream(CONFIGPATH);
619             // 将Properties集合保存到流中
620             prop.store(new OutputStreamWriter(fos, "utf-8"), "Copyright (c) Alon Studio");
621             fos.close();// 关闭流
622         } catch (FileNotFoundException e) {
623             // TODO Auto-generated catch block
624             e.printStackTrace();
625             return false;
626         } catch (IOException e) {
627             // TODO Auto-generated catch block
628             e.printStackTrace();
629             return false;
630         }
631         return true;
632     }
633 
634     private void getRMConfig() {
635         rmMap = new HashMap<String, String>();
636         Properties prop = new Properties();
637         try {
638             // 读取属性文件a.properties
639             InputStream in = new BufferedInputStream(new FileInputStream(CONFIGPATH));
640             prop.load(new InputStreamReader(in, "utf-8")); /// 加载属性列表
641             Iterator<String> it = prop.stringPropertyNames().iterator();
642             while (it.hasNext()) {
643                 String key = it.next();
644                 if (key.startsWith("RM")) {
645                     rmMap.put(key, prop.getProperty(key));
646                 }
647             }
648             in.close();
649         } catch (Exception e) {
650             System.out.println(e);
651         }
652     }
653 
654     /**
655      * @param args
656      */
657     public static void main(String[] args) {
658         // TODO Auto-generated method stub
659         new FileCheackFrame();
660     }
661 
662 }

 

posted @ 2018-01-01 02:32  aloneOnWorld  阅读(646)  评论(0编辑  收藏  举报