黑马程序员技术交流社区
标题:
看了HTTP协议视频,自己敲的作业
[打印本页]
作者:
tougboy
时间:
2015-5-22 17:10
标题:
看了HTTP协议视频,自己敲的作业
作业敲出来能不能混经验呢?
package studing;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UTFDataFormatException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class cc {
private Frame f;
private TextField tf;
private Button btn;
private TextArea ta;
cc() throws Exception{
f= new Frame("测试窗口");
f.setBounds(100, 100, 600, 400);
f.setLayout(new FlowLayout());
tf=new TextField(60);
btn=new Button("确定");
ta= new TextArea(200,60);
f.add(tf);
f.add(btn);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent()throws Exception {
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
ta.setText("");
String tfstring= tf.getText();
URL url;
try {
url = new URL(tfstring);
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
byte[] data = new byte[1024];
int len=in.read(data);
ta.setText(new String(data,0,len, "GBK"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
}
public static void main(String[] args) throws Exception {
new cc();
}
}
作者:
Always,
时间:
2015-5-22 17:35
我想是没问题的。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2