java_swing_浏览器

java浏览器源代码1.0版
package com.danqing.www;
import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
/**
 * @author 丹青
 *
 */
public class Browser extends JFrame {
 private static final long serialVersionUID = 1;
    //下拉列表
    private JComboBox _combobox;
    //页面滚动条
    private JScrollPane _scrollpane;
    //存放页面的容器
    private JEditorPane _editorpanel;
    //存放url的字符串
    private String _string;
    //超链接url
    private URL _url;
       Browser(){
        //调试代码
        System.out.println("browser test");
        //组件实例化
        _combobox = new JComboBox();
        _editorpanel = new JEditorPane();
        _string = new String();
        //给frame加上关闭按扭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //初始大小
        this.setBounds(300,300,700,500);
        //定义布局管理器
        this.setLayout(new BorderLayout());
        //默认面板添加下拉列表框
        this.getContentPane().add(_combobox,BorderLayout.NORTH);
        //下拉列表设置为可以编辑
        _combobox.setEditable(true);
        //JEditorPane的滚动条
        _scrollpane = new JScrollPane(_editorpanel);
        //默认面板添加网页容器
        this.getContentPane().add(_scrollpane,BorderLayout.CENTER);
        //设置滚动条的行为方式
        _scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        _scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        //网页容器不可手动编辑
        _editorpanel.setEditable(false);
        //网页容器可见
        _editorpanel.setVisible(true);
        //_editorpanel.setContentType("text/html");
        //下拉列表键盘监听器
           _combobox.getEditor().getEditorComponent().addKeyListener(new KeyAdapter(){
         public void keyPressed(KeyEvent e){
          System.out.println("key enter");
          if(KeyEvent.VK_ENTER==e.getKeyCode()){
           _string = _combobox.getEditor().getItem().toString();
           System.out.println(_string);
           _url = gainURL(_string);
           if(null!=_url){
            System.out.println("url is not null");
            parseURL(_url);
           }
        }
         }
        });
        //this.pack();
        this.setVisible(true);
       }
       //获得链接url
       private URL gainURL(String str){
        URL _temp;
        try{
         _temp = new URL(_string);
        }catch(MalformedURLException e){
         e.printStackTrace();
         _temp = null;
        }
        return _temp;
       }
       //解析url并且显示在JEditorPane中
       private void parseURL(URL url){
        StringBuffer _buffer = new StringBuffer();
        try{
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream()));
        String str;
        str = in.readLine();
        while(null!=str){
         //System.out.println("str is not null"+str);
         _buffer.append(str).append("\n");
         str = in.readLine();
        }
        _editorpanel.setContentType(url.openConnection().getContentType());
        System.out.println(url.openConnection().getContentType().toString());
        if(_buffer==null){
         System.out.println("buffer is null");
        }else{
         System.out.println("buffer is not null");
        }
        String temp = _buffer.toString();
        //setText只能显示<body> </body>之间的内容
        String _content = temp.substring(temp.indexOf("<body"),temp.lastIndexOf("body>")+5);
        _editorpanel.setText(_content);
        System.out.println("parseURL finish");
        }catch(IOException ex){
         ex.printStackTrace();
        }
       }
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Browser browser = new Browser();
 }
}
相关文章
相关标签/搜索