日期:2014-05-20  浏览次数:20794 次

怎么把一个网页的Html源码贴在文本框中
package org.testhtmlDemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.awt.*;
import java.awt.event.ActionListener;

import javax.swing.*;

public class TestHtmlDemo extends JFrame {
private URL url;
private JEditorPane jep ;
private JSplitPane jsp;
private JTextArea jtext;

public TestHtmlDemo() throws Exception
{
super("测试");
this.setBounds(300,240,640,480);
InputStreamReader in = new InputStreamReader(this.url.openStream());
url =new URL("http://www.baidu.com");
jep = new JEditorPane(url);
jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
jsp.setDividerLocation(300);
jtext = new JTextArea();
jsp.add(new JScrollPane(jep));
jsp.add(new JScrollPane(jtext));
BufferedReader bin = new BufferedReader(in); //通过字符缓冲流输入读取文件内容
String aline = bin.readLine();
while(aline!=null)
{
jtext.append(aline+"\r\n");
aline = bin.readLine();
}
bin.close();
in.close();
this.getContentPane().add(jsp);
this.setVisible(true);

}

public static void main(String[] args) throws Exception {

new TestHtmlDemo();

}



}


------解决方案--------------------
下面这个可以运行了,但是有个别的乱码字符,这个是否可以执行也跟具体的URL地址相关
Java code


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.awt.*;
import java.awt.event.ActionListener;

import javax.swing.*;

public class TestHtmlDemo extends JFrame {
    private URL url;
    private JEditorPane jep ;
    private JSplitPane jsp;
    private JTextArea jtext;

    public TestHtmlDemo() throws Exception
    {
        super("测试");
        this.setBounds(300,240,640,480);

        url =new URL("http://www.baidu.com");
        InputStreamReader in = new InputStreamReader(this.url.openStream());
        jep = new JEditorPane(url);
        jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        jsp.setDividerLocation(300);
        jtext = new JTextArea();
        jsp.add(new JScrollPane(jep));
        jsp.add(new JScrollPane(jtext));
        BufferedReader bin = new BufferedReader(in); //通过字符缓冲流输入读取文件内容
        String aline = bin.readLine();
        while(aline!=null)
        {
            jtext.append(aline+"\r\n");
            aline = bin.readLine();
        }
        bin.close();
        in.close();
        this.getContentPane().add(jsp);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) throws Exception {

        new TestHtmlDemo();

    }



}