<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 九寶 閱讀(2479) 評論(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  回復  更多評論   

    主站蜘蛛池模板: 免费中文字幕在线| 久久伊人久久亚洲综合| 成人精品综合免费视频| 亚洲人成网站在线观看播放| 在线免费观看亚洲| 国产成人亚洲精品91专区高清| 国产亚洲AV手机在线观看| 99精品国产免费久久久久久下载| 国产精品亚洲专区在线播放| 亚洲国产成人私人影院| 午夜一级毛片免费视频| 久久免费区一区二区三波多野| 亚洲日韩精品无码专区加勒比☆| 亚洲综合久久夜AV | 一个人看的www在线观看免费| 一级大黄美女免费播放| 亚洲中文字幕日本无线码| 亚洲三区在线观看无套内射| 在线播放免费播放av片| 美女视频黄的免费视频网页| 亚洲AV日韩综合一区| 亚洲精品福利网站| 亚洲一区二区三区AV无码| 天天天欲色欲色WWW免费| 天堂在线免费观看| 男男gvh肉在线观看免费| 亚洲国产成人久久综合一区| 亚洲人成影院在线无码按摩店| 免费网站看v片在线香蕉| 久久综合国产乱子伦精品免费| 春意影院午夜爽爽爽免费| 国产人成亚洲第一网站在线播放| 亚洲av日韩av无码黑人| 久久夜色精品国产亚洲av| 国产精品色午夜免费视频| www.999精品视频观看免费| 无码精品人妻一区二区三区免费看| 人妖系列免费网站观看| 色屁屁www影院免费观看视频| 在线亚洲午夜片AV大片| 亚洲美女在线观看播放|