Java <;输入>;输入JEditorPane(带HtmlEditorKit) 如果我把光标放在中> p>一行文本并点击回车,我得到

Java <;输入>;输入JEditorPane(带HtmlEditorKit) 如果我把光标放在中> p>一行文本并点击回车,我得到,java,swing,newline,paragraph,htmleditorkit,Java,Swing,Newline,Paragraph,Htmleditorkit,一行 正文 这是在我的Linux开发机器上 当我在Windows计算机上运行相同的应用程序时,我得到 <p>A line of text</p> 一行 正文 i、 e.插入\n而不是创建额外的元素。由于\n在HTML中只是呈现为一个空格,所以在Windows上,enter基本上不起作用 问题:如何在Windows上按enter键时强制执行插入行为?根据enter键映射到“插入中断”动作 在Windows中,此操作在DefaultEditorKit中定义: publi

一行

正文

这是在我的Linux开发机器上

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

<p>A line
of text</p>
一行
正文

i、 e.插入
\n
而不是创建额外的
元素。由于
\n
在HTML中只是呈现为一个空格,所以在Windows上,enter基本上不起作用

问题:如何在Windows上按enter键时强制执行插入行为?

根据enter键映射到“插入中断”动作

在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");
        }
    }
}
并按照您的建议简单地添加“\n”

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


因此,我建议您需要找到该操作,然后将其添加到Windows平台的HTMLEditorKit中。

我的调试也显示了这一点。事实上,它实际上在
StyledEditorKit
中使用了
StyledInsertBreakAction
。在不同的机器中使用不同的操作,这会让我感到惊讶。我怀疑这与系统行结尾有关。我曾尝试在不同的Windows机器上复制它,但运气不佳:-/
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");
        }
    }
}