一个简单的java窗体小程序(完整代码)

上传人:ss****gk 文档编号:208872864 上传时间:2021-11-08 格式:DOC 页数:9 大小:241KB
返回 下载 相关 举报
一个简单的java窗体小程序(完整代码)_第1页
第1页 / 共9页
一个简单的java窗体小程序(完整代码)_第2页
第2页 / 共9页
一个简单的java窗体小程序(完整代码)_第3页
第3页 / 共9页
一个简单的java窗体小程序(完整代码)_第4页
第4页 / 共9页
一个简单的java窗体小程序(完整代码)_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《一个简单的java窗体小程序(完整代码)》由会员分享,可在线阅读,更多相关《一个简单的java窗体小程序(完整代码)(9页珍藏版)》请在金锄头文库上搜索。

1、首先,这只是一个很简单的程序,所以大神请绕道。心血来潮,用java编写了一个能访14 http链接,并能下载文件的窗体小程序。实测能 够U:确下载17M的压缩包,阁片,文把,html等。但是因为程序效率低下,于是没有继续 测试更人的压缩文件(问题可能在于保存输入流到缓冲区的数组)。程序已打色jar可执行文件,并将代码一齐打到jar中。为方便使用jar,使用word 文捫对象乜装了它,将下面的对象(显示为一个图标)拖到枭面或者其他文件夹,即可得到 该程序:睡java下载小程序.jar也可以右击,激活内容,直接运行。程序运行效果如图:该测试链接来Apc6,下载结果为:文件顺利打开。程序主要使用了

2、Ud访fu http地址,并获取输入流,创建本地文件,输出读取的数据到 本地文件中。其他部分则是窗体。以下是全部代码:M样是word对象,直接拖到桌面即可。显示窗体的ui包:Download Win.javaMainWin.java分割字符串的util包:SplitString.java监听事件的listener包:Download Listener.java其中,ui包:MainWin.javapackage 卜载.ui;public class MainWin public static void main(String args) DownloadWin win = new Downlo

3、adWin(H 卜载);DownloadWin.javapackage 下载.ui;import java.awt.FlowLayout; import java.awt.GridLayout;import javax.swing.Box; import javax.swing JButton; import javax.swingJFrame; import javax.swing.JLabel; import javax.swing.JTextField;import 下载.listener.DownloadLjstener;public class DownloadWin extends

4、 JFramey*/private static final long serialVersionUID = -7226361346020458023L;private JTextField httpLine;private JTextField save Path;private J Button btnDownload;private JLabel lineLabel;private JLabel saveLabel;private static JLabel infoLabel;public DownloadWin(String title) init();setTitle(title)

5、;setSize(400,300);setLayout(new GridLayout(2,1, 0, 0);/为美观考虑,第二行没有使用。 setLocationRelativeTo(null);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);private void init() Box baseBox = Box.create Vertical Box();Box boxVI = Box.createHorizontalBox();Box boxV2 = Box.createHorizontalBox();Bo

6、x boxV3 = Box.createHorizontalBox();Box boxV4 = Box.createHorizontalBox();Box boxV5 = Box.createHorizontalBox();lineLabel = new儿abel(访问地址:n); httpLine = new JTextField(); save La be I = new 儿 abel(保存路径:); savePath = new JTextField(); btnDownload = new JButton(下载); infoLabel = new JLabel();/*监听器 */Do

7、wnloadListener listener = new DownloadUstener(httpLine,savePath,infoLabel);/将 需要改变显示状态的控件传递过去处理btnDownload.addActionListener(listener);boxVl.add(lineLabel);boxVl.add(httpLine);boxV2.add(saveLabel);boxV2.add(savePath);boxV3.add(btnDownload);boxV4.add(infoLabel);baseBox.add(boxVl);Box.createVerticalSt

8、rut(15); baseBox.add(boxV2);Box.createVerticalStrut(15);baseBox.add(boxV3);Box.createVerticalStrut(15); baseBox.add(boxV4);Box.createVerticalStrut(15);baseBox.add(boxV5);add(baseBox);Util 包:SplitStnng.java package 下载.util;public class SpHtString private String splitstr;public SplitString() /TODO S动生

9、成的构造函数存根y* param httpAddress* param accordingString* (return返回7之后最后一个字符串 */public String split(String splitstring,String accordingString) / httpAddress.endsWith(/);/如果a/c/则返回的字符串是c /StringQ str = httpAddress.split(7);/unhappy.substring(2) returns happy/ a.s.k.httpAddress.lastlndexOf(.) returns 3/spl

10、itstr = strstr.length-l;/获取/之后最后一个字符串splitstr = splitString.substring(splitString.lastlndexOf(accordingString)+l);/获取最后个之后的字符串/如果要分解的字符串为a/c/,测试得返回换行 return splitstr;listenerListener.javapackage 下载.listener;import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.Datal

11、nputStream; import java.io.DataOutputStream; import java.io.File;import javaJo.FileOutputStream; import javaJoJOException; import java.io.InputStream; import .URL; import .URLConnection;import javax.swing JLabel; import javax.swing.JTextField;import 卜载.util.SplitString;public class DownloadListener

12、implements ActionUstener private JTextField httpLine, savePath; private JLabel infoLabel; private String httpAddress; private String fileName;private String fileDjr; private File file; private URL url; private URLConnection conn; private InputStream itStream; private DatalnputStream in; private File

13、OutputStream otStream; private DataOutputStream out;public DownloadUstener(JTextField httpLine, JTextField savePath, JLabel infoLabel) this.httpLine = httpLine; this.infoLabel = infoLabel; this.savePath = savePath;(Overridepublic void actionPerformed(ActionEvent e) httpAddress = httpLine.getText();

14、fileDir = save Path .getText(); infoLabel.setText();downFile(httpAddress,fileDir);/* param httpAddress* 下载链接* param fileDir *文件保存目录 */private void downFile(String httpAddress, String fileDir) Splitstring httpSplit = new SplitStringO;fileName = httpSplitsplitfhttpAddress,/;/根据/分解System.out.println(fi

15、leName);if(=fileName| |null=fileName| | =fileDir| | null=fileDir)System.out.printlnf网络地址或保存路径不正确; infoLabel.setText(网络地址或保存路径不正确);elseSystem.out.println( F载中); infoLabel.setText(下载中.); try url = new URL(httpAddress);/设置链接 conn = url.openConnection();/打开链接 file = new File(fileDir,fileName);/获取链接指向的文件的相关数据itStream = conn.getlnputStream();/in = new DatalnputStream(itStream);/数据输入流otStre

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号