2016-06-08 39 views
-1

如何將多個圖像添加到JTextPane中?在JTextpane中插入兩個圖標

我有以下代碼只附加第一個圖像。

Image img = null; 
Image img2 = null; 

try { 
    img = ImageIO.read(new File(Main.imagePath + "1.jpg")); 
    img2 = ImageIO.read(new File(Main.imagePath + "2.jpg")); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

Image simg = img.getScaledInstance(310, 90, Image.SCALE_SMOOTH); 
Image simg2 = img2.getScaledInstance(310, 90, Image.SCALE_SMOOTH); 

Main.ScreenText.insertIcon (new ImageIcon(simg)); 
Main.ScreenText.insertIcon (new ImageIcon(simg2)); 

ScreenText是一個JTextPane。

+1

我想這和它工作得很好。我發現無法看到第二張圖像的唯一潛在原因是容器太小而不在屏幕上時,這可能是您的問題? – Peter

回答

0

通過將圖像轉換成的JLabel和設置JTextPane對象框佈局解決了這個問題:

Image img = null; 
Image img2 = null; 

try { 
    img = ImageIO.read(new File(Main.imagePath + "1.jpg")); 
    img2 = ImageIO.read(new File(Main.imagePath + "2.jpg")); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

Image simg = img.getScaledInstance(310, 90, Image.SCALE_SMOOTH); 
Image simg2 = img2.getScaledInstance(310, 90, Image.SCALE_SMOOTH); 

JLabel simgLabel = new JLabel(new ImageIcon(simg)); 
JLabel simgLabel2 = new JLabel(new ImageIcon(simg2)); 

Main.ScreenText.setLayout(new BoxLayout(Main.ScreenText, BoxLayout.Y_AXIS)); 
Main.ScreenText.insertComponent(simgLabel); 
Main.ScreenText.insertComponent(simgLabel2);