如何显示ImageIcons在JTextPane只有垂直,一个低于另一个?



我正在从设备中获取BufferdImages并将它们转换为ImageIcons并在JTextPane中显示它们。

public static void insertImage(BufferedImage img, JTextPane jTextPane){
ImageIcon icon = new ImageIcon(img);
jTextPane.insertIcon(icon);
}

我的问题是图像在一行中一个接一个地添加,我必须水平滚动才能看到它们。我希望每个新的ImageIcon都位于前一个ImageIcon的下面,换句话说,在每个ImageIcon后面添加一个新行。

我尝试在添加每个图像后做jTextPane.setText("n");,但这只重置整个jTextPane。

我需要在一些其他数据的Swing应用程序中显示这些图像,如果有人有一个更好的建议,关于什么使用而不是jTextPane用于此目的,请建议它。

提前感谢。

下面是如何使用底层的StyledDocument在JTextPane中垂直对齐图像。使用此文档,您可以在您选择的位置手动添加换行符。

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

最新更新