【问题标题】:Behavior of <enter> key in JEditorPane (with HtmlEditorKit)JEdi​​torPane 中 <enter> 键的行为(使用 HtmlEditorKit)
【发布时间】:2018-02-15 10:18:38
【问题描述】:

如果我将光标放在 &lt;p&gt;A line of text&lt;/p&gt; 的中间并点击 enter 我得到

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

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

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

<p>A line
of text</p>

即插入 \n 而不是创建额外的 &lt;p&gt; 元素。由于\n 只是在 HTML 中呈现为空格,所以 enter 在我在 Windows 上时基本上不起作用。

问题:如何在 Windows 上 enter 时强制 insert &lt;p&gt; 行为?

【问题讨论】:

    标签: java swing newline paragraph htmleditorkit


    【解决方案1】:

    根据Key Bindings,Enter 键映射到“插入-中断”Acton。

    在 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 中添加了一个自定义操作来插入段落标签。

    所以我建议你需要找到那个Action,然后将它添加到Windows平台的HTMLEditorKit中。

    【讨论】:

    • 这也是我的调试结果。事实上,它实际上在StyledEditorKit 中使用了StyledInsertBreakAction。在不同的机器上使用不同的动作会让我感到惊讶。我怀疑它与系统行结尾有关。我试图在另一台 Windows 机器上重现它,但是没有运气:-/
    猜你喜欢
    • 2012-03-11
    • 2011-05-24
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 2011-04-11
    • 2011-06-06
    • 2011-08-20
    • 2013-10-06
    相关资源
    最近更新 更多