图像不会留在Java中的JTextPane中

时间:2014-03-19 16:56:12

标签: java swing clipboard jtextpane drag-and-drop

我一直在尝试将图片(图纸)从剪贴板传输到JTextPane或JTextArea。 附加的代码正确显示这些窗口中的图像,但是一旦我将光标从窗口移开,图像就会消失。如何修复JText中的图像......?我的代码中明显缺少某些东西。我的网络搜索没有产生任何有用的东西。我对C#没有这个问题。我非常感谢你的帮助。

    private void jTextPaneStructuresMouseClicked(java.awt.event.MouseEvent evt)     {                                                 

    TransferImage.TransferableImage newImage = new TransferImage.TransferableImage(image);

        transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents( null );

        if ( transferable != null && transferable.isDataFlavorSupported( DataFlavor.imageFlavor ) ) {
            try {
                image = (Image)transferable.getTransferData( DataFlavor.imageFlavor );
                Image cpImage = newImage.getTransferData(DataFlavor.imageFlavor);
                jTextPaneStructures.getGraphics().drawImage(cpImage, 0, 0, rootPane);
            } catch (    UnsupportedFlavorException | IOException ex) {
                Logger.getLogger(WeeklyData.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }     



    /* This is what TransferImage.TransferableImage refers to */

    public class TransferImage 
{ 
  static class TransferableImage implements Transferable
  {
      /* -- image for transfer */
        private final Image image;

      /* -- constructor */

      public TransferableImage(Image image)
      {
      this.image = image;
      {
   }
      }
    @Override
   public DataFlavor[] getTransferDataFlavors()
         {
            return new DataFlavor[]
            { 
                DataFlavor.imageFlavor
            };
         }

    @Override
   public boolean isDataFlavorSupported( DataFlavor flavor )
        {
            return flavor == DataFlavor.imageFlavor;
        }

    @Override
   public Image getTransferData( DataFlavor flavor ) throws UnsupportedFlavorException {
        if ( isDataFlavorSupported( flavor ) )
        {
             return image;
        } 
        else
        {
             throw new UnsupportedFlavorException( flavor );
        }
      }
   }          

1 个答案:

答案 0 :(得分:1)

如果在侦听器中执行getGraphics()。drawImage,则会在JTextArea上绘制图像,但只要系统重新绘制JTextArea,您的图像就会消失,因为它没有存储在JTextArea中。

我认为你应该采用不同的方法:

  • 编写一个扩展JTextArea(MyTextArea)
  • 的新类
  • 包含用于存储图像的私有属性(myImage)
  • 提供设置图像的set方法(setImage)
  • 覆盖paintComponent方法,以便:
    • 它调用super.paintComponent
    • 然后执行graphics.drawImage(myImage,0,0,rootPane)//你可能需要修复一些关于rootPane的内容

然后,在您的代码中:

  • jTextPaneStructures必须是新类的实例(MyTextArea)
  • 在你的监听器中(你甚至可能想在MyTextArea中提供一个执行这三个任务的方法):
    • jTextPaneStructures.setImage(cpImage);
    • jTextPaneStructures.invaliate();
    • jTextPaneStructures.repaint();

示例:

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class MyTextArea extends JTextArea implements MouseListener {

   private Image myImage;

   public MyTextArea() {
      addMouseListener(this);
   }

   public void addImage(Image image) {
      this.myImage = image;
      invalidate();
      repaint();
   }

   @Override
   public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.drawImage(myImage, 0, 0, this);
   }

   public static void main(String[] args) {
      JFrame f = new JFrame("Test");
      MyTextArea txt = new MyTextArea();
      f.getContentPane().add(txt);
      f.getContentPane().add(new JButton("Just another component to click on"), BorderLayout.SOUTH);
      f.pack();
      f.setVisible(true);

   }

   @Override
   public void mouseClicked(MouseEvent e) {
      ImageIcon img = new ImageIcon("twitter.png"); //place a png image in your working directory for testing
      addImage(img.getImage());

   }

   @Override
   public void mousePressed(MouseEvent e) {
      // TODO Auto-generated method stub

   }

   @Override
   public void mouseReleased(MouseEvent e) {
      // TODO Auto-generated method stub

   }

   @Override
   public void mouseEntered(MouseEvent e) {
      // TODO Auto-generated method stub

   }

   @Override
   public void mouseExited(MouseEvent e) {
      // TODO Auto-generated method stub

   }

}