【问题标题】:JEditorPane, alignment of images in the textJEdi​​torPane,文本中图像的对齐方式
【发布时间】:2012-03-11 00:38:26
【问题描述】:

_http://i.stack.imgur.com/W2We5.gif

<img src=\"file:b:/smile.gif\" align=\"middle\">"

_http://i.stack.imgur.com/VPHzw.gif

<img src=\"file:b:/smile.gif\">

必填:
_http://i.stack.imgur.com/WlMhG.gif

我需要在 JEditorPane 中对齐图像,并且图像不应影响行的高度。如果我使用 align=middle - 图片未对齐并保留对行高的影响。

我使用的html中类似问题的解决方案:

<span style=\"background-image: url('file:b:/smile.gif') 50% 50% no-repeat\"> &nbsp;&nbsp;&nbsp;&nbsp;</span>

但是这种方法在 JEditorPane 中不起作用。我该如何解决这个问题?

【问题讨论】:

    标签: java image swing alignment jeditorpane


    【解决方案1】:

    要将您的图像与 JEditorPane 的中间对齐,您可以使用以下内容:

    editPane.setText("<html><p style = \"text-align:center;\"><img src = " + 
            "\"http://gagandeepbali.uk.to/gaganisonline/images/" + 
            "editsystemvariable2.png\" alt = \"pic\" /></p></html>\n");
    

    这里的 text-align 属性可以帮你解决这个问题。关于图像不应该影响行大小的事情,我不确定您的意图,但如果我理解正确,那么您可以在&lt;img&gt; 标签中为您的图像提供固定的宽度和高度。

    我在这里使用了这段代码,如果你想要别的东西,除了你在这段代码中找到的东西。希望我能帮忙,

    import java.awt.*;
    import javax.swing.*;
    
    public class EditorPaneTest extends JFrame
    {
        public EditorPaneTest()
        {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationByPlatform(true);        
    
            JEditorPane editPane = new JEditorPane();
            JScrollPane scrollPane = new JScrollPane(editPane);     
    
            editPane.setContentType("text/html");
    
            editPane.setText("<html><p style = \"text-align:center;\">Hello there, How you doing ?<img src = " + 
                                                "\"http://s018.radikal.ru/i504/1202/03/c01a2e35713f.gif" + 
                                                    "\" alt = \"pic\" width = \"15\" height = \"15\" />I guess all is good!!" +
                                                            "<br />I hope this is what you wanted!! " + 
                                                                        "<img src =  \"http://s018.radikal.ru/i504/1202/03/c01a2e35713f.gif" + 
                                                    "\" alt = \"pic\" width = \"15\" height = \"15\" /> Hope this works for you :-)</p></html>\n");
    
            add(scrollPane, BorderLayout.CENTER);
            setSize(400, 300);
            setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new EditorPaneTest();
                }
            });
        }
    }
    

    这是输出图像:

    更多使用此。我已经为你删除了图像中多余的底部空间。

    【讨论】:

    • 缩小图片大小并不能解决问题,需要让图片在上下文字的后面。示例:_http://i.stack.imgur.com/WlMhG.gif
    • @user1221483 :请您发布实际用于将图像和文本放在 JEditorPane 上的代码。会很感激的。
    • String str = new String(" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
      aaaaaaaaaaa "aaaaaaaaaaaaaaaaaa
      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
      "); JEditorPane.setText(str);
    • 可惜图片依然不在中间行,仍然影响高度_http://s017.radikal.ru/i428/1202/08/bc5c139e0c48.png 微笑:20x20px _http://s018 .radikal.ru/i504/1202/03/c01a2e35713f.gif JEditorPane 有更灵活的替代方案吗?
    • @user1221483 :现在检查图像,这是您想要的吗?
    猜你喜欢
    • 2020-03-20
    • 2014-06-30
    • 2013-10-06
    • 1970-01-01
    • 2012-03-14
    • 2019-06-01
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    相关资源
    最近更新 更多