【问题标题】:Editing html text with JEditorPane使用 JEditorPane 编辑 html 文本
【发布时间】:2014-02-08 01:25:14
【问题描述】:

我写了一个文本编辑器,将文本保存为 html。

我对粗体、斜体等样式没有任何问题,我唯一遇到的问题是当我按下回车键时它的行为方式。 它不是创建一条新的法线,而是创建一条额外的间隔线。我认为这与<p> 标签有关,但我不确定... 无论如何,这是我的问题的一个例子:

import java.awt.BorderLayout;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.html.HTMLEditorKit;

public class test extends JFrame {

    public static void main(String[] args){
        new test().open();
    }

    private void open() {
        setSize(200, 200);
        JEditorPane jp = new JEditorPane();
        setLayout(new BorderLayout());
        add(jp, BorderLayout.CENTER);

        jp.setEditorKit(new HTMLEditorKit());
        jp.setText("<html><body><p>hey</p><p>Write in here</p></body></html>");
        setVisible(true);
    }
}

有没有办法解决这个问题?

【问题讨论】:

  • 你能贴出处理关键事件的代码吗?
  • @JuanManuel 我没有任何处理关键事件的代码:/
  • 看看HTMLDocumentEditor,引用here

标签: java html swing jeditorpane


【解决方案1】:

我认为您正在尝试在呈现 HTML 后直接在 JEditorPane 上进行编辑。 JEdi​​torPane 读入您提供的文本,并根据文本随附的 HTML 标记呈现它。所以,如果你想让它在你的文本中的某个点有一个换行符,那么你需要输入一个中断标签,如下所示:

public class test extends JFrame
{
  public static void main(String[] args){
    test t = new test();
    t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    t.open();
  }

  private void open()
  {
    setSize(200, 200);
    JEditorPane jp = new JEditorPane();
    jp.setEditable(false);
    setLayout(new BorderLayout());
    add(jp, BorderLayout.CENTER);

    jp.setEditorKit(new HTMLEditorKit());
    jp.setText("<html><body><p>hey</p><p>Don't write here.<br>Let JEditor rendor your HTML text. </p></body></html>");
    setVisible(true);
  }
}

希望这会有所帮助。

【讨论】:

  • 感谢您的回答,但这实际上并没有回答问题... JEditorPane 旨在让用户编辑 HTML 文件的文本(每个示例),而不仅仅是查看它。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多