SWT(JFace)体验之复制粘贴
2019/7/7 19:29:58
本文主要是介绍SWT(JFace)体验之复制粘贴,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
演示代码如下:
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();
}
}
复制代码 代码如下:
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)体验之复制粘贴的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-24怎么修改Kafka的JVM参数?-icode9专业技术文章分享
- 2024-12-23线下车企门店如何实现线上线下融合?
- 2024-12-23鸿蒙Next ArkTS编程规范总结
- 2024-12-23物流团队冬至高效运转,哪款办公软件可助力风险评估?
- 2024-12-23优化库存,提升效率:医药企业如何借助看板软件实现仓库智能化
- 2024-12-23项目管理零负担!轻量化看板工具如何助力团队协作
- 2024-12-23电商活动复盘,为何是团队成长的核心环节?
- 2024-12-23鸿蒙Next ArkTS高性能编程实战
- 2024-12-23数据驱动:电商复盘从基础到进阶!
- 2024-12-23从数据到客户:跨境电商如何通过销售跟踪工具提升营销精准度?