JTextpane не показывает фотографии с HTMLEditorkit и HTMLDocument (неработающие значки) - PullRequest
0 голосов
/ 06 апреля 2019

Jtextpane с HTMLEditorKit и HTMLDocument не отображает фотографию и не отображает поврежденную иконку, когда я сохраняю файл, и нахожу, что фотография загружена в созданный HTML-файл

try {
                    File f =im.file;
                    icon = new ImageIcon(f.getName());

                    int w = 300;
int h =300;
                    MutableAttributeSet attr = new SimpleAttributeSet();
attr.addAttribute(StyleConstants.NameAttribute,
HTML.Tag.IMG);
attr.addAttribute(HTML.Attribute.SRC, f.getAbsolutePath());
attr.addAttribute(HTML.Attribute.HEIGHT,
Integer.toString(h));
attr.addAttribute(HTML.Attribute.WIDTH,
Integer.toString(w));
int p = editor.getCaretPosition();
doc.insertString(p, " ", attr);

                   HTMLDocument docx = (HTMLDocument)editor.getDocument();
                    HTMLEditorKit kit=(HTMLEditorKit)editor.getEditorKit();
         String filename = f.getAbsolutePath();
    String preTag="filename is : "+filename+"
"; String imageTag =""; String s =" image "; // kit.insertHTML (doc, doc.getLength (), s, 0, 0, HTML.Tag.IMG); doc.insertString(editor.getCaretPosition (), "\ r \ n", editor.getInputAttributes ());} `` `` [см. сломанный значок [\] \ [1 \] [1] [1]: https://i.stack.imgur.com/memDr.png

1 Ответ

0 голосов
/ 07 апреля 2019

вот как это было сделано с использованием `` `` file.toURI.toURL (). ToString ();`` `` `

try {
                    File f =im.file;
                    icon =(ImageIcon) label.getIcon();
             String filename= f.toURI().toString();  
                    int w =icon.getIconWidth();
int h =icon.getIconHeight();

                    MutableAttributeSet attr = new SimpleAttributeSet();
attr.addAttribute(StyleConstants.NameAttribute,HTML.Tag.IMG);
attr.addAttribute(HTML.Attribute.SRC, filename);
attr.addAttribute(HTML.Attribute.HEIGHT,
Integer.toString(h));
attr.addAttribute(HTML.Attribute.WIDTH,
Integer.toString(w));
int p = editor.getCaretPosition();
doc.insertString(p, " ", attr);
 doc.insertString(editor.getCaretPosition(), "\r\n", editor.getCharacterAttributes());







                } catch (Exception ex) {
                    }

        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...