2012-02-20 62 views
2

_http圖像的對準://i.stack.imgur.com/W2We5.gifJEditorPane中,在文本

<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中工作。我怎麼解決這個問題?

回答

3

要對齊你的圖像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屬性可以做到這一點招你。關於那個圖片不應該影響行的大小,我不確定你的意圖,但是如果我理解你的話,那麼你可以在<img>標記中爲圖片提供固定的寬度和高度。

在這裏,我使用了這段代碼,告訴我你是否想要別的東西,除了你在這段代碼中找到的東西。希望我能幫上忙,

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(); 
      } 
     }); 
    } 
} 

這裏是輸出圖像:

EditorPane's View

而且使用此Smiley Image。我已經從圖像中刪除了額外的底部空間。

+0

縮小圖像大小並不能解決問題,因此有必要讓圖片位於上方和下方文本的後面。例如: _http://i.stack.imgur.com/WlMhG.gif – user1221483 2012-02-20 18:28:30

+0

@ user1221483:您可以發佈您實際使用的將圖像和文本放在JEditorPane上的代碼。將欣賞這一點。 – 2012-02-20 18:30:49

+0

字符串str =新的String( 「 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
AAAAAAAAAA 」 aaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
「); JEditorPane.setText(STR); – user1221483 2012-02-20 18:45:23