怎么样实现GUI界面和下载代码联系起来 通过按一个按钮就能实现下载 用监听器是吧 如题 我需要的最好是具体的代码 谢谢各位大侠 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是的,如果用Swing的话,就是在监听器的actionPerformed方法中做。 import java.awt.*;import java.awt.event.*;import java.net.*;import java.io.*;public class DownLoad implements ActionListener{ private Frame f; private TextField tf; private Panel p1,p2,p3; private TextArea ta; private Label l1,l2; private Button b; public static void main(String args []) { new DownLoad().init(); } public void init() { f=new Frame("蝴蝶下载程序"); f.setSize(600,400); f.setLocation(100,100); l1=new Label("请输入URL:"); l2=new Label("蝴蝶下载支持的类型为:html,jpg,txt,psd,wav,doc,ppt,bmp,exe,mp3,rm,jsp,zip,gif,class"); tf=new TextField(30); p1=new Panel(); p2=new Panel(); p1.add(l1); p1.add(tf); p2.add(l2); p3=new Panel(); p3.setLayout(new GridLayout(2,1)); p3.add(p1); p3.add(p2); f.add("North",p3); ta=new TextArea(); f.add(ta); b=new Button("下载"); f.add("South",b); b.addActionListener(this); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { f.setVisible(false); f.dispose(); System.exit(0); } }); f.setVisible(true); } public void actionPerformed(ActionEvent e) { String str=tf.getText(); try { URL url=new URL(str); URLConnection urlcon=url.openConnection(); String line=System.getProperty("line.separator"); ta.append("Host:"+url.getHost()); ta.append(line); ta.append("Port:"+url.getDefaultPort()); ta.append(line); ta.append("ContentType:"+urlcon.getContentType()); ta.append(line); ta.append("ContentLength:"+urlcon.getContentLength()); ta.append(line); String type=urlcon.getContentType(); if(type.equals("text/html")) type="html"; else if(type.equals("text/plaign")) type="txt"; else if(type.equals("image/x-photoshop")) type="psd"; else if(type.equals("audio/x-wav")) type="wav"; else if(type.equals("application/msword")) type="doc"; else if(type.equals("application/powerpoint")) type="ppt"; else if(type.equals("image/bmp")) type="bmp"; else if(type.equals("exe")) type="exe"; else if((type.equals("audio/x-mpeg")) | (type.equals("audio/mpeg"))) type="mp3"; else if(type.equals("application/vnd.rn-realmedia")) type="rm"; else if(type.equals("text/html;charset=ISO-8859-1")) type="jsp"; else if(type.equals("application/zip")) type="zip"; else if(type.equals("image/gif")) type="gif"; else if(type.equals("application/java")) type="class"; else if(type.equals("image/jpeg")) type="jpg"; InputStream is=urlcon.getInputStream(); //InputStreamReader ipr=new InputStreamReader(is); // BufferedReader br=new BufferedReader(ipr); FileOutputStream fos=new FileOutputStream("d:/Download/Download."+type); //String strLine; //while((strLine=br.readLine())!=null) //{ // fos.write(strLine.getBytes()); // fos.write(line.getBytes()); // } //br.close(); int data; while((data=is.read())!=-1) { fos.write(data); } fos.close(); is.close(); ta.append("下载完毕!"); ta.append(line); } catch(Exception ex) { ta.append("下载错误"); ta.append(System.getProperty("line.seperator")); } }}在学校的时候写的一个简单程序,楼主可以看看! "a"代表一个匿名String对象,那么new StringBuilder("a")是不是产生了两个对象? 第一个程序写了个有while嵌套的,老是运行不对 一道基础性的java题目,大家看看谁的方法更好 JTable 与 Vector组合,查询未知数据 算法题 如何用URLConnection请求具有SSL证书的网页? 散分:关于jar的exe外衣,穿个马甲,普通用户就不眼生了! 诸位帮帮忙! 菜鸟求救,高分享送!!! Applet高手请进 刚学java,哪位高手能指点我一下谢谢 java,线程同步问题?
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class DownLoad implements ActionListener
{
private Frame f;
private TextField tf;
private Panel p1,p2,p3;
private TextArea ta;
private Label l1,l2;
private Button b;
public static void main(String args [])
{
new DownLoad().init();
}
public void init()
{
f=new Frame("蝴蝶下载程序");
f.setSize(600,400);
f.setLocation(100,100);
l1=new Label("请输入URL:");
l2=new Label("蝴蝶下载支持的类型为:html,jpg,txt,psd,wav,doc,ppt,bmp,exe,mp3,rm,jsp,zip,gif,class");
tf=new TextField(30);
p1=new Panel();
p2=new Panel();
p1.add(l1);
p1.add(tf);
p2.add(l2);
p3=new Panel();
p3.setLayout(new GridLayout(2,1));
p3.add(p1);
p3.add(p2);
f.add("North",p3);
ta=new TextArea();
f.add(ta);
b=new Button("下载");
f.add("South",b);
b.addActionListener(this);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
f.setVisible(false);
f.dispose();
System.exit(0);
}
});
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String str=tf.getText();
try
{
URL url=new URL(str);
URLConnection urlcon=url.openConnection();
String line=System.getProperty("line.separator");
ta.append("Host:"+url.getHost());
ta.append(line);
ta.append("Port:"+url.getDefaultPort());
ta.append(line);
ta.append("ContentType:"+urlcon.getContentType());
ta.append(line);
ta.append("ContentLength:"+urlcon.getContentLength());
ta.append(line);
String type=urlcon.getContentType();
if(type.equals("text/html"))
type="html";
else if(type.equals("text/plaign"))
type="txt";
else if(type.equals("image/x-photoshop"))
type="psd";
else if(type.equals("audio/x-wav"))
type="wav";
else if(type.equals("application/msword"))
type="doc";
else if(type.equals("application/powerpoint"))
type="ppt";
else if(type.equals("image/bmp"))
type="bmp";
else if(type.equals("exe"))
type="exe";
else if((type.equals("audio/x-mpeg")) | (type.equals("audio/mpeg")))
type="mp3";
else if(type.equals("application/vnd.rn-realmedia"))
type="rm";
else if(type.equals("text/html;charset=ISO-8859-1"))
type="jsp";
else if(type.equals("application/zip"))
type="zip";
else if(type.equals("image/gif"))
type="gif";
else if(type.equals("application/java"))
type="class";
else if(type.equals("image/jpeg"))
type="jpg";
InputStream is=urlcon.getInputStream();
//InputStreamReader ipr=new InputStreamReader(is);
// BufferedReader br=new BufferedReader(ipr);
FileOutputStream fos=new FileOutputStream("d:/Download/Download."+type);
//String strLine;
//while((strLine=br.readLine())!=null)
//{
// fos.write(strLine.getBytes());
// fos.write(line.getBytes());
// }
//br.close();
int data;
while((data=is.read())!=-1)
{
fos.write(data);
}
fos.close();
is.close();
ta.append("下载完毕!");
ta.append(line);
}
catch(Exception ex)
{
ta.append("下载错误");
ta.append(System.getProperty("line.seperator"));
}
}}在学校的时候写的一个简单程序,楼主可以看看!