📄 copyfileframe.java~2~
字号:
package copyfile;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.borland.jbcl.layout.*;import java.io.*;import java.util.*;/** * <p>Title: Copy File Demo</p> * <p>Description: This is a Copy File demo</p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: d6-125</p> * @author Liujun * @version 1.0 */public class CopyFileFrame extends JFrame { JPanel contentPane; XYLayout xYLayout1 = new XYLayout(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JTextArea jTextArea1 = new JTextArea(); JLabel jLabel1 = new JLabel(); //定义源文件集和目标文件目录 File [] source ; File target = null; //Construct the frame public CopyFileFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(CopyFileFrame.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); jButton1.setText("选择源文件"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); contentPane.setLayout(xYLayout1); this.setSize(new Dimension(363, 192)); this.setTitle("Copy File Frame Demo"); jButton2.setText("选择目标文件"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton2_actionPerformed(e); } }); jTextArea1.setText("jTextArea1"); jLabel1.setText("jLabel1"); contentPane.add(jTextArea1, new XYConstraints(197, 11, 158, 142)); contentPane.add(jLabel1, new XYConstraints(7, 160, 353, 26)); contentPane.add(jButton2, new XYConstraints(23, 89, 118, -1)); contentPane.add(jButton1, new XYConstraints(23, 31, 118, 28)); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void jButton1_actionPerformed(ActionEvent e) { JFileChooser JFileCh = new JFileChooser();//创建文件对话框 JFileCh.setMultiSelectionEnabled(true);//设置可多选文件属性 int returnVal = JFileCh.showOpenDialog(this);//显示文件对话框 if(returnVal == JFileChooser.APPROVE_OPTION){ source = JFileCh.getSelectedFiles();//得到源文件集 jTextArea1.setText("选择了文件:\n"); for(int i=0;i<source.length;i++){//显示选择了的文件列表 jTextArea1.append(source[i].getPath()+"\n"); }//end for }//end if else jTextArea1.setText("你没有选择文件"); } void jButton2_actionPerformed(ActionEvent e) { JFileChooser JFileCh = new JFileChooser();//创一个文件对话框选择目标文件 String filepath = null; int returnVal = JFileCh.showSaveDialog(this);//显示文件对话框选择目标文件 if(returnVal == JFileChooser.APPROVE_OPTION){//当选择好目标文件后 target = JFileCh.getSelectedFile();//得到目标文件 filepath = target.getPath();//给filepath赋值为目标文件串 if(source !=null){ try{ jLabel1.setText("正在复制文件:"+target.getPath()+"..."); for(int k=0;k<source.length;k++){//根据输入的文件集,创建输出流 FileInputStream FileIn=new FileInputStream(source[k]); FileOutputStream f = new FileOutputStream(target);//定义输出流 DataInputStream in = new DataInputStream(FileIn); int c; while((c = in.read()) != -1)//写入流 out.write(c); in.close();//关闭一个输入流 out.close();//关闭输出流,复制完成 } jLabel1.setText("文件复制成功:"+target.getPath()); }catch(Exception ei){ jLabel1.setText("文件复制失败:"+target.getPath()); } } else jLabel1.setText("请先选择需要复制的文件"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -