在JTextPane中显示选定的图像 [英] Display selected image in a JTextPane

查看:93
本文介绍了在JTextPane中显示选定的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的文本窗格(jTextPaneBody)中显示从文件选择器中选择的图像?这是我到目前为止的代码,但是我不知道要实现此目的还需要添加什么.

How can I show an image I have selected from the file chooser in a my textpane (jTextPaneBody)? This is the code I have so far but I don't know what else needs to be added in order to achieve this.

private void jButtonAttachActionPerformed(java.awt.event.ActionEvent evt) {                                              
        JFileChooser jc = new JFileChooser();
        jc.setDialogType(JFileChooser.OPEN_DIALOG);
        jc.showOpenDialog(null);
        File f = jc.getSelectedFile();
    } 

推荐答案

首先查看如何使用标签阅读/加载图像

BufferedImage img = ImageIO.read(f);
JLabel label = new JLabel(new ImageIcon(img));

然后,您可以使用

Then you can use JTextPane#insertIcon or JTextPane#insertComponent to physically add the image based on your needs

这篇关于在JTextPane中显示选定的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆