SWT(JFace)體驗(yàn)之復(fù)制粘貼
更新時間:2009年06月25日 12:16:18 作者:
SWT(JFace)體驗(yàn)之復(fù)制粘貼
演示代碼如下:
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();
}
}
復(fù)制代碼 代碼如下:
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();
}
}
您可能感興趣的文章:
- JS插件clipboard.js實(shí)現(xiàn)一鍵復(fù)制粘貼功能
- VirtualBox安裝CentOS實(shí)現(xiàn)鼠標(biāo)自動切換和復(fù)制粘貼功能
- html5+CSS 實(shí)現(xiàn)禁止IOS長按復(fù)制粘貼功能
- Clipboard.js 無需Flash的JavaScript復(fù)制粘貼庫
- javascript復(fù)制粘貼與clipboardData的使用
- JS+flash實(shí)現(xiàn)chrome和ie瀏覽器下同時可以復(fù)制粘貼
- C++小知識:C/C++中不要按值傳遞數(shù)組
- C++小知識:不要節(jié)約代碼行數(shù)
- SVN報錯:Error Updating changes:svn:E155037的解決方案
- C++小知識:復(fù)制粘貼代碼千萬要小心
相關(guān)文章
spring基礎(chǔ)概念A(yù)OP與動態(tài)代理理解
這篇文章主要為大家詳細(xì)介紹了spring基礎(chǔ)概念A(yù)OP與動態(tài)代理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
Mybatis-plus 雙主鍵的實(shí)現(xiàn)示例
本文主要介紹了Mybatis-plus 雙主鍵的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
Springboot處理跨域的實(shí)現(xiàn)方式(附Demo)
這篇文章主要介紹了Springboot處理跨域的實(shí)現(xiàn)方式(附Demo),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
Java獲取項(xiàng)目路徑方式System.getProperty(“user.dir“)
這篇文章主要介紹了Java獲取項(xiàng)目路徑方式System.getProperty(“user.dir“),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
一文詳解如何在SpringMVC的視圖中渲染模型數(shù)據(jù)
SpringMVC是一個基于Spring框架的Web框架,它提供了一種方便的方式來處理 HTTP 請求和響應(yīng),在SpringMVC中,視圖是用來渲染模型數(shù)據(jù)的組件,它們負(fù)責(zé)將模型數(shù)據(jù)轉(zhuǎn)換為HTML、JSON、XML等格式的響應(yīng),在本文中,我們將討論如何在SpringMVC中的視圖中渲染模型數(shù)據(jù)2023-07-07
如何使用Spring-Test對Spring框架進(jìn)行單元測試
這篇文章主要介紹了如何使用Spring-Test對Spring框架進(jìn)行單元測試,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09

