哥们,给你改了改就好了~~
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class DownLoad extends Thread
{
static JFrame jf;
static JTextField tf=new JTextField(30);
static JTextArea ta=new JTextArea();
public static void main(String [] args)
{
jf=new JFrame("蜗牛下载");//新建一个窗体
jf.setSize(600,400);
jf.setLocation(100,100);
JPanel p=new JPanel();//新建一个容器他extends JComponent
JLabel l=new JLabel("请输入地址:");
p.add(l);
p.add(tf);
jf.getContentPane().add(p,"North");
jf.getContentPane().add(ta,"Center");
JButton btn=new JButton("下载");
jf.getContentPane().add(btn,"South");
jf.setVisible(true);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{DownLoad dl=new DownLoad();
dl.start();}catch(Exception ee){
ee.printStackTrace();
}
}
});
}
public void run()
{
String str=tf.getText();
ta.append("开始下载:");
try{
URL url=new URL(str);
URLConnection urlConn=url.openConnection();
String line=System.getProperty("line.separator");
ta.append(line);
ta.append("主机名:"+url.getHost());
ta.append(line);
ta.append("端口:"+url.getDefaultPort());
ta.append(line);
ta.append("文件类型:"+urlConn.getContentType());
ta.append(line);
ta.append("文件长度:"+urlConn.getContentLength());
ta.append(line);
InputStream is=urlConn.getInputStream();
FileOutputStream fos=new FileOutputStream("1.html");
int data;
while((data=is.read())!=-1)
{
fos.write(data);
}
ta.append("下载完毕!");
ta.append(line);
is.close();
fos.close();
}
catch(Exception ex){
ex.printStackTrace();
}
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
主要是你再次调用的时候再运行同一个线程是不对的,本来这个线程已经运行了~~
答案: 错。"interface Rollable extends Playable, Bounceable"没有问题。interface可继承多个interfaces,所以这里没错。问题出在interface Rollable里的"Ball ball = new Ball("PingPang");"。任何在interface里声明的interface variable (接口变量,也可称成员变量),默认为public static final。也就是说"Ball ball = new Ball("PingPang");"实际上是"public static final Ball ball = new Ball("PingPang");"。在Ball类的Play()方法中,"ball = new Ball("Football");"改变了ball的reference,而这里的ball来自Rollable interface,Rollable interface里的ball是public static final的,final的object是不能被改变reference的。因此编译器将在"ball = new Ball("Football");"这里显示有错。
什么东西啊?