使用 ­在 Java HTML 感知组件中

发布于 2024-10-02 01:25:46 字数 2071 浏览 4 评论 0原文

我有以下问题:

我使用 JTextPane 显示 HTML 文档。

在我的 HTML 文本中有 ­ (shy at w3.org) 进行软连字符。 我的问题是,没有出现连字符。是否有某种我不知道的标志来使用这些选项?

以下程序将显示问题:

package com.dvelop.ckue.swing;

import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
import java.awt.*;

public class SwingGui extends JFrame {

    public static void main(String[] args) {
        SwingGui sg = new SwingGui();
        sg.setSize(new Dimension(200, 800));
        sg.setPreferredSize(new Dimension(200, 800));
        sg.pack();
        sg.setVisible(true);
        sg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private SwingGui() {
        super();
        setLayout(new FlowLayout());

        // No "-" appears, but a linebreak
        add(createField("<html>longlong<br>longlong<br>longlonglonglonglonglonglonglonglonglongWord"));
        // No linebreak, but the hyphenationsymbol
        add(createField("<html>longlong&shy;longlong&shy;longlonglonglonglonglonglonglonglonglongWord"));
        // Linebreak, but not where expected and no symbol            
        add(createField("<html>longlong&#8203;longlong&#8203;longlonglonglonglonglonglonglonglonglongWord"));
        // No linebreak, no symbol
        add(createField("<html>longlonglonglonglonglonglonglonglonglonglonglonglonglongWord"));
    }

    private JTextPane createField(String content) {
        JTextPane field1 = new JTextPane();
        field1.setPreferredSize(new Dimension(100, 200));
        field1.setAutoscrolls(true);
        field1.setEditorKit(new HTMLEditorKit());
        field1.setText(content);
        return field1;
    }

}

我的预期行为是,我的文本将被分解到下一行:

longlong- 
longlong-
longlonglonglongWord

正如第一个块所示,但带有连字符。

编辑: 它适用于大多数浏览器,但我在这里不使用网络浏览器。

编辑2: 我使用 JTextPane,我不知道 Java 是否会在内部使用一些安装的 HTML 渲染引擎。

I have following problem:

I display an HTML-Document with an JTextPane.

In my HTML-Text there are ­ (shy at w3.org) to make a soft-hyphenation.
My problem is, that no hyphenation appears. Is there some kind of flag, which I don't know, to use these option?

Following Programm will show the problem:

package com.dvelop.ckue.swing;

import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
import java.awt.*;

public class SwingGui extends JFrame {

    public static void main(String[] args) {
        SwingGui sg = new SwingGui();
        sg.setSize(new Dimension(200, 800));
        sg.setPreferredSize(new Dimension(200, 800));
        sg.pack();
        sg.setVisible(true);
        sg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private SwingGui() {
        super();
        setLayout(new FlowLayout());

        // No "-" appears, but a linebreak
        add(createField("<html>longlong<br>longlong<br>longlonglonglonglonglonglonglonglonglongWord"));
        // No linebreak, but the hyphenationsymbol
        add(createField("<html>longlong­longlong­longlonglonglonglonglonglonglonglonglongWord"));
        // Linebreak, but not where expected and no symbol            
        add(createField("<html>longlong​longlong​longlonglonglonglonglonglonglonglonglongWord"));
        // No linebreak, no symbol
        add(createField("<html>longlonglonglonglonglonglonglonglonglonglonglonglonglongWord"));
    }

    private JTextPane createField(String content) {
        JTextPane field1 = new JTextPane();
        field1.setPreferredSize(new Dimension(100, 200));
        field1.setAutoscrolls(true);
        field1.setEditorKit(new HTMLEditorKit());
        field1.setText(content);
        return field1;
    }

}

My expected behavior is, that my text will be broken to the next line:

longlong- 
longlong-
longlonglonglongWord

As seem by the first block, but with an hyphenation-sign.

EDIT:
It will work in most browsers, but I don't use a webbrowser here.

EDIT 2:
I use a JTextPane, I don't know, if Java will use some installes HTML rendering-engine internally.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世态炎凉 2024-10-09 01:25:46

许多浏览器不处理这个字符。最好使用实体名称 (­),而不是 ISO 数字实体 (­)。

但大多数浏览器对这个实体的处理都非常糟糕。

只是一个零宽度空格字符

更简单的是:(但连字符始终可见......)

<p>longlonglong-​longlonglong</p>

你甚至可以尝试这个(但我不认为你看到你的连字符......):

<p>longlonglong­​longlonglong</p>

但我不明白你为什么插入纯文本就在 节点之后,这不会减轻浏览器的任务,不是吗?

否则,您在什么浏览器上测试您的代码?因为这在最新版本的 Chrome 和 Firefox 上完美运行:

<p>longlonglong­longlonglong</p>

无论如何,您可能会对 感兴趣 标签,以及 这个专门讨论软连字符问题的文章...

Many browsers do not handle this character. It is preferable to use the name of the entity (­) instead of the ISO numeric entity (­).

But this entity is pretty bad handled by most browsers.

is just a zero width space character.

The simpler is: (but the hyphen is always visible...)

<p>longlonglong-​longlonglong</p>

And you can even try this (but I don't think that you see your hyphen...):

<p>longlonglong­​longlonglong</p>

But I don't understand why you insert plain-text just after an <html> node, this should not ease the task of the browser, isn't it?

Otherwise, on what browser(s) do you test your code? Because this work perfectly on the lastest versions of Chrome and Firefox:

<p>longlonglong­longlonglong</p>

Anyway you'll may be interested by the <wbr> tag, and this article dedicated to the soft hyphen problem...

陈年往事 2024-10-09 01:25:46

http://java-sl.com/Hyphenation_In_JEditorPane.html
这是自定义连字符的一个例子。您可以使用相同的方法并更改 HTMLEditorKit 以使用连字符。

http://java-sl.com/Hyphenation_In_JEditorPane.html
That's an example of custom hyphnation. You can use the same approach and change the HTMLEditorKit to use your hyphens.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文