📄 plgm.java
字号:
} private void jtnumber_actionPerformed(ActionEvent e) { // Add any appropriate code here } //全选按钮 private void jcballsel_actionPerformed(ActionEvent e) { // File tardir; addcheckbox addcb =new addcheckbox(); // tardir=new File(jtpath.getText().toString().trim()); if(jcballsel.isSelected()) { //jpanellist.removeAll(); // addcb.addcheckbox(tardir,true); addcb.selected(true); } else { // jpanellist.removeAll(); // addcb.addcheckbox(tardir,false); addcb.selected(false); } } //反选按钮 //private void jcbresel_actionPerformed(ActionEvent e) //{ //} //删除文件private void delfile(File tardir){ Object[] options={"确定","取消"};int choose=JOptionPane.showOptionDialog(this,"点击确定将删除:"+tardir.getName(),"警告:如果删除将无法恢复",JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE,null, options, options[0]); if(choose!=0) { return; } else { tardir.deleteOnExit(); if(tardir.delete()){ JOptionPane.showMessageDialog(null,"成功删除 "+tardir.getName()); } else { JOptionPane.showMessageDialog(null,"删除不成功"); } } } //增加文件 private void addfile(File tardir){ try{ tardir=new File("","FO.ini"); System.out.println(tardir.toString()); if(tardir.createNewFile()){ JOptionPane.showMessageDialog(null,"增加成功"); } } catch(Exception exp){ JOptionPane.showMessageDialog(null,"增加失败"); } } /** * *只能删除一个记录??????????不能批量删除????????? *如果不判断是否被删除(delete())则可以删除!不过要重新启动才能有效! *解决问题:repaintpanell();的位置摆放! */ //删除按钮事件private void jbdel_actionPerformed(ActionEvent e) { for(int i=0;i<jpanellist.getComponentCount();++i) { JCheckBox tempjcb= (JCheckBox)jpanellist.getComponent(i);//将jpanellist中组件的类型转换成JCheckBoxif (tempjcb.isSelected())//如果被选中{ File tardir=new File(tempjcb.getText().trim()); //得到文件 delfile(tardir); } } repaintpanell();//重新调整} // // TODO: Add any method code to meet your needs in the following area // //jtpath的键盘事件class myKeyAdapter extends KeyAdapter { public void keyPressed(KeyEvent e) { switch(e.getKeyCode()) { case KeyEvent.VK_ENTER: { // JOptionPane.showMessageDialog(null," 你按了Enter键!"); repaintpanell();break;} } } }//endprivate void repaintpanell(){ File tardir; jpanellist.removeAll(); jpanellist.repaint(); tardir=new File(jtpath.getText().toString().trim()); if(tardir.isDirectory()){ addcheckbox add =new addcheckbox(); add.addcheckbox(tardir);} } //搜索文件 public void chosefile(File tardir) { String name; String strname; String str2; int count=0; File[] list=tardir.listFiles(); if(list!=null && list.length>0) { for(int x=0;x<list.length;++x) { if(list[x].isFile()){ name=jtname.getText().trim(); // str1=name.substring(0,name.indexOf(".")); // str2=name.substring(name.indexOf("."));// System.out.println(str1); // System.out.println(str2); //注意indexof的用法!!!!!!!!!!! strname=list[x].getName().toString().trim(); if(strname.indexOf(name)!=-1 || strname.endsWith(name) ){ System.out.println(list[x].getPath()); statusbar.setText("正在搜索中......"); statusbar.repaint(); // statusbar.setText(""); addcheckbox addjcb =new addcheckbox(); addjcb.addcheckbox(list[x]); // System.out.println("aa"); count++; // System.out.println("bb"); tardir=new File(list[x].getPath().trim()); // System.out.println(tardir); // Vector checkboxes =new Vector(); JCheckBox jcb=(JCheckBox)jpanellist.add(new JCheckBox()); checkboxes.addElement(jcb); jcb.setText(list[x].getPath().toString()); //System.out.println("cc"); } } else { chosefile(list[x]); } }//JOptionPane.showMessageDialog(null,"找到了"+count+"个文件!"); } // }//end 搜索文件//写入ini文件 //写入整 private void blockall(Vector a)//定义写入所有块函数 { Vector b=new Vector(); try{ BufferedWriter mybuff= new BufferedWriter(new FileWriter("FO.ini")); System.out.println("size: "+a.size()); for ( int i=0;i<a.size();i++){ try{ b= (Vector)a.get(i); blockwrite(b,mybuff); //,true mybuff.write("[...]"); mybuff.newLine(); }catch(Exception exp){}} mybuff.flush(); mybuff.close();}catch(Exception exp){}}private void blockwrite(Vector a,BufferedWriter mybuff)//定义块写入函数 { int i; String temp; for (i=0;i<=a.size();i++) { try{ temp=a.get(i).toString(); mybuff.write(temp); mybuff.newLine(); } catch(Exception exp1){} }}//写入文件public void write(Vector OldnameToNewname){ Vector vall= new Vector();//存储所有块 Vector vfo = new Vector();//临时数组 String temp,strold; int flag=0; try{ BufferedReader mybuffreader =new BufferedReader(new FileReader("FO.ini")); temp=mybuffreader.readLine();if(temp!=null)//如果读一行不为空 { strold=temp.substring(0,temp.indexOf("..."));//得到旧文件名 File path=new File(strold); if (path.getParent().trim().compareTo(jtpath.getText().trim())==0 ) { vfo.add(temp);//将temp加入动态数组 vfo while(true) { temp=mybuffreader.readLine(); if(temp==null)//为空就退出 break; else { if (temp.compareTo("[...]")!=0)//如果读到字符串[...] { vfo.add(temp);//将temp加入动态数组 vfo } else { vall.add(new Vector(vfo)); //动态增加一个动态数组 vfo.clear();//将临时数组清空 flag++;//标志位加一 } } } } if (flag==10) //如果有十个块 vall.remove(0); //清除第一个块 vall.add(OldnameToNewname); } else { vall.add(OldnameToNewname); } mybuffreader.close();//关闭blockall(vall) ; //调用blockall}catch(IOException exp){}}//end 写入ini文件// 读出ini文件进行还原操作。public void back(){ Vector vfo=new Vector(); Vector vall=new Vector(); String temp; String strold; String strnew; File fold; File fnew; int i; int count; try{ BufferedReader myreader =new BufferedReader(new FileReader("FO.ini")); temp=myreader.readLine(); if(temp==null)//如果文件为空 { JOptionPane.showMessageDialog(null,"不可以再还原了!"); } else { //把文件全部读入vall,注意上面的temp=myreader.readLine()已经读取了一行 vfo.add(temp); while((temp=myreader.readLine())!=null) { if(temp.compareTo("[...]")!=0) { vfo.add(temp); } else { vall.add(new Vector(vfo)); //动态增加一个动态数组 vfo.clear();//清空临时变量 } } vfo=(Vector)vall.lastElement(); count=vfo.size(); for(int x=0;x<count;x++) { temp=vfo.get(x).toString(); strold=temp.substring(0,temp.indexOf("..."));//得到旧文件名 strnew=temp.substring(temp.indexOf("...")+3);//得到新文件名 fold=new File(strold); fnew=new File(strnew); if (fnew.exists()) fnew.renameTo(fold); } vall.remove(vfo); blockall(vall);//将vall写入FO.ini //全部完成刷新界面 repaintpanell(); } } catch(IOException e) {} }//end读出文件进行还原操作。 //============ Inner Classes below =============///************************************************//* Inner Class: AListener *//* *//************************************************/ private class AListener implements ActionListener { public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == jbchose) jbchose_actionPerformed(e); else if (source == jbchange) jbchange_actionPerformed(e); else if (source == jbback) jbback_actionPerformed(e); else if(source==jbdel)jbdel_actionPerformed(e); if (source == jtpath) jtpath_actionPerformed(e); else if (source == jtname) jtname_actionPerformed(e); else if (source == jtrename) jtrename_actionPerformed(e); else if (source == jtreext) jtreext_actionPerformed(e); else if (source == jtnumber) jtnumber_actionPerformed(e) ; else if (source == jcbrename) jcbrename_actionPerformed(e) ; if(source==jcballsel)jcballsel_actionPerformed(e); //if(source==jcbresel)jcbresel_actionPerformed(e); } } /************************************************//* Inner Class: WListener *//* *//************************************************/ private class WListener extends WindowAdapter { public void windowClosing(WindowEvent e) { // // TODO: Add any code you want implement here,when closing this window // //System.exit(0); } }//============================= Testing ================================////= =////= The following main method is just for testing this class you built.=////= After testing,you may simply delete it. =////======================================================================// public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception ex) { System.out.println("Failed loading L&F: "); System.out.println(ex); } new PLGM(); }//MyTreeCellRendererclass MyTreeCellRenderer extends DefaultTreeCellRenderer { public MyTreeCellRenderer() {} public Component getTreeCellRendererComponent(JTree tree,Object value, boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus) {super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,hasFocus); setIcon(fileSystemView.getSystemIcon(((FileNode)value).getFile()));//得到系统图标 return this; }}//定义一个增加JCheckBoxa的类class addcheckbox{ Vector checkboxes =new Vector(); public void addcheckbox(File tardir){ if (tardir.exists()){ File[] file=tardir.listFiles(); if( file!=null && file.length>0) { for(int x=0; x<file.length; x++) { if(!file[x].isDirectory()){ JCheckBox jcb=(JCheckBox)jpanellist.add(new JCheckBox()); checkboxes.addElement(jcb); jcb.setText(file[x].getPath().toString()); } } } } }//判断是否被选择public void selected(boolean selected){ for(int i=0;i<jpanellist.getComponentCount();++i)//得到jpanellist中所有组件的数目 { try{ JCheckBox tempjcb= (JCheckBox)jpanellist.getComponent(i);//将jpanellist中组件的类型转换成JCheckBox tempjcb.setSelected(selected); } catch(Exception e) { JOptionPane.showMessageDialog(null," 找不到组件! "); } } }}//end}//end plgm//定义FileNodeclass FileNode extends DefaultMutableTreeNode {private boolean explored = false, selected = false;public FileNode(File file) { setUserObject(file); }public boolean getAllowsChildren() { return isDirectory(); }//反回是否允许有子节点.是否为目录public boolean isLeaf() { return !isDirectory(); }//如果没有子节点就反回truepublic File getFile() { return (File)getUserObject(); }//反回此节点的对象public void explore() { explore(false); }//展开public boolean isExplored() { return explored; }//是否展开public void setSelected(boolean s) { selected = s; }//选择public boolean isSelected() { return selected; }//是否被选择//定义isDirectory函数文件是否为目录public boolean isDirectory() {File file = (File)getUserObject();return file.isDirectory();}//定义toString函数public String toString() {File file = (File)getUserObject();String filename = file.toString();int index = filename.lastIndexOf("\\");return (index != -1 && index != filename.length()-1) ? filename.substring(index+1) : filename;}//定义explore函数public void explore(boolean force) {if(!isExplored() || force) {File file = getFile();File[] children = file.listFiles();//反回此目录下的所有字子目录或文件for(int i=0; i < children.length; ++i) {if (children[i].isDirectory())//如果是目录执行下面的田加语句add(new FileNode(children[i]));//递归增加子目}explored = true;}}}//end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -