SWT JFace 拖曳效果
演示代碼如下:
package swt_jface.demo11;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
public class WordJumbles {
Display display = new Display();
Shell shell = new Shell(display);
String word;
Label[] labelsRowOne;
Label[] labelsRowTwo;
Font font = new Font(display, "Arial", 18, SWT.BOLD);
public void setDragSource(final Label label) {
int operations = DND.DROP_MOVE;
final DragSource dragSource = new DragSource(label, operations);
Transfer[] formats = new Transfer[] { TextTransfer.getInstance()};
dragSource.setTransfer(formats);
dragSource.addDragListener(new DragSourceListener() {
public void dragStart(DragSourceEvent event) {
if (label.getText().length() == 0)
event.doit = false;
}
public void dragSetData(DragSourceEvent event) {
if (TextTransfer.getInstance().isSupportedType(event.dataType))
event.data = label.getText();
}
public void dragFinished(DragSourceEvent event) {
if (event.doit == true || event.detail == DND.DROP_MOVE) {
label.setText("");
}
}
});
label.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
dragSource.dispose();
}
});
}
public void setDropTarget(final Label label) {
int operations = DND.DROP_MOVE;
final DropTarget dropTarget = new DropTarget(label, operations);
Transfer[] formats = new Transfer[] { TextTransfer.getInstance()};
dropTarget.setTransfer(formats);
dropTarget.addDropListener(new DropTargetListener() {
public void dragEnter(DropTargetEvent event) {
if(label.getText().length() != 0)
event.detail = DND.DROP_NONE;
}
public void dragLeave(DropTargetEvent event) {
}
public void dragOperationChanged(DropTargetEvent event) {
}
public void dragOver(DropTargetEvent event) {
}
public void drop(DropTargetEvent event) {
if (TextTransfer
.getInstance()
.isSupportedType(event.currentDataType)) {
String text = (String) event.data;
label.setText(text);
check();
}
}
public void dropAccept(DropTargetEvent event) {
}
});
label.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
dropTarget.dispose();
}
});
}
private void check() {
for(int i=0; i<word.length(); i++) {
if(! labelsRowTwo[i].getText().equals(word.charAt(i) + ""))
return;
}
MessageBox messageBox = new MessageBox(shell);
messageBox.setMessage("Success!");
messageBox.open();
}
public WordJumbles(String word) {
this.word = word;
shell.setText("Word Jumbles");
labelsRowOne = new Label[word.length()];
labelsRowTwo = new Label[word.length()];
int width = 40;
int[] randomPermutation = { 5, 2, 6, 3, 1, 4, 0 };
for (int i = 0; i < word.length(); i++) {
final Label labelRowOne = new Label(shell, SWT.BORDER);
labelsRowOne[i] = labelRowOne;
labelRowOne.setBounds(10 + width * i, 10, width - 5, width - 5);
labelRowOne.setFont(font);
labelRowOne.setText(word.charAt(randomPermutation[i]) + "");
labelRowOne.setAlignment(SWT.CENTER);
setDragSource(labelRowOne);
setDropTarget(labelRowOne);
final Label labelRowTwo = new Label(shell, SWT.BORDER);
labelsRowTwo[i] = labelRowTwo;
labelRowTwo.setBounds(
10 + width * i,
20 + width,
width - 5,
width - 5);
labelRowTwo.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
labelRowTwo.setFont(font);
labelRowTwo.setAlignment(SWT.CENTER);
setDragSource(labelRowTwo);
setDropTarget(labelRowTwo);
}
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new WordJumbles("ECLIPSE");
}
}
上一篇:跟我學(xué)Java Swing之游戲設(shè)計(jì)(1)
欄 目:Java編程
本文標(biāo)題:SWT JFace 拖曳效果
本文地址:http://mengdiqiu.com.cn/a1/Javabiancheng/8529.html
您可能感興趣的文章
- 01-10SWT(JFace)體驗(yàn)之FormLayout布局
- 01-10SWT(JFace)體驗(yàn)之RowLayout布局
- 01-10SWT JFace Bookmark 制作
- 01-10SWT(JFace)小制作 BugTracker
- 01-10SWT(JFace)體驗(yàn)之StyledText類
- 01-10SWT(JFace)體驗(yàn)之GridLayout布局
- 01-10SWT(JFace)體驗(yàn)之ApplicationWindow
- 01-10SWT(JFace)體驗(yàn)之復(fù)制粘貼
- 01-10SWT(JFace)體驗(yàn)之打開多個(gè)Form
- 01-10SWT(JFace)體驗(yàn)之Sash(活動(dòng)控件)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Java咖啡館(1)——嘆咖啡
- 01-10JVM的垃圾回收機(jī)制詳解和調(diào)優(yōu)
- 01-10Java Socket編程(三) 服務(wù)器Sockets
- 01-10Java進(jìn)階:Struts多模塊的技巧
- 01-10J2SE 1.5版本的新特性一覽
- 01-10Java Socket編程(一) Socket傳輸模式
- 01-10Java運(yùn)行時(shí)多態(tài)性的實(shí)現(xiàn)
- 01-10Java Socket編程(二) Java面向連接的類
- 01-10Java Socket編程(四) 重復(fù)和并發(fā)服務(wù)
- 01-10Java經(jīng)驗(yàn)點(diǎn)滴:處理沒(méi)有被捕獲的異常
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改