当前位置: 代码迷 >> J2SE >> 如何把一个网页的Html源码贴在文本框中
  详细解决方案

如何把一个网页的Html源码贴在文本框中

热度:137   发布时间:2016-04-24 02:32:29.0
怎么把一个网页的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();    }}
  相关解决方案