JEdi​​torPane中的<enter>键的行为(使用HtmlEditorKit)

时间:2018-02-15 10:18:38

标签: java swing newline paragraph htmleditorkit

如果我将光标放在<p>A line of text</p>的中间并点击,请输入

<p>A line</p>
<p>of text</p>

这是在我的Linux开发机器上。

当我在Windows机器上运行相同的应用程序时,我得到了

<p>A line
of text</p>

即。插入\n而不是创建额外的<p>元素。由于\n只是在HTML中呈现为空格,因此当我在Windows上时, enter 基本上无法正常工作。

问题:如何在Windows上输入时强制执行插入<p> 行为?

1 个答案:

答案 0 :(得分:1)

根据,Enter键被映射到&#34; insert-break&#34;采取行动。

在Windows中,此操作在DefaultEditorKit

中定义
public static class InsertBreakAction extends TextAction {

    /**
     * Creates this object with the appropriate identifier.
     */
    public InsertBreakAction() {
        super(insertBreakAction);
    }

    /**
     * The operation to perform when this action is triggered.
     *
     * @param e the action event
     */
    public void actionPerformed(ActionEvent e) {
        JTextComponent target = getTextComponent(e);
        if (target != null) {
            if ((! target.isEditable()) || (! target.isEnabled())) {
                UIManager.getLookAndFeel().provideErrorFeedback(target);
                return;
            }
            target.replaceSelection("\n");
        }
    }
}

并简单地添加&#34; \ n&#34;正如你的建议。

如果您在Linux上有不同的行为,那么我猜想会在HTMLEditorKit中添加一个自定义Action以插入段落标记。

所以我建议您需要找到Action,然后将其添加到Windows平台的HTMLEditorKit中。