在JTextpane中插入两个图标

时间:2016-06-08 21:54:23

标签: java

如何在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 个答案:

答案 0 :(得分:0)

通过将图像放入JLabel并将JTextpane设置为Box Layout来解决它:

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