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

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

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

    常用鏈接

    統計

    最新評論

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

    在JAVA中使用拖拽功能
    sun在java2中引入了一些新的方法來幫助實現拖拽功能,這些新的類在java.awt.dnd包中
    實現一個D&D操作一般包括三個步驟:
     首先實現一個拖拽源,這個拖拽源和相應的組件是關聯起來的
     第二步實現一個拖拽目標,這個目標用來實現拖拽物的接收
     第三步實現一個數據傳輸對象,該對象封裝拖動的數據
      _____________________                                      _____________________
     |                     |                                     |                    |   
     | DragSource Component|                                     |DropTarget Component|
     |_____________________|                                     |____________________|
                       |                                              |
                       |____________Transferable Data_________________|
                       
     Transferable 接口實現出的對象能夠保證  DropTarget Component讀懂拖拽過來的對象中包含的信息
     如果是在同一個虛擬機中實現拖拽的話,DragSource Component會傳遞一個引用給DropTarget Component
     但是如果在不同的JVM中或者是在JVM和本地系統之間傳遞數據的話我們就必須實現一個Transferable對象來傳遞數據
     Transferable中封裝的內容存放到DataFlavors,用戶可以通過訪問DataFlavors來獲取數據
     1。創建可拖拽對象
       一個對象那個如果想作為拖拽源的話,必須和五個對象建立練習,這五個對象分別是:
        * java.awt.dnd.DragSource
        獲取DragSource的方法很簡單,直接調用DragSource.getDefaultDragSource();就可以得到DragSource對象
        * java.awt.dnd.DragGestureRecognizer
        DragGestureRecognizer類中實現了一些與平臺無關的方法,我們如果想在自己的組件上實現拖拽的話只要調用createDefaultDragGestureRecognizer()方法就可以了
        該方法接收三個參數,建立組件和拖拽動作之間的關系
        * java.awt.dnd.DragGestureListener
        當建立了組件和拖拽動作之間的聯系后,如果用戶執行了拖拽操作,組件將發送一個消息給DragGestureListener監聽器
        DragGestureListener監聽器接下來會發送一個startDrag()消息給拖拽源對象,告訴組件應該執行拖拽的初始化操作了
        拖拽源會產生一個DragSourceContext對象來監聽動作的狀態,這個監聽過程是通過監聽本地方法DragSourceContextPeer來實現的
        * java.awt.datatransfer.Transferable
       
        * java.awt.dnd.DragSourceListener
        DragSourceListener接口負責當鼠標拖拽對象經過組件時的可視化處理, DragSourceListener接口的顯示結果只是暫時改變組件的外觀
        同時他提供一個feedback,當用戶的拖拽操作完成之后會收到一個dragDropEnd的消息,我們可以在這個函數中執行相應的操作
        再來回顧一下拖拽源的建立過程
        首先、 DragGestureRecognizer 確認一個拖拽操作,同時告知 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。創建droppable Component
       創建一個 droppable Component必須和下面兩個對象發生關聯  
        * java.awt.dnd.DropTarget
        DropTarget構造函數使DropTarget 和 DropTargetListener objects發生關聯
        Droptarget對象提供 setComponent 和addDropTargetListener 兩個方法
       
        * 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.
     




    下面的例子演示了一個從樹中拖拽一個節點到文本域中

    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(); //創建拖拽源
            dragSource.createDefaultDragGestureRecognizer(jtr,
                    DnDConstants.ACTION_COPY_OR_MOVE,
                    new DragAndDropDragGestureListener()); //建立拖拽源和事件的聯系
          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) {
            //將數據存儲到Transferable中,然后通知組件開始調用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()) {
            //拖拽動作結束的時候打印出移動節點的字符串
          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();//使用該函數從Transferable對象中獲取有用的數據
           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 九寶 閱讀(2484) 評論(2)  編輯  收藏 所屬分類: Java

    評論

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

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

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

    2010最新日賺300元的項目|只需要每天掛機10小時就可以賺到300元|完全0投資,0風險。詳情查看:http://www.31do.info/?203047.htm  回復  更多評論   

    主站蜘蛛池模板: 日韩a在线观看免费观看| 亚洲一区免费在线观看| 国产成人青青热久免费精品| 中文在线日本免费永久18近| 97免费人妻无码视频| 国内精品99亚洲免费高清| 亚洲A∨精品一区二区三区下载| 美女被免费视频网站a国产| 亚洲免费视频在线观看| 十九岁在线观看免费完整版电影| 四虎免费永久在线播放| 亚洲福利秒拍一区二区| 久久精品免费一区二区| ass亚洲**毛茸茸pics| 国产麻豆成人传媒免费观看| 国产AV无码专区亚洲AV毛网站| 成全视频免费观看在线看| 亚洲精品美女在线观看| 免费毛片a在线观看67194| 亚洲精品女同中文字幕| 一本久久综合亚洲鲁鲁五月天 | 在线看片免费人成视频久网下载| 最近中文字幕无吗高清免费视频| 亚洲今日精彩视频| 国产精品怡红院永久免费| 亚洲精品无码一区二区| 亚洲精品无码成人片在线观看| 精品免费久久久久国产一区| 亚洲国产成人久久综合碰碰动漫3d| 免费黄色福利视频| 国产成人高清亚洲一区91| 免费高清小黄站在线观看| 无码毛片一区二区三区视频免费播放 | 日韩在线看片免费人成视频播放| 青青草国产免费国产是公开| 精品久久香蕉国产线看观看亚洲| 亚洲AV无码专区在线厂| 日本19禁啪啪无遮挡免费动图| 人妖系列免费网站观看| 亚洲制服丝袜在线播放| 亚洲精品无码激情AV|