| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 772 人关注过本帖
标题:[分享]用Swing写的一个复制文件的程序
只看楼主 加入收藏
limaoyuan
Rank: 1
等 级:新手上路
帖 子:493
专家分:0
注 册:2006-6-25
收藏
 问题点数:0 回复次数:4 
[分享]用Swing写的一个复制文件的程序

没怎么做过Swing方面的,对纯纯新手可能这程序能起到一点点启示作用吧...

图片附件: 游客没有浏览图片的权限,请 登录注册


--------------------------------------------------------------------------------------
CopyMp3类

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CopyMp3
{
public static void main(String[] args)
{
MainFrame frm = new MainFrame();
frm.setVisible(true);
}
}

class MainFrame extends JFrame
{
public MainFrame()
{
super("COPY");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,160);
MainPanel pan = new MainPanel();
setContentPane(pan);
setLayout(null);
}
}

class MainPanel extends JPanel
{
JTextArea txtInFile;
JTextArea txtOutFile;
JFileChooser open;
String strFilePath;
String strFilePathCopyTo;
public MainPanel()
{
JLabel lblIn = new JLabel("源文件:");
lblIn.setBounds(30, 30, 50, 20);
txtInFile = new JTextArea();
txtInFile.setBounds(80,30,100,20);

JButton btnSelect = new JButton("选择文件");
btnSelect.setBounds(190, 30, 80, 20);

open = new JFileChooser();

btnSelect.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
open.showOpenDialog(null);
strFilePath = open.getSelectedFile().getPath();
((JButton)e.getSource()).setLabel("已选");
txtInFile.setText(strFilePath);

System.out.println(txtInFile.getText());

System.out.println("打开文件");
}
}
);

JLabel lblOut = new JLabel("复制到:");
lblOut.setBounds(30, 60, 50, 20);
txtOutFile = new JTextArea();
txtOutFile.setBounds(80,60,100,20);

JButton btnSelectCopyTo = new JButton("选择文件");
btnSelectCopyTo.setBounds(190, 60, 80, 20);

btnSelectCopyTo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
open.showSaveDialog(null);
strFilePathCopyTo = open.getSelectedFile().getPath();
((JButton)e.getSource()).setLabel("已选");
txtOutFile.setText(strFilePathCopyTo);

System.out.println(txtOutFile.getText());

System.out.println("打开文件");
}
}
);

JButton btnCopy = new JButton("开始复制");
btnCopy.setBounds(60, 90, 90, 20);

btnCopy.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
strFilePath = txtInFile.getText();
strFilePathCopyTo = txtOutFile.getText();

System.out.println(strFilePath);
System.out.println(strFilePathCopyTo);

IOTest.copyFile(strFilePath, strFilePathCopyTo);
}
});



add(lblIn);
add(txtInFile);
add(btnSelect);
add(lblOut);
add(txtOutFile);
add(btnSelectCopyTo);
add(btnCopy);
}
}


IOTest类

import java.io.*;
import javax.swing.*;
public class IOTest
{
public static void copyFile(String in,String out)
{
FileInputStream fin = null;
FileOutputStream fout = null;
try
{
fin = new FileInputStream(new File(in));
fout = new FileOutputStream(new File(out));
byte[] b = new byte[512];

while(true)
{
if(fin.read(b)!=-1)
{
fout.write(b);
}
else
{
break;
}
}
System.out.println("成功将文件:" + in + "复制到:" + out);
JOptionPane.showMessageDialog(null, "文件已成功复制!");
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
fin.close();
fout.close();
}catch(IOException ioe)
{
ioe.printStackTrace();
}
}

}

}

搜索更多相关主题的帖子: Swing 文件 分享 
2007-01-25 13:48
风月_无边
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:60
帖 子:2960
专家分:652
注 册:2007-1-19
收藏
得分:0 
不错!支持一下!

我的网站 http://www.
2007-01-25 14:07
wfd2004
Rank: 1
等 级:新手上路
帖 子:116
专家分:0
注 册:2006-11-7
收藏
得分:0 

我现在正在学swing,希望多多交流!

[此贴子已经被作者于2007-1-25 17:13:34编辑过]


在交流中成长
2007-01-25 17:13
system32
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:58
帖 子:3096
专家分:0
注 册:2006-9-24
收藏
得分:0 
多谢了。

[glow=200,blue,1]世界上妞是无限的,而我们的精力是有限的,用有限去搞无限,死定了。要用有限的精力去泡一个有钱妞,要发达,吃软饭![/glow]
2007-01-25 17:36
无理取闹
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:53
帖 子:4264
专家分:0
注 册:2006-7-26
收藏
得分:0 
以下是引用风月_无边在2007-1-25 14:07:00的发言:
不错!支持一下!

好像老千的口吻


win32汇编
病毒 加密
目前兴趣所在
2007-01-25 17:40
快速回复:[分享]用Swing写的一个复制文件的程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012291 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved