欢迎来到入门教程网!

Java编程

当前位置:主页 > 软件编程 > Java编程 >

SWT(JFace)体验之复制粘贴

来源:本站原创|时间:2020-01-10|栏目:Java编程|点击:

演示代码如下:

复制代码 代码如下:

package swt_jface.demo11;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.RTFTransfer;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class CopyPaste {

    Display display = new Display();
    Shell shell = new Shell(display);
    public CopyPaste() {
        shell.setLayout(new GridLayout());

        ToolBar toolBar = new ToolBar(shell, SWT.FLAT);
        ToolItem itemCopy = new ToolItem(toolBar, SWT.PUSH);
        ToolItem itemPaste = new ToolItem(toolBar, SWT.PUSH);
        itemCopy.setText("Copy");
        itemPaste.setText("Paste");

        itemCopy.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                Clipboard clipboard = new Clipboard(display);
                String plainText = "Hello World";
                String rtfText = "{\\rtf1\\b Hello World}";
                TextTransfer textTransfer = TextTransfer.getInstance();
                RTFTransfer rftTransfer = RTFTransfer.getInstance();
                clipboard.setContents(new String[]{plainText, rtfText}, new Transfer[]{textTransfer, rftTransfer});
                clipboard.dispose();
            }
        });

        itemPaste.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                Clipboard clipboard = new Clipboard(display);
                TransferData[] transferDatas = clipboard.getAvailableTypes();
                for(int i=0; i<transferDatas.length; i++) {
                    if(RTFTransfer.getInstance().isSupportedType(transferDatas[i])) {
                        System.out.println("Data is available in RTF format");
                        break;
                    }
                }
                String plainText = (String)clipboard.getContents(TextTransfer.getInstance());
                String rtfText = (String)clipboard.getContents(RTFTransfer.getInstance());
                System.out.println("PLAIN: " + plainText + "\n" + "RTF: " + rtfText);
                clipboard.dispose();
            }
        });
        shell.pack();
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
    public static void main(String[] args) {
        new CopyPaste();
    }
}

上一篇:Java中的functor实现

栏    目:Java编程

下一篇:SWT(JFace)体验之ApplicationWindow

本文标题:SWT(JFace)体验之复制粘贴

本文地址:https://www.xiuzhanwang.com/a1/Javabiancheng/8526.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有