扣丁学堂Java培训

Java在线学习之SWT(JFace)体验之复制粘贴

2019-01-17  本文已影响0人  994d14631d16

今天小编给大家分享的是扣丁学堂Java在线学习之SWT(JFace)体验之复制粘贴,别的不多说了,下面和扣丁学堂小编一起来看一下SWT(JFace)体验之复制粘贴吧。

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

    }

}

以上就是小编给大家分享的SWT(JFace)体验之复制粘贴,希望对小伙伴们有所帮助,想要了解更多内容的小伙伴可以登录扣丁学堂官网咨询。扣丁学堂是专业的Java培训机构,不仅有专业的老师和与时俱进的课程体系,还有大量的Java在线教程供学员观看学习,心动的小伙伴快快行动吧。

上一篇下一篇

猜你喜欢

热点阅读