簡體   English   中英

如何在 JTextPane java 中復制圖像?

[英]How to copy an image in JTextPane java?

我想知道如何在 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。

您可以實現自己的 Reader/Writer。 請參閱http://java-sl.com/editor_kit_tutorial.html關於讀寫器的章節。

不幸的是,沒有辦法做到這一點。 出於某種原因,該字段被稱為 JTextPane。 它無法處理圖像。

我認為有一種方法:如果你想在 jtextpane 中打開一個 rtf 文檔,如果你想加載它,請使用 FileInputStream 和 FileOutputStream。因為它是一個字節的 stream,它會嘗試逐字節加載它。

但是幾乎沒有任何方法可以復制它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM