传入的是html代码,JEditorPane怎样显示网页?

IEQQ 2007-03-05 02:39:26
已知JEditorPane.setPage(URL ); 可以显示一个网页.

如果传入的参数不是url 而是 "<font colro='#ff0000'>abc</font>",
怎样达到类似的效果?

谢谢先.
...全文
490 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
IEQQ 2007-03-06
  • 打赏
  • 举报
回复
应该说自己找到了, 其实还是人家帮忙搞定的.
IEQQ 2007-03-06
  • 打赏
  • 举报
回复
自己搞定了. 下面是方法.

方法一
這是比較基本的方法

JEditPane XD = new JEditPane();
XD.setEditorKit(new HTMLEditorKit());
XD.setText("<h1>zz</h1>");

[編輯]方法二
方法一最大的缺點在於說,如果你有一狗票的 html 內容,要這麼傻傻地用 setText() 的方式去輸入嗎?當然有別的方法。首先我們另外寫一個 html 檔,暫且稱之為「sample.html」,接著用 InputStreamer 去讀取它。

JEditPane XD = new JEditPane();
XD.setContentType("text/html");
HTMLEditorKit kit = new HTMLEditorKit();
XD.setEditorKit(kit);

HTMLDocument doc = (HTMLDocument)XD.getDocument();
File f = new File("sample.html");
try {
FileInputStream reader = new FileInputStream(f);
kit.read(reader,doc,0);
}catch(Exception e) {
e.printStackTrace();
}
lixiaoxue85 2007-03-05
  • 打赏
  • 举报
回复
package csdn;

/**
@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.*;

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

62,614

社区成员

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

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