如何在 JTextPane java 中复制图像?

发布于 2024-11-13 08:29:39 字数 2540 浏览 2 评论 0原文

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

    }
 }

}

I want to know how to copy an image and text in JTextPane.
When I use this code, it copies just text but I want to copy text and image. How can be done this?

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

    }
 }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白馒头 2024-11-20 08:29:39

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

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

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

I'm afraid there is no simple way to do this.
All the default EditorKits (StyledEditorKit, HTMLEditorKit, RTFEditorKit) don't support images copying.

The closest one is HTMLEditorKit but it will generate HTML with links to images.

You can implement your own Reader/Writer. See http://java-sl.com/editor_kit_tutorial.html chapter about reader and writer.

猫九 2024-11-20 08:29:39

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

Unfortunately there is no way to do this. The field is called a JTextPane for a reason. It cannot handle images.

微暖i 2024-11-20 08:29:39

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

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

I think there is a way: use FileInputStream and FileOutputStream if you want to open an rtf document in jtextpane if you want to load it.Since it is a stream of byte it will try to load it byte by byte.

But there is hardly any way to copy it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文