java - 如何在 JTextPane java 中复制图像?

标签 java image swing jtextarea jfilechooser

我想知道如何在 JTextPane 中复制图像和文本。 当我使用这段代码时,它只复制文本,但我想复制文本和图像。如何做到这一点?

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;

/**
  *
     * @author admin
                     */
public class Main extends JFrame implements KeyListener, ActionListener{
public static JTextPane textPane;
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
                JFrame Frame = new Main();
                Frame.setVisible(true);
                Frame.setSize(400, 400);





}
public Main()
{
    JMenuBar mb = new JMenuBar();
    setJMenuBar(mb);

    JMenu menu = new JMenu("File");
    JMenuItem mi = new JMenuItem("select all");
    mi.addActionListener(this);
    menu.setMnemonic(KeyEvent.VK_F);
    menu.add(mi);
    mi = new JMenuItem("copy");
    mi.addActionListener(this);
    menu.add(mi);
    mi = new JMenuItem("Exit");
    mi.addActionListener(this);
    menu.add(mi);
     mi = new JMenuItem("insert image");
    mi.addActionListener(this);
    menu.add(mi);
    mb.add(menu);
    textPane = new JTextPane();

    JScrollPane scrollPane = new JScrollPane(textPane);
    getContentPane().add(scrollPane);


}

public void keyTyped(KeyEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void keyPressed(KeyEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void keyReleased(KeyEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void actionPerformed(ActionEvent e) {
    String cmd=e.getActionCommand();
    if ("Exit".equals(cmd)) {
        System.exit(0);
    } else if ("select all".equals(cmd)) {
        textPane.selectAll();
    } 
    else if ("copy".equals(cmd)) {
      textPane.copy();

    }
    else if("insert image".equals(cmd))
    {
        try {
            JFileChooser file = new JFileChooser();
            file.showOpenDialog(null);
            File selFile = file.getSelectedFile();
            Image img = ImageIO.read(selFile);
            textPane.insertIcon(new ImageIcon(img));
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
 }

}

最佳答案

恐怕没有简单的方法可以做到这一点。 所有默认的 EditorKit(StyledEditorKit、HTMLEditorKit、RTFEditorKit)都不支持图像复制。

最接近的是 HTMLEditorKit,但它会生成带有图像链接的 HTML。

您可以实现自己的读取器/写入器。参见 http://java-sl.com/editor_kit_tutorial.html关于读者和作者的章节。

关于java - 如何在 JTextPane java 中复制图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6208530/

相关文章:

java - Hibernate 和 AspectJ 字段访问

Java 问题 : is float not allowed to be declared as final?

java - Neo4j 在同一查询中删除/设置标签和属性

javascript - 显示随机背景图像

java - 更新 JFrame 内容

java - 如何将图像添加到 JFrame 项目

java - 从相机捕获图像并设置到android中的 ListView 中

ios - 图像比例 AF 网络

java - java中jscrollpane的问题

java - 当鼠标在 JFrame 内移动时,Swing Timer 停止调用 actionPerformed()