SWT(JFace)体验之复制粘贴
SWT(JFace)体验之复制粘贴
发布时间:2016-12-28 来源:查字典编辑
摘要:演示代码如下:复制代码代码如下:packageswt_jface.demo11;importorg.eclipse.swt.SWT;impo...

演示代码如下:

复制代码 代码如下:

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 = "{rtf1b 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学习
热门Java学习
编程开发子分类