有没有办法让多行 JLabel 文本围绕图标流动?
Is there a way to make multiline JLabel text flow around the icon?
目前在我的项目中我有多个 JLabel,每个都有自己的文本和图标
我的问题是图标占据了整个左侧
我想要达到的效果是让文字围绕图标流动
JLabel 可以实现这种效果吗?如果不能,是否可以使用任何其他组件?
如果这是一个 JLabel 而不是文本可编辑的 JTextArea,我会使用 HTMLed JLabel 而不是 JEditorPane。
public static void main(String[] args) throws Exception {
String imageUrl = "https://d1nhio0ox7pgb.cloudfront.net/_img/g_collection_png/standard/512x512/person.png";
Image sourceImage = ImageIO.read(new URI(imageUrl).toURL()).getScaledInstance(25, 25,
Image.SCALE_SMOOTH);
//Write the image to disk locally
File fileWithImage = File.createTempFile("myicon", ".png");
fileWithImage.deleteOnExit();
ImageIO.write(toBufferedImage(sourceImage), "png", fileWithImage);
String lorem = "Lorem Ipsum is simply dummy text of the printing<br>"
+ " and typesetting industry. Lorem Ipsum has been the industry's<br>"
+ " standard dummy text ever since the 1500s, when an unknown printer<br>"
+ " took a galley of type and scrambled it to make a type specimen book.<br>"
+ " It has survived not only five centuries, but also the leap into electronic<br>"
+ " typesetting, remaining essentially unchanged. It was popularised in the 1960s<br>"
+ " with the release of Letraset sheets containing Lorem Ipsum passages ";
String imgTag = "<img src=\"" + fileWithImage.toURI() + "\">";
final String htmlText = "<html>" + imgTag + " " + lorem;
JOptionPane.showMessageDialog(null, new JLabel(htmlText));
}
public static BufferedImage toBufferedImage(Image img) {
if (img instanceof BufferedImage) {
return (BufferedImage) img;
}
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
return bimage;
}
运行如上例。您将得到以下图像:
图像转换取决于您如何拥有图标,但想法是这样的。将图像写入临时文件,将其设为 URI,并将其作为 HTML 文本添加到标签中。
is it possible with any other component?
您可以使用 JTextPane
.
它支持 insertIcon(...)
方法,允许您在文本中的任意位置插入图标。
您可以将文本窗格设置为不可编辑和透明,使其外观和行为更像 JLabel。
目前在我的项目中我有多个 JLabel,每个都有自己的文本和图标
我的问题是图标占据了整个左侧
我想要达到的效果是让文字围绕图标流动
JLabel 可以实现这种效果吗?如果不能,是否可以使用任何其他组件?
如果这是一个 JLabel 而不是文本可编辑的 JTextArea,我会使用 HTMLed JLabel 而不是 JEditorPane。
public static void main(String[] args) throws Exception {
String imageUrl = "https://d1nhio0ox7pgb.cloudfront.net/_img/g_collection_png/standard/512x512/person.png";
Image sourceImage = ImageIO.read(new URI(imageUrl).toURL()).getScaledInstance(25, 25,
Image.SCALE_SMOOTH);
//Write the image to disk locally
File fileWithImage = File.createTempFile("myicon", ".png");
fileWithImage.deleteOnExit();
ImageIO.write(toBufferedImage(sourceImage), "png", fileWithImage);
String lorem = "Lorem Ipsum is simply dummy text of the printing<br>"
+ " and typesetting industry. Lorem Ipsum has been the industry's<br>"
+ " standard dummy text ever since the 1500s, when an unknown printer<br>"
+ " took a galley of type and scrambled it to make a type specimen book.<br>"
+ " It has survived not only five centuries, but also the leap into electronic<br>"
+ " typesetting, remaining essentially unchanged. It was popularised in the 1960s<br>"
+ " with the release of Letraset sheets containing Lorem Ipsum passages ";
String imgTag = "<img src=\"" + fileWithImage.toURI() + "\">";
final String htmlText = "<html>" + imgTag + " " + lorem;
JOptionPane.showMessageDialog(null, new JLabel(htmlText));
}
public static BufferedImage toBufferedImage(Image img) {
if (img instanceof BufferedImage) {
return (BufferedImage) img;
}
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
return bimage;
}
运行如上例。您将得到以下图像:
图像转换取决于您如何拥有图标,但想法是这样的。将图像写入临时文件,将其设为 URI,并将其作为 HTML 文本添加到标签中。
is it possible with any other component?
您可以使用 JTextPane
.
它支持 insertIcon(...)
方法,允许您在文本中的任意位置插入图标。
您可以将文本窗格设置为不可编辑和透明,使其外观和行为更像 JLabel。