<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    常用鏈接

    統(tǒng)計(jì)

    最新評論

    在JAVA中使用拖拽功能(2轉(zhuǎn))

    在JAVA中使用拖拽功能
    sun在java2中引入了一些新的方法來幫助實(shí)現(xiàn)拖拽功能,這些新的類在java.awt.dnd包中
    實(shí)現(xiàn)一個(gè)D&D操作一般包括三個(gè)步驟:
     首先實(shí)現(xiàn)一個(gè)拖拽源,這個(gè)拖拽源和相應(yīng)的組件是關(guān)聯(lián)起來的
     第二步實(shí)現(xiàn)一個(gè)拖拽目標(biāo),這個(gè)目標(biāo)用來實(shí)現(xiàn)拖拽物的接收
     第三步實(shí)現(xiàn)一個(gè)數(shù)據(jù)傳輸對象,該對象封裝拖動(dòng)的數(shù)據(jù)
      _____________________                                      _____________________
     |                     |                                     |                    |   
     | DragSource Component|                                     |DropTarget Component|
     |_____________________|                                     |____________________|
                       |                                              |
                       |____________Transferable Data_________________|
                       
     Transferable 接口實(shí)現(xiàn)出的對象能夠保證  DropTarget Component讀懂拖拽過來的對象中包含的信息
     如果是在同一個(gè)虛擬機(jī)中實(shí)現(xiàn)拖拽的話,DragSource Component會傳遞一個(gè)引用給DropTarget Component
     但是如果在不同的JVM中或者是在JVM和本地系統(tǒng)之間傳遞數(shù)據(jù)的話我們就必須實(shí)現(xiàn)一個(gè)Transferable對象來傳遞數(shù)據(jù)
     Transferable中封裝的內(nèi)容存放到DataFlavors,用戶可以通過訪問DataFlavors來獲取數(shù)據(jù)
     1。創(chuàng)建可拖拽對象
       一個(gè)對象那個(gè)如果想作為拖拽源的話,必須和五個(gè)對象建立練習(xí),這五個(gè)對象分別是:
        * java.awt.dnd.DragSource
        獲取DragSource的方法很簡單,直接調(diào)用DragSource.getDefaultDragSource();就可以得到DragSource對象
        * java.awt.dnd.DragGestureRecognizer
        DragGestureRecognizer類中實(shí)現(xiàn)了一些與平臺無關(guān)的方法,我們?nèi)绻朐谧约旱慕M件上實(shí)現(xiàn)拖拽的話只要調(diào)用createDefaultDragGestureRecognizer()方法就可以了
        該方法接收三個(gè)參數(shù),建立組件和拖拽動(dòng)作之間的關(guān)系
        * java.awt.dnd.DragGestureListener
        當(dāng)建立了組件和拖拽動(dòng)作之間的聯(lián)系后,如果用戶執(zhí)行了拖拽操作,組件將發(fā)送一個(gè)消息給DragGestureListener監(jiān)聽器
        DragGestureListener監(jiān)聽器接下來會發(fā)送一個(gè)startDrag()消息給拖拽源對象,告訴組件應(yīng)該執(zhí)行拖拽的初始化操作了
        拖拽源會產(chǎn)生一個(gè)DragSourceContext對象來監(jiān)聽動(dòng)作的狀態(tài),這個(gè)監(jiān)聽過程是通過監(jiān)聽本地方法DragSourceContextPeer來實(shí)現(xiàn)的
        * java.awt.datatransfer.Transferable
       
        * java.awt.dnd.DragSourceListener
        DragSourceListener接口負(fù)責(zé)當(dāng)鼠標(biāo)拖拽對象經(jīng)過組件時(shí)的可視化處理, DragSourceListener接口的顯示結(jié)果只是暫時(shí)改變組件的外觀
        同時(shí)他提供一個(gè)feedback,當(dāng)用戶的拖拽操作完成之后會收到一個(gè)dragDropEnd的消息,我們可以在這個(gè)函數(shù)中執(zhí)行相應(yīng)的操作
        再來回顧一下拖拽源的建立過程
        首先、 DragGestureRecognizer 確認(rèn)一個(gè)拖拽操作,同時(shí)告知 DragGestureListener.

        其次、 Assuming the actions and/or flavors are OK, DragGestureListener asks DragSource to startDrag().

        第三、 DragSource creates a DragSourceContext and a DragSourceContextPeer. The DragSourceContext adds itself as a DragSourceListener to the DragSourceContextPeer.

        第四、 DragSourceContextPeer receives state notifications (component entered/exited/is over) from the native system and delegates them to the DragSourceContext.

        第五、 The DragSourceContext notifies the DragSourceListener, which provides drag over feedback (if the DropTargetListener accepts the action). Typical feedback includes asking the DragSourceContext to change the cursor.

        最后、 When the drop is complete, the DragSourceListener receives a dragDropEnd notification message

    2。創(chuàng)建droppable Component
       創(chuàng)建一個(gè) droppable Component必須和下面兩個(gè)對象發(fā)生關(guān)聯(lián)  
        * java.awt.dnd.DropTarget
        DropTarget構(gòu)造函數(shù)使DropTarget 和 DropTargetListener objects發(fā)生關(guān)聯(lián)
        Droptarget對象提供 setComponent 和addDropTargetListener 兩個(gè)方法
       
        * java.awt.dnd.DropTargetListener
        The DropTargetListener needs an association with the Component so that the Component can notify the DropTargetListener to display "drag under" effects during the operation. This listener, which can be conveniently created as an inner class, transfers the data when the drop occurs. Warning: The Component itself shouldn't be the listener, since this implies its availability for use as some other Component's listener.
     




    下面的例子演示了一個(gè)從樹中拖拽一個(gè)節(jié)點(diǎn)到文本域中

    package appletandservlet;

    import java.awt.*;

    import javax.swing.*;
    import com.borland.jbcl.layout.XYLayout;
    import com.borland.jbcl.layout.*;
    import java.awt.dnd.*;
    import java.awt.datatransfer.*;
    import java.io.*;
    import javax.swing.tree.*;

    public class DragAndDrop extends JFrame {

        XYLayout xYLayout1 = new XYLayout();
        JScrollPane jScrollPane1 = new JScrollPane();
        JTextArea jTextArea1 = new JTextArea();

        public DragAndDrop() {
            try {
                jbInit();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            getContentPane().setLayout(xYLayout1);


            jScrollPane1.getViewport().setBackground(new Color(105, 38, 125));
            jTextArea1.setBackground(Color.orange);
            jTextArea1.setToolTipText("");
            JTree jtr = new JTree();
            jtr.setBackground(Color.BLUE);
            jScrollPane1.getViewport().add(jtr);
            this.getContentPane().add(jTextArea1,
                                      new XYConstraints(238, 42, 140, 248));
            this.getContentPane().add(jScrollPane1,
                                      new XYConstraints(16, 42, 217, 249));

            DragSource dragSource = DragSource.getDefaultDragSource(); //創(chuàng)建拖拽源
            dragSource.createDefaultDragGestureRecognizer(jtr,
                    DnDConstants.ACTION_COPY_OR_MOVE,
                    new DragAndDropDragGestureListener()); //建立拖拽源和事件的聯(lián)系
          DropTarget dropTarget = new DropTarget(jTextArea1,
                new DragAndDropDropTargetListener());

        }

        private void jbInit() throws Exception {

        }

        public static void main(String[] args) {

            DragAndDrop dad = new DragAndDrop();
            dad.setTitle("拖拽演示");
            dad.setSize(400, 300);
            dad.setVisible(true);

        }
    }


    class DragAndDropDragGestureListener implements DragGestureListener {
        public void dragGestureRecognized(DragGestureEvent dge) {
            //將數(shù)據(jù)存儲到Transferable中,然后通知組件開始調(diào)用startDrag()初始化
            JTree tree = (JTree) dge.getComponent();
          TreePath path = tree.getSelectionPath();
           if(path!=null){
               DefaultMutableTreeNode selection = (DefaultMutableTreeNode) path
                .getLastPathComponent();
               DragAndDropTransferable dragAndDropTransferable = new
                       DragAndDropTransferable(selection);
               dge.startDrag(DragSource.DefaultCopyDrop, dragAndDropTransferable, new DragAndDropDragSourceListener());
           }
        }


    }


    class DragAndDropTransferable implements Transferable {
        private DefaultMutableTreeNode treeNode;
        DragAndDropTransferable(DefaultMutableTreeNode treeNode) {
            this.treeNode = treeNode;
        }
        static DataFlavor flavors[] = {DataFlavor.stringFlavor};
        public DataFlavor[] getTransferDataFlavors() {
            return flavors;
        }

        public boolean isDataFlavorSupported(DataFlavor flavor) {
            if(treeNode.getChildCount()==0){
             return true;
            }
            return false;
        }

        public Object getTransferData(DataFlavor flavor) throws
                UnsupportedFlavorException, IOException {

          return treeNode;
       
        }


    }


     class DragAndDropDragSourceListener implements DragSourceListener {
      public void dragDropEnd(DragSourceDropEvent dragSourceDropEvent) {
        if (dragSourceDropEvent.getDropSuccess()) {
            //拖拽動(dòng)作結(jié)束的時(shí)候打印出移動(dòng)節(jié)點(diǎn)的字符串
          int dropAction = dragSourceDropEvent.getDropAction();
          if (dropAction == DnDConstants.ACTION_MOVE) {
            System.out.println("MOVE: remove node");
          }
        }
      }

      public void dragEnter(DragSourceDragEvent dragSourceDragEvent) {
        DragSourceContext context = dragSourceDragEvent
            .getDragSourceContext();
        int dropAction = dragSourceDragEvent.getDropAction();
        if ((dropAction & DnDConstants.ACTION_COPY) != 0) {
          context.setCursor(DragSource.DefaultCopyDrop);
        } else if ((dropAction & DnDConstants.ACTION_MOVE) != 0) {
          context.setCursor(DragSource.DefaultMoveDrop);
        } else {
          context.setCursor(DragSource.DefaultCopyNoDrop);
        }
      }

      public void dragExit(DragSourceEvent dragSourceEvent) {
      }

      public void dragOver(DragSourceDragEvent dragSourceDragEvent) {
      }

      public void dropActionChanged(DragSourceDragEvent dragSourceDragEvent) {
      }
    }
    class DragAndDropDropTargetListener implements DropTargetListener{
        public void dragEnter(DropTargetDragEvent dtde){
       
        }
        public void dragOver(DropTargetDragEvent dtde){
        }
        public void dropActionChanged(DropTargetDragEvent dtde){
        }
        public void dragExit(DropTargetEvent dte){
        }
        public void drop(DropTargetDropEvent dtde){
           Transferable tr=dtde.getTransferable();//使用該函數(shù)從Transferable對象中獲取有用的數(shù)據(jù)
           String s="";
            try {
                if(tr.isDataFlavorSupported(DataFlavor.stringFlavor)){
                    s = tr.getTransferData(DataFlavor.stringFlavor).toString();
                }
            } catch (IOException ex) {
            } catch (UnsupportedFlavorException ex) {
            }
            System.out.println(s);
            DropTarget c=(DropTarget)dtde.getSource();
            JTextArea d=(JTextArea)c.getComponent();
            if(s!=null&&s!=""){
                d.append(s + "\n");
            }
        }


    }

    posted on 2008-01-10 14:16 九寶 閱讀(2479) 評論(2)  編輯  收藏 所屬分類: Java

    評論

    # re: 在JAVA中使用拖拽功能(2轉(zhuǎn)) 2009-06-03 00:48 陳夏飛

    你好,請問如果想在JAVA中實(shí)現(xiàn)把文字拖入jpane該如何實(shí)現(xiàn),而且在這個(gè)Jpane里還有一個(gè)繪制有3d圖形的Canvas3d.
    我的郵箱:cxf.efrei@gamil.com
    這個(gè)問題困擾我很久,望高手能幫助我下,不勝感激。  回復(fù)  更多評論   

    # re: 在JAVA中使用拖拽功能(2轉(zhuǎn))[未登錄] 2010-10-15 10:45 jay

    2010最新日賺300元的項(xiàng)目|只需要每天掛機(jī)10小時(shí)就可以賺到300元|完全0投資,0風(fēng)險(xiǎn)。詳情查看:http://www.31do.info/?203047.htm  回復(fù)  更多評論   

    主站蜘蛛池模板: 最新亚洲卡一卡二卡三新区| 特级毛片全部免费播放| 性色av极品无码专区亚洲| 国产精彩免费视频| 亚洲日韩VA无码中文字幕| 亚洲av成人一区二区三区观看在线 | 亚洲女久久久噜噜噜熟女| 色多多www视频在线观看免费| 国产hs免费高清在线观看| 亚洲日本一线产区和二线| 女人18毛片水最多免费观看 | 久久免费的精品国产V∧| 国产V亚洲V天堂无码久久久| 国产精品偷伦视频观看免费| 亚洲午夜久久久久久噜噜噜| a色毛片免费视频| 亚洲高清日韩精品第一区| 国产黄色免费网站| 免费一级特黄特色大片| 黑人精品videos亚洲人| 4399影视免费观看高清直播| 亚洲私人无码综合久久网| 亚洲国产一成久久精品国产成人综合| 又大又硬又粗又黄的视频免费看 | 中文字幕免费在线视频| 亚洲专区先锋影音| 国产精品成人无码免费| 一个人免费观看视频在线中文 | 亚洲精品无码专区在线在线播放| 99久久精品日本一区二区免费 | 亚洲色图在线播放| 国产一区二区三区在线免费观看 | v片免费在线观看| 亚洲午夜一区二区电影院| 亚洲日本va午夜中文字幕久久| 69式国产真人免费视频 | 久久av免费天堂小草播放| 亚洲一本一道一区二区三区| 中文字幕亚洲不卡在线亚瑟| 1000部国产成人免费视频| 你懂的网址免费国产|