[英]How to show ImageIcons in JTextPane only vertically, one below the other?
我從設備獲取 BufferdImages 並將它們轉換為 ImageIcons 並在 JTextPane 中顯示它們。
public static void insertImage(BufferedImage img, JTextPane jTextPane){
ImageIcon icon = new ImageIcon(img);
jTextPane.insertIcon(icon);
}
我的問題是圖像在一行中一個接一個地添加,我必須水平滾動才能看到它們。 我希望每個新的 ImageIcon 都比前一個 ImageIcon 低 go,換句話說,在每個 ImageIcon 之后添加一個新行。
我試着做jTextPane.setText("\n");
添加每個圖像后,但這只會重置整個 jTextPane。
我需要在 Swing 應用程序中顯示這些圖像以及其他一些數據,如果有人對此有更好的建議而不是 jTextPane,請提出建議。
提前致謝。
以下是使用底層StyledDocument添加新行的方法。 使用此文檔,您可以在您選擇的 position 處手動添加換行符。
BufferedImage img1 = ...
BufferedImage img2 = ...
// add first image
jTextPane.insertIcon(img1);
// add line break at the end
StyledDocument doc = jTextPane.getStyledDocument();
doc.insertString(doc.getLength(), "\n" , null);
// add second image
jTextPane.insertIcon(img2);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.