• 黑马程序员 最简单的浏览器


    URL的全名叫做对统一资源定位符(Uniform  Resource Locator)的抽象,其实就好像现在中的身份证号一样的,可以通过这一串字符找到自己想要去找的地方;

    URL url=new URL(www.baidu.com);创建一个百度的URL

    Internet是用于获取主机信息

    有两个实例方法

    getHostName()用来获取InetAddress对象所含的域名。

    gethostAddress()获取InetAddress对象的IP地址

    //显示网络的文档内容

    import java.awt.*;

    import java.awt.event.*;

    import java.net.*;

    import java.io.*;

    import javax.swing.JEditorPane;

    public class Example11_2

    {

             public static void main(String args[])

             {

                       new Win();

             }

    }

    class Win extends Frame implements ActionListener,Runnable

    {

             Button button;

             URL url;

             TextField text;

             JEditorPane editpane;

             byte b[]=new byte[118];

             Thread thread;

             public Win()

             {

                       text=new TextField(20);

                       editpane=new JEditorPane();

                       editpane.setEditable(false);

                       button=new Button("确定");

                       button.addActionListener(this);

                       thread=new Thread(this);

                       Panel p=new Panel();

                       p.add(new Label("输入网址:"));

                       p.add(text);

                       p.add(button);

                       ScrollPane scroll=new ScrollPane();

                       scroll.add(editpane);

                       add(scroll,BorderLayout.CENTER);

                       add(p,BorderLayout.NORTH);

                       setBounds(160,60,360,300);

                       setVisible(true);

                       validate();

                       addWindowListener(new WindowAdapter()

                                         {

                                                   public void windowClosing(WindowEvent e)

                                                   {

                                                            System.exit(0);

                                                   }

                                         });

             }

             public void actionPerformed(ActionEvent e)

             {

                       if(!(thread.isAlive()))

                       {

                                thread=new Thread(this);

                                try{thread.start();}

                                catch(Exception ee)

                                {text.setText("我正在读取"+url);

                       }

                       }

             }

             public void run()

             {

                       try{

                                editpane.setText(null);

                                url=new URL(text.getText().trim());

                                editpane.setPage(url);

                       }

                       catch(Exception e1)

                       {

                                text.setText(""+e1);

                                return;

                       }

             }

    }

     

    史上最差的模拟网页浏览器代码

    import java.awt.*;

    import java.awt.event.*;

    import java.net.*;

    import java.io.*;

    import javax.swing.event.*;

    import javax.swing.*;

    public class Example11_3

    {

             public static void main(String args[])

             {

                       new LinkWin();

             }

    }

    class LinkWin extends JFrame implements ActionListener,Runnable

    {

             Button button;

             URL url;

             TextField text;

             JEditorPane editpane;

             byte b[]=new byte[118];

             Thread thread;

             public LinkWin()

             {

                       text=new TextField(20);

                       editpane=new JEditorPane();

                       editpane.setEditable(false);

                       button=new Button("确定");

                       button.addActionListener(this);

                       thread=new Thread(this);

                       Panel p=new Panel();

                       p.add(new Label("输入网址"));

                       p.add(text);

                       p.add(button);

                       ScrollPane scroll=new ScrollPane();

                       scroll.add(editpane);

                       add(scroll,BorderLayout.CENTER);

                       add(p,BorderLayout.NORTH);

                       setBounds(60,60,360,300);

                       setVisible(true);

                       validate();

                       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                       editpane.addHyperlinkListener(new HyperlinkListener()

                                         {

                                                   public void hyperlinkUpdate(HyperlinkEvent e)

                                                   {

                                                                     if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED)

                                                                     {

                                                                               try{

                                                                                        editpane.setPage(e.getURL());

                                                                               }catch(IOException e1)

                                                                               {

                                                                                        editpane.setText(""+e1);

                                                                               }

                                                                     }

                                                   }

                                         });

             }

             public void actionPerformed(ActionEvent e)

             {

                       if(!(thread.isAlive()))

                                thread=new Thread(this);

                       try{thread.start();}catch(Exception ee){text.setText("我正在读取"+url);}

             }

             public void run()

             {

                       try{

                                editpane.setText(null);

                                url=new URL(text.getText().trim());

                                editpane.setPage(url);

                       }catch(Exception e1)

                       {

                                text.setText(""+e1);

                                return;

                       }

             }

    }

  • 相关阅读:
    uva 10306
    系统学习Linux的11点建议
    SharePoint 2013 讨论板列表"Connect to Outlook" 不可用解决方案
    Java可视化编程,基于布局管理器的UI设计
    【JavaScript脚本编程技术详解-----(一)】
    一个百度程序员的创业故事
    HDU 4729 An Easy Problem for Elfness (主席树,树上第K大)
    Android SQLite Database Tutorial
    最优化方法在图像处理中的应用【4】
    [置顶] 编辑框Editext光标最后显示
  • 原文地址:https://www.cnblogs.com/tianyake/p/2359534.html
Copyright © 2020-2023  润新知