【问题标题】:How to copy an image in JTextPane java?如何在 JTextPane java 中复制图像?
【发布时间】:2011-09-06 16:51:51
【问题描述】:

我想知道如何在 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);
        }

    }
 }

}

【问题讨论】:

    标签: java image swing jtextarea jfilechooser


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      不幸的是,没有办法做到这一点。出于某种原因,该字段被称为 JTextPane。它无法处理图像。

      【讨论】:

        【解决方案3】:

        我认为有一种方法:如果你想在 jtextpane 中打开一个 rtf 文档,如果你想加载它,就使用 FileInputStream 和 FileOutputStream。因为它是一个字节流,它会尝试一个字节一个字节地加载它。

        但几乎没有任何方法可以复制它。

        【讨论】:

          猜你喜欢
          • 2012-04-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-25
          • 1970-01-01
          相关资源
          最近更新 更多