java 如何操作剪切板?例如:在这个java程序中按一个按钮,把文本复制到windows剪切板,然后可以去别的地方粘贴!期待高手解答!!

解决方案 »

  1.   

    根本不用那么麻烦,假设你那个文本框是
    JTextArea textArea=new JTextArea();
    textArea.copy();
    这样就把你文本框内的内容都拷贝到系统剪贴版去了。
      

  2.   

    rehte()   
    的方法我先前试过的。这只是textArea 所支持的一个方法.这样做起来很不灵活,不能对剪切板进行具体的操作。
    而且在textArea edtiable=false 的时候copy() 就失效了我去研究一下 amozon  所说的那个Clipboard 包把。呵呵。
      

  3.   

    如何在Java程序中读写系统剪切板的数据Wednesday, 15. November 2006, 14:15:45Java中使用java.awt.datatransfer.Clipboard类来描述剪切板,并把剪切板分为两种类型:本地和系统,本地剪切板使用 Clipborad cp = new Clipboard("clip1"); 来构造;系统剪切板通过Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();获取,下面我们给出几个常用的方法用于读写剪切板中的文本数据以及图像数据1. 从指定的剪切板中获取文本内容protected static String getClipboardText(Clipboard clip) throws Exception{
    // 获取剪切板中的内容
    Transferable clipT = clip.getContents(null);
    if (clipT != null) {
    // 检查内容是否是文本类型
    if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor))
    return (String)clipT.getTransferData(DataFlavor.stringFlavor);
    }
    return null;
    }2. 往剪切板写文本数据protected static void setClipboardText(Clipboard clip, String writeMe) {
    Transferable tText = new StringSelection(writeMe);
    clip.setContents(tText, null);
    }3. 从剪切板读取图像public static Image getImageFromClipboard() throws Exception{
    Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable cc = sysc.getContents(null);
    if (cc == null)
    return null;
    else if(cc.isDataFlavorSupported(DataFlavor.imageFlavor))
    return (Image)cc.getTransferData(DataFlavor.imageFlavor);
    return null;
    }4. 写图像到剪切板protected static void setClipboardImage2(final Image image) {
    Transferable trans = new Transferable(){
    public DataFlavor[] getTransferDataFlavors() {
    return new DataFlavor[] { DataFlavor.imageFlavor };
    }
    public boolean isDataFlavorSupported(DataFlavor flavor) {
    return DataFlavor.imageFlavor.equals(flavor);
    }
    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
    if(isDataFlavorSupported(flavor))
    return image;
    throw new UnsupportedFlavorException(flavor);
    }};
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null);
    }有了这四个方法,你下面可以自己写一些程序来进行测试,利用它来实现与其他程序结合测试对剪切板数据的操作。这里给出一个用于显示图像的类,只要把Image实例作为参数传入即可。/*
    * Created on 2004-12-23
    * 查看图形的窗口
    */
    package javayou.clipboard;import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;/**
    * @author Winter Lau 用于显示图形的窗口
    */
    public class ImageViewer extends Frame {private Image image;/**
    * 显示一个图像
    * @param viewMe
    */
    public ImageViewer(Image viewMe) {
    image = viewMe;
    MediaTracker mediaTracker = new MediaTracker(this);
    mediaTracker.addImage(image, 0);
    try {
    mediaTracker.waitForID(0);
    } catch (InterruptedException ie) {
    ie.printStackTrace();
    System.exit(1);
    }
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    //窗口适应图像大小
    setSize(image.getWidth(null), image.getHeight(null));
    //窗口标题
    setTitle("Viewing Image from Clipboard");
    setVisible(true);
    }public void paint(Graphics graphics) {
    graphics.drawImage(image, 0, 0, null);
    }/**
    * 用于读取图像文件并生成Image对象
    */
    public static Image getImageFromFile(String fileName) {
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image image = toolkit.getImage(fileName);
    return image;
    }}
      

  4.   

    /**
       @version 1.02 2004-08-24
       @author Cay Horstmann
    */import java.awt.*;
    import java.awt.event.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import java.util.*;
    import javax.swing.*;/**
       This program demonstrates the use of a clip shape.
    */
    public class ClipTest
    {  
       public static void main(String[] args)
       {  
          JFrame frame = new ClipTestFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
       }
    }/**
       This frame contains a check box to turn a clip off
       and on, and a panel to draw a set of lines with or without
       clipping.
    */
    class ClipTestFrame extends JFrame
    {  
       public ClipTestFrame()
       {  
          setTitle("ClipTest");
          setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);      final JCheckBox checkBox = new JCheckBox("Clip");
          checkBox.addActionListener(new
             ActionListener()
             {
                public void actionPerformed(ActionEvent event)
                {
                   panel.repaint();
                }
             });
          add(checkBox, BorderLayout.NORTH);      panel = new 
             JPanel()
             {
                public void paintComponent(Graphics g)
                {  
                   super.paintComponent(g);
                   Graphics2D g2 = (Graphics2D)g;               if (clipShape == null) clipShape = makeClipShape(g2);               g2.draw(clipShape);               if (checkBox.isSelected()) g2.clip(clipShape);
                      
                   // draw line pattern
                   final int NLINES = 50;
                   Point2D p = new Point2D.Double(0, 0);
                   for (int i = 0; i < NLINES; i++)
                   {  
                      double x = (2 * getWidth() * i) / NLINES;
                      double y = (2 * getHeight() * (NLINES - 1 - i)) / NLINES;
                      Point2D q = new Point2D.Double(x, y);
                      g2.draw(new Line2D.Double(p, q));
                   }
                }
             };
          add(panel, BorderLayout.CENTER);
       }   /**
          Makes the clip shape.
          @param g2 the graphics context
          @return the clip shape
       */
       Shape makeClipShape(Graphics2D g2)
       {
          FontRenderContext context = g2.getFontRenderContext();
          Font f = new Font("Serif", Font.PLAIN, 100);
          GeneralPath clipShape = new GeneralPath();
          
          TextLayout layout = new TextLayout("Hello", f, context);
          AffineTransform transform = AffineTransform.getTranslateInstance(0, 100);
          Shape outline = layout.getOutline(transform);
          clipShape.append(outline, false);
          
          layout = new TextLayout("World", f, context);
          transform = AffineTransform.getTranslateInstance(0, 200);
          outline = layout.getOutline(transform);
          clipShape.append(outline, false);
          return clipShape;
       }   private JPanel panel;
       private Shape clipShape;
       private static final int DEFAULT_WIDTH = 300;
       private static final int DEFAULT_HEIGHT = 300;
    }