JEditorPane

lvxn_sina 2006-12-14 01:39:13
为什么JEditorPane的read(InputStream,object)总是抛出NullPointException.

怎么样才能从InputStream中加载HTML页面呢?
...全文
242 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lvxn_sina 2006-12-20
  • 打赏
  • 举报
回复
使用URL是可以的,但数据在数据库中,不存在真实的地址,由动态库进行重组,解压,解密后生成的数据流。
fool_leave 2006-12-20
  • 打赏
  • 举报
回复
找问题是比较烦

我还是觉得InputStream is = CreateInputStream(strURL); 有问题。

你可以把后面的代码改一下,通过这个is来读取String,然后把String打印出来,看看有没有问题。其实CreateInputStream(strURL); 和URL.openStream()一样的,你也可以通过openStream对流作任何操作,甚至包括加密解密
lixiaoxue85 2006-12-20
  • 打赏
  • 举报
回复
/**
@version 1.02 2004-08-22
@author Cay Horstmann
*/

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

/**
This program demonstrates how to display HTML documents
in an editor pane.
*/
public class EditorPaneTest
{
public static void main(String[] args)
{
JFrame frame = new EditorPaneFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

/**
This frame contains an editor pane, a text field and button
to enter a URL and load a document, and a Back button to
return to a previously loaded document.
*/
class EditorPaneFrame extends JFrame
{
public EditorPaneFrame()
{
setTitle("EditorPaneTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

final Stack<String> urlStack = new Stack<String>();
final JEditorPane editorPane = new JEditorPane();
final JTextField url = new JTextField(30);

// set up hyperlink listener

editorPane.setEditable(false);
editorPane.addHyperlinkListener(new
HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkEvent event)
{
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
{
try
{
// remember URL for back button
urlStack.push(event.getURL().toString());
// show URL in text field
url.setText(event.getURL().toString());
editorPane.setPage(event.getURL());
}
catch (IOException e)
{
editorPane.setText("Exception: " + e);
}
}
}
});

// set up checkbox for toggling edit mode

final JCheckBox editable = new JCheckBox();
editable.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
editorPane.setEditable(editable.isSelected());
}
});

// set up load button for loading URL

ActionListener listener = new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
// remember URL for back button
urlStack.push(url.getText());
editorPane.setPage(url.getText());
}
catch (IOException e)
{
editorPane.setText("Exception: " + e);
}
}
};

JButton loadButton = new JButton("Load");
loadButton.addActionListener(listener);
url.addActionListener(listener);

// set up back button and button action

JButton backButton = new JButton("Back");
backButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (urlStack.size() <= 1) return;
try
{
// get URL from back button
urlStack.pop();
// show URL in text field
String urlString = urlStack.peek();
url.setText(urlString);
editorPane.setPage(urlString);
}
catch (IOException e)
{
editorPane.setText("Exception: " + e);
}
}
});

add(new JScrollPane(editorPane), BorderLayout.CENTER);

// put all control components in a panel

JPanel panel = new JPanel();
panel.add(new JLabel("URL"));
panel.add(url);
panel.add(loadButton);
panel.add(backButton);
panel.add(new JLabel("Editable"));
panel.add(editable);

add(panel, BorderLayout.SOUTH);
}

private static final int DEFAULT_WIDTH = 600;
private static final int DEFAULT_HEIGHT = 400;
}
fool_leave 2006-12-14
  • 打赏
  • 举报
回复
把代码贴出来。这样怎么知道

必须从IS里加载吗?用setPage不可以吗
fool_leave 2006-12-14
  • 打赏
  • 举报
回复
JEditorPane p=new JEditorPane();
JFrame f=new JFrame();
f.getContentPane().add(p,BorderLayout.CENTER);
URL url=new URL("http://www.sina.com.cn");
p.setEditorKit(p.getEditorKitForContentType("text/html"));
p.read(url.openStream(),p.getEditorKit().createDefaultDocument());
f.show();


没问题

我想你的问题在CreateInputStream方法,看看传入的string是不是个url格式.为什么不用URL的openStream呢?
lvxn_sina 2006-12-14
  • 打赏
  • 举报
回复
动态库实现的InputStream

private class View extends JEditorPane
{
public View()
{
setEditable(false);
setEditorKit(getEditorKitForContentType("text/html"));
}
public Set(String strURL)
{
InputStream is = CreateInputStream(strURL); // 动态库实现的stream,测试没问题
read(is,getEditorKit().createDefaultDocument()); // 异常
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧