java_swing_브 라 우 저

4698 단어
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("
"); 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(); } }

좋은 웹페이지 즐겨찾기