该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;
public class H1 extends JFrame implements ActionListener{
Panel panel=null;
JButton button=null;
static JTextField text_field=null;//这里的静态是为了在静态的search()函数中引用
JTextArea text_area=null;
JLabel label=null;
JScrollPane scroll_pane=null;
public static void main(String[] args){
H1 h=new H1();
}
public H1(){
panel=new Panel();
label=new JLabel("URL:");
text_field=new JTextField(15);//在这里输入网址,前面要写http://
text_field.getText();
button=new JButton("搜索");
button.addActionListener(this);//添加监听
button.setActionCommand("search");//设置监听命令
text_area=new JTextArea();
text_area.setText(ReadURLif.sl);
scroll_pane=new JScrollPane(text_area);
panel.add(label); panel.add(text_field); panel.add(button);
this.add(panel,BorderLayout.NORTH);
this.add(scroll_pane);
this.setTitle("浏览器");
this.setIconImage((new ImageIcon("image/ie.jpg")).getImage());
this.setSize(400,300);
this.setLocation(200,150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){//监听
if(e.getActionCommand().equals("search")){
ReadURLif.search();
text_area.setText(ReadURLif.sl);
}
}
}
class ReadURLif{//获取HTML源代码
static URL url;
static URLConnection context=null;
static InputStream in=null;
static String s=null;
static String sl=null;
public static void search()
{//静态是为了能够在启动监听的时候用类名调用search()函数,用对象名调用会出错
try{
url=new URL(H1.text_field.getText());
context = url.openConnection();
in = context.getInputStream();
BufferedReader str = new BufferedReader(new InputStreamReader(in,"gbk"));
while (str.ready()) {
s = str.readLine();
sl+=s;
}
}
catch(IOException ioe){
System.out.println("IOException");
}
}
}
class Panel extends JPanel{ //这个类在这段程序中没有任何作用
public void paint(Graphics g){//但是可拓展性强,方便添加函数
super.paint(g);
}
}