java写的一个简易的浏览器_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java写的一个简易的浏览器

java写的一个简易的浏览器

 2011/10/28 8:18:16  chen106106  http://chen106106.iteye.com  我要评论(0)
  • 摘要:java写的一个简易的浏览器importjava.awt.*;importjava.awt.event.*;importjava.io.IOException;importjava.net.*;importjavax.swing.*;importjavax.swing.event.HyperlinkEvent;importjavax.swing.event.HyperlinkListener;publicclassHTTPBrowserDemoextendsJFrame
  • 标签:浏览 Java 一个 浏览器
java写的一个简易的浏览器


import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class HTTPBrowserDemo extends JFrame {
        private JLabel jlAddress,jlInfo;
        private JTextField jtfAddress;
        private JEditorPane jtpShow;
        private JPanel panel;
        private JButton btnGO;
       
        public static void main(String[] args) {
                HTTPBrowserDemo hbd=new HTTPBrowserDemo();
        }
        HTTPBrowserDemo(){
               
                jlAddress=new JLabel("地址");
                jlInfo=new JLabel();
                jtpShow=new JEditorPane();
                panel=new JPanel();
                jtfAddress=new JTextField(20);
                btnGO=new JButton("转到");
               
                add(panel,BorderLayout.NORTH);
                add(jtpShow,BorderLayout.CENTER);
                add(jlInfo,BorderLayout.SOUTH);
               
                panel.add(jlAddress);
                panel.add(jtfAddress);
                panel.add(btnGO);
               
                setVisible(true);
                setSize(350, 280);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               
                btnGO.addActionListener(new ShowHTMLListener());
     jtpShow.setEditable(false);
                jtpShow.addHyperlinkListener(new MyHyperlinkListener());
        }
        class ShowHTMLListener implements ActionListener{
                public void actionPerformed(ActionEvent e){
                        String str=jtfAddress.getText();
                        try {
                                if (!str.startsWith("http://")){
                                        str="http://"+str;
                                }
                                jlInfo.setText("连接中...");
                                URL address=new URL(str);
                                jtpShow.setPage(address);
                                jlInfo.setText("完成");
                        } catch (MalformedURLException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                        } catch (IOException e2) {
                                // TODO: handle exception
                        }
                }
        }
        class MyHyperlinkListener implements HyperlinkListener{
                public void hyperlinkUpdate(HyperlinkEvent e) {
                        if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
                                JEditorPane pane=(JEditorPane)e.getSource();
                                try {
                                        pane.setPage(e.getURL());
                                } catch (IOException e1) {
                                        // TODO Auto-generated catch block
                                        e1.printStackTrace();
                                }
                        }
                }       
        }
}

原文出自:http://www.diybl.com/course/3_program/java/javajs/20100106/186444.html
发表评论
用户名: 匿名