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");
    }
}

(0)

相关推荐

  • Java拖曳鼠标实现画线功能的方法

    本文实例主要实现Java拖曳鼠标画线的功能,为了达到画线的功能,分别用implements MouseListener与MouseMotionListener,并且由mousePressed(),mouseReleased()取得鼠标拖曳的开始与结束坐标.这是一个掌握Java鼠标事件的很好的范例. 具体实现代码如下: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MouseDemo e

  • Sortable.js拖拽排序使用方法解析

    最近公司项目经常用到一个拖拽 Sortable.js插件,所以有空的时候看了 Sortable.js 源码,总共1300多行这样,写的挺完美的. 官网: http://rubaxa.github.io/Sortable/ 拖拽的时候主要由这几个事件完成, ondragstart 事件:当拖拽元素开始被拖拽的时候触发的事件,此事件作用在被拖曳元素上     ondragenter 事件:当拖曳元素进入目标元素的时候触发的事件,此事件作用在目标元素上     ondragover 事件:拖拽元素在目

  • Jquery 快速构建可拖曳的购物车DragDrop

    这样一来,购买者只需要把自己感兴趣的商品拖曳到自己的购物车中,也可以从购物车中删除商品 同时更新购物车的总体价格和数量. 那咱们就开始实例吧,本实例并没有链接数据库读取数据来初始化Products,而是创建了一些虚拟的商品如下: 1. 创建Product实体类 复制代码 代码如下: public class Product { public string Code { get; set; } public string Name { get; set; } public string Descr

  • 基于JavaScript实现右键菜单和拖拽功能

    下面先给大家介绍下js实现的右键菜单功能,具体详情如下所示: 这一章解决的问题 1.实现右键菜单功能代码. 2.阻止默认事件的实际应用. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>右键菜单</title> <style type="text/css"> #menu {

  • js拖拽功能实现代码解析

    本文解决的问题: 1.怎样在网页中实现拖曳功能: 2.document.documentElement与document.body的区别. document.documentElement.clientWidth指整个html文档的宽度,document.body.clientWidth的宽度.这两者是不一样的.可以在console控制台通过console.log(document.documentElement)和console.log(document.body)进行测试. 3.getBou

  • jquery 关键字“拖曳搜索”之“拖曳”以及 图片“提示自适应放大”效果 的实现

    关键字"拖曳搜索"之"拖曳"功能需要 jQuery UI 之 droppable 库 效果如下: 搜索包含相关关键字时,把拖曳左边关键字到右边框里面 也可以在输入框里输入自定义关键字到下面框 即可搜索 如果去掉不需要的关键词 搜索时 把不需要的关键词从右边框拖曳回到左边 即可 无论原来还是自定义加入的关键词 如果已存在 她会提示... 实现: 复制代码 代码如下: <script type="text/javascript" src=&qu

  • 仿3721首页模块拖曳移动效果js代码[可拖曳层移动层]

    仿3721首页模块拖曳移动效果[拖曳层移动层]呵,做了一些细致的处理 html, body { width:100%; height:100%; padding:0px; margin:0px; } body { text-align:center; font-size:14px; } .cell_left, .cell_right { width:202px; } .cell_center { width:404px; } .left { float:left; } .row { clear:

  • JS实现可展开折叠层的鼠标拖曳效果

    本文实例讲述了JS实现可展开折叠层的鼠标拖曳效果.分享给大家供大家参考.具体如下: 这是一款简单JS代码实现的鼠标拖曳图层效果,比较精简,大家参考一下.鼠标点击层标题栏可实现层内容的展开与折叠,按住标题栏可实现层的拖动. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/js-fade-out-mouse-draw-style-demo/ 具体代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01

  • js点击弹出div层实现可拖曳的弹窗效果

    弹出层.弹窗效果+拖曳功能 *{ margin:0px; padding:0px;} body{ font-size:12px; font:Arial, Helvetica, sans-serif; margin:25PX 0PX; background:#eee;} .botton{ color:#F00; cursor:pointer;} .mybody{width:600px; margin:0 auto; height:1500px; border:1px solid #ccc; pad

  • JS实现可拖曳、可关闭的弹窗效果

    本文实例讲述了JS实现可拖曳.可关闭的弹窗效果.分享给大家供大家参考.具体如下: 运行该实例,点击文字,弹出一个窗口,其实是一个弹出层,这个弹出层可以随鼠标拖曳,另外,示例演示了用本方法弹出文字层和弹出图片层的具体代码,请根据选择使用哦. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/js-draw-close-able-alert-dlg-demo/ 具体代码如下: <!DOCTYPE HTML> <html> <head

随机推荐