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(); } }

解决方案 »

  1.   

    下面这个可以运行了,但是有个别的乱码字符,这个是否可以执行也跟具体的URL地址相关
    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();    }}
      

  2.   

    将上面内容当中的
    InputStreamReader in = new InputStreamReader(this.url.openStream());
    改为
    InputStreamReader in = new InputStreamReader(this.url.openStream(),"gbk");
    就可以正常显示上面百度的源码,中文不会乱码了
      

  3.   

    主要的逻辑改懂就是把你原来的代码
    InputStreamReader in = new InputStreamReader(this.url.openStream());
    url =new URL("http://www.baidu.com");
    这两行调换了位置,如果不调换url初始为null会报空指针错误
      

  4.   

    package org.htmlDemo;import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;import java.net.*;
    import java.io.*;
    import java.util.Date;
    import java.text.SimpleDateFormat;
    public class HtmlJFrame extends JFrame implements ActionListener,javax.swing.event.ChangeListener{
    private URL urls[];                            //URL对象数组        
    private JComboBox combobox_url;                //组合框,输入或选择URL地址
    private JTextField text_attribute;             //文本行,显示文件属性
    private JTabbedPane tab;                       //选项卡窗格,每页显示一个文件

    public HtmlJFrame()
    {
    super("查看源文档");
    this.setBounds(300,260,640,480);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    String urls[] = {"http://java.sun.com",
              "http://www.baidu.com",
              "file://localhost/F:/JAVA/html_zh_CN/html/zh_CN/api/index.html"
    };
    combobox_url = new JComboBox(urls);        //组合框,显示URL地址
    combobox_url.setEditable(true);           //组合框可以编辑
    this.getContentPane().add(combobox_url,"North");
    combobox_url.addActionListener(this);     //组合框注册单击事件
    tab = new JTabbedPane();                  //选项卡窗格
    tab.addChangeListener(this);              //注册选择事件监听器
    this.getContentPane().add(tab);

    text_attribute = new JTextField();       //文本行,显示文件属性
    this.getContentPane().add(text_attribute,"South");   
    this.setVisible(true);
    this.urls = new URL[20];                //创建URL对象数组
    actionPerformed(null);                  //调用事件处理方法,初始化
    }

    public void actionPerformed(ActionEvent e)
    {
    String urlname = (String)combobox_url.getSelectedItem();
    int i = tab.getTabCount();
    try


    InputStreamReader in = new InputStreamReader(this.urls[i].openStream());
    //由字节输入流创建字符输入流,若url[i]指定文件不存在,跑出异常,不添加tab页
    this.urls[i] = new URL(urlname); //创建一个URL对象
    String filename = (String)this.urls[i].toString();   //URL路径名
    for(int j= i-1;j<0;j--)                       // 在已有页面中查找,不重复打开文件
    if(tab.getTitleAt(j).equals(filename))    //getTitleAt(j)返回tab第j页标题
    {
    tab.setSelectedIndex(j);             //若找到,选中
    return;
    }
    JEditorPane preview = new JEditorPane(this.urls[i]);  //创建指定URL的编辑器窗格
    JTextArea text_content = new JTextArea();        //文本区,显示文件内容
    JSplitPane splitter_v = new JSplitPane(JSplitPane.VERTICAL_SPLIT);  //垂直分隔窗格
    splitter_v.setDividerLocation(200);         //设置水平分隔条的位置
    splitter_v.add(new JScrollPane(preview));
    splitter_v.add(new JScrollPane(text_content));
    tab.addTab(filename, splitter_v);       //tab添加页,页中添加分割窗格
    tab.setSelectedIndex(tab.getTabCount()-1);  //tab指定新页为选中状态

    BufferedReader bin = new BufferedReader(in);  //通过字符缓冲流输入读取文件内容
    String aline = bin.readLine();
    while(aline!=null)
    {
    text_content.append(aline+"\r\n");
    aline = bin.readLine();
    }
    bin.close();
    in.close();
    }
    catch(MalformedURLException murle)
    {
    JOptionPane.showMessageDialog(this, "字符串指定未知协议错位");
    }
    catch(FileNotFoundException fe)
    {
    JOptionPane.showMessageDialog(this, "\""+urls[i].getFile()+"文件不成功");
    }
    catch(IOException ioex)
    {
    JOptionPane.showMessageDialog(this, "IO错,读取"+urls[i].getFile()+"\"文件不存在");
    }

    }
    public void stateChanged(ChangeEvent e)  //当单击选项卡窗格的页标题时触发执行
    {
    String str = "文件:";                  //通过文件对象获得文件属性
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM--dd hh:mm");
    int i = tab.getSelectedIndex();
    if(urls[i].getProtocol().equals("file"))   //当URL对象使用FILE协议时
    {
    File afile = new File(urls[i].getFile());  //获得文件对象
    str +=afile.getAbsolutePath()+"\t";     //获得文件路径名
    str +=afile.length()+"B\t";         //获得文件长度
    str += sdf.format(new Date(afile.lastModified()));//获得文件修改时间
    try
    {
    InetAddress local = InetAddress.getLocalHost();
    str +="本机名:"+local.getHostName()+", 本机IP地址" +local.getHostAddress();
    }
    catch(UnknownHostException ioe)
    {
    JOptionPane.showMessageDialog(this, "找不到指定主机的IP地址");
    }

    }
    if(urls[i].getProtocol().equals("http"))
    {
    try
    {
    URLConnection urlconn = urls[i].openConnection();
    str += urls[i].toString()+"\t";          //获得URL路径名
    str += urlconn.getContentLength()+"B\t"; //获得文件长度
    str += urlconn.getContentType()+"\t";    //获得文件类型
    str += sdf.format(new Date(urlconn.getLastModified()));
    str +="\t主机名"+urls[i].getHost();
    str +="\tIP地址:"+InetAddress.getByName(urls[i].getHost()).getHostAddress();

    }
    catch(UnknownHostException ioe)
    {
    JOptionPane.showMessageDialog(this, "找不到指定主机的IP地址");
    }
    catch(IOException ioe)
    {
    JOptionPane.showMessageDialog(this, "IO 错,读取"+urls[i].getFile()+"文件不成功");
    }
    }
    text_attribute.setText(str);//文件属性添加到文本行
    }
    public static void main(String args[])
    {
    new HtmlJFrame();
    }}
    还能帮我改下这个吗  真的很烦