/** */ /** * 打开,选择文件对话框 */ package filetrans; import javax.swing. * ; import java.io. * ; import java.util. * ; import java.awt. * ; import javax.swing.border.TitledBorder; public class FileChooser ... { private JFileChooser fileChooser; private JFileChooser fileChooser2; private JFrame frame; private int selectButton; private int saveButton; private TitledBorder titledBorder1; private int height=Toolkit.getDefaultToolkit().getScreenSize().height; private int width=Toolkit.getDefaultToolkit().getScreenSize().width; public FileChooser(JFrame frame)throws IOException...{ this.frame=frame; jbInit(); } /** *//** * 初始化 */ public void jbInit() throws IOException...{ this.selectButton=0; this.saveButton=0; titledBorder1=new TitledBorder(""); fileChooser=new JFileChooser(); fileChooser2=new JFileChooser(); } /** *//** * 获得选择的文件路径 */ public String getSelectFile()...{ String filePath=fileChooser.getSelectedFile().getAbsolutePath(); return filePath; } /** *//** * 获得保存的文件 */ public File getSaveSelectFile()...{ File file=fileChooser2.getSelectedFile(); return file; } /** *//** * 获得打开/保存文件对话框上选择的按钮 */ public int getSelectButton()...{ return selectButton; } public int getsaveButton()...{ return saveButton; } /** *//** * 获得当前的路径 */ public File getCurrentPath()...{ fileChooser2.rescanCurrentDirectory(); return fileChooser2.getCurrentDirectory(); } /** *//** * 打开文件对话框及过滤方法 */ public void setSelectFileChooser()throws Exception ...{ fileChooser.setDialogTitle("请选择你要传送的文件"); fileChooser.setBorder(titledBorder1); fileChooser.setPreferredSize(new Dimension(width/2+150,height/4+150)); MyFilefilter filter1=new MyFilefilter(); String st1[]=...{".java"}; filter1.add(st1); filter1.setDescription("Java文件(.java)"); fileChooser.setFileFilter(filter1); MyFilefilter filter2=new MyFilefilter(); String st2[]=...{".jsp"}; filter2.add(st2); filter2.setDescription("jsp文件(.jsp)"); fileChooser.setFileFilter(filter2); MyFilefilter filter3=new MyFilefilter(); String st3[]=...{".html","htm"}; filter3.add(st3); filter3.setDescription("Html文件(.html;.htm)"); fileChooser.setFileFilter(filter3); MyFilefilter filter4=new MyFilefilter(); String st4[]=...{".xml",".syn",".tld",".form",".dtd"}; filter4.add(st4); filter4.setDescription("Xml文件(.xml;.syn;.tld;.form;.dtd)"); fileChooser.setFileFilter(filter4); MyFilefilter filter5=new MyFilefilter(); String st5[]=...{".wma",".wmv",".diz",".asf",".wav",".ra",".rm",".ram",".rmvb",".au"}; filter5.add(st5); filter5.setDescription("音频文件(.wma;.wmv;.diz;.asf;.wav;.ra;.rm;.ram;.rmvb;.au)"); fileChooser.setFileFilter(filter5); MyFilefilter filter6=new MyFilefilter(); String st6[]=...{".mp3",".mp4",".mp2",".mp1",".mpa",".mid",".rmi",".midi"}; filter6.add(st6); filter6.setDescription("音乐文件(.mp3;.mp4;.mp2;.mp1;.mpa;.mid;.rmi;.midi)"); fileChooser.setFileFilter(filter6); MyFilefilter filter7=new MyFilefilter(); String st7[]=...{".jpg",".jpeg",".jpe",".jif",".jfif",".bmp",".png",".gif"}; filter7.add(st7); filter7.setDescription("图像文件(.jpg;.jpeg;.jpe;.jif;.jfif;.bmp;.png;.gif)"); fileChooser.setFileFilter(filter7); MyFilefilter filter8=new MyFilefilter(); String st8[]=...{".txt",".doc",".diz",".bak",".ini",".policy",".mf"}; filter8.add(st8); filter8.setDescription("Plain文件(.txt;.doc;.diz;.bak;.ini;.policy;.mf)"); fileChooser.setFileFilter(filter8); selectButton=fileChooser.showOpenDialog(frame); } /** *//** * 保存文件对话框及过滤 */ public void setSaveFileChooser(String st)throws Exception ...{ fileChooser2.setDialogTitle("保存文件"); fileChooser2.setBorder(titledBorder1); fileChooser2.setPreferredSize(new Dimension(width/2+150,height/4+150)); fileChooser2.setSelectedFile(new File(st)); MyFilefilter filter1=new MyFilefilter(); String st1[]=...{".java"}; filter1.add(st1); filter1.setDescription("Java文件(.java)"); fileChooser2.setFileFilter(filter1); MyFilefilter filter2=new MyFilefilter(); String st2[]=...{".jsp"}; filter2.add(st2); filter2.setDescription("jsp文件(.jsp)"); fileChooser2.setFileFilter(filter2); MyFilefilter filter3=new MyFilefilter(); String st3[]=...{".html","htm"}; filter3.add(st3); filter3.setDescription("Html文件(.html;.htm)"); fileChooser2.setFileFilter(filter3); MyFilefilter filter4=new MyFilefilter(); String st4[]=...{".xml",".syn",".tld",".form",".dtd"}; filter4.add(st4); filter4.setDescription("Xml文件(.xml;.syn;.tld;.form;.dtd)"); fileChooser2.setFileFilter(filter4); MyFilefilter filter5=new MyFilefilter(); String st5[]=...{".wma",".wmv",".diz",".asf",".wav",".ra",".rm",".ram",".rmvb",".au"}; filter5.add(st5); filter5.setDescription("音频文件(.wma;.wmv;.diz;.asf;.wav;.ra;.rm;.ram;.rmvb;.au)"); fileChooser2.setFileFilter(filter5); MyFilefilter filter6=new MyFilefilter(); String st6[]=...{".mp3",".mp4",".mp2",".mp1",".mpa",".mid",".rmi",".midi"}; filter6.add(st6); filter6.setDescription("音乐文件(.mp3;.mp4;.mp2;.mp1;.mpa;.mid;.rmi;.midi)"); fileChooser2.setFileFilter(filter6); MyFilefilter filter7=new MyFilefilter(); String st7[]=...{".jpg",".jpeg",".jpe",".jif",".jfif",".bmp",".png",".gif"}; filter7.add(st7); filter7.setDescription("图像文件(.jpg;.jpeg;.jpe;.jif;.jfif;.bmp;.png;.gif)"); fileChooser2.setFileFilter(filter7); MyFilefilter filter8=new MyFilefilter(); String st8[]=...{".txt",".doc",".diz",".bak",".ini",".policy",".mf"}; filter8.add(st8); filter8.setDescription("Plain文件(.txt;.doc;.diz;.bak;.ini;.policy;.mf)"); fileChooser2.setFileFilter(filter8); saveButton=fileChooser2.showSaveDialog(frame); }}