Java實(shí)現(xiàn)鼠標(biāo)拖放功能的方法
本文實(shí)例講述了Java利用鼠標(biāo)的拖放來實(shí)現(xiàn)交換程序數(shù)據(jù)的方法,即所謂的鼠標(biāo)拖放功能。鼠標(biāo)的拖放功能在圖形化系統(tǒng)中非常常用,Java 提供了java.awt.dnd 和java.awt.datatransfer 包來支持該功能。本例演示如何在程序中實(shí)現(xiàn)拖放的實(shí)現(xiàn)方法,當(dāng)在窗口上部的“Hello World!”標(biāo)簽點(diǎn)下鼠標(biāo),并拖至窗口下部的文本框放開,則在文本框中將添加“Hello World !”文本;繼續(xù)上述過程,將繼續(xù)添加該文本。
該程序功能具體的實(shí)現(xiàn)思路和方法為:在鼠標(biāo)拖放的實(shí)現(xiàn)中,兩個最重要的概念是拖拽源和放置目標(biāo),即drag source 和drop target。拖拽源和放置目標(biāo)都是與可視化的組件相關(guān)聯(lián)的(如果不可視,還怎么拖呢??。M戏偶夹g(shù)的實(shí)質(zhì)就是將拖拽源組件上的數(shù)據(jù)傳遞到放置目標(biāo)組件上,因此從底層看,拖放和上例中的剪貼板技術(shù)很接近。
拖拽源的實(shí)現(xiàn):拖拽源類必須先創(chuàng)建一個DragGestureRecognizer 實(shí)例,表明該類是拖拽源組件類或包含拖拽源組件??梢酝ㄟ^調(diào)用DataSource 對象的createDefaultDragGestureRecognizer()方法實(shí)現(xiàn)。具體的實(shí)現(xiàn)如下:
int action = DnDConstants.ACTION_COPY_OR_MOVE; //拖放的類型 ds.createDefaultDragGestureRecognizer(this,action,this);
上面的語句表明, 拖拽源組件是本類自身的實(shí)例對象, 要完成的拖放的種類是DnDConstants.ACTION_COPY_OR_MOVE 型的,實(shí)現(xiàn)DragGestureListener 接口的類是本類。拖拽源一般實(shí)現(xiàn)DragGestureListener 接口,該接口中定義了一個dragGestureRecognized()方法,當(dāng)開始拖拽是,DragGestureListener 監(jiān)聽到事件,隨即轉(zhuǎn)入dragGestureRecognized()方法處理事件,如將拖拽源的數(shù)據(jù)發(fā)送出去。具體代碼:
public void dragGestureRecognized(DragGestureEvent dge) { //throw new java.lang.UnsupportedOperationException("Method dragGestureRecognized() not yet implemented."); try{ Transferable tr = new StringSelection(this.getText()); //將標(biāo)簽的文本作為數(shù)據(jù),由Transferable 對象包裝 //開始拖拽,設(shè)置光標(biāo)為DragSource.DefaultCopyNoDrop 形,拖放的數(shù)據(jù)是tr 對象,DragSourceListener 是本類 dge.startDrag(DragSource.DefaultCopyNoDrop,tr,this); }catch(Exception err){ err.printStackTrace(); } }
拖拽源還得實(shí)現(xiàn)DragSourceListener 接口,該接口定義了拖拽相關(guān)的各狀態(tài)的事件處理方法。如dragEnter、dragOver、dropActionChanged、dragExit等方法。本例中,將實(shí)現(xiàn)dragEnter()方法來設(shè)置拖拽時(shí)的光標(biāo)形狀,其他的方法為空方法。具體實(shí)現(xiàn)代碼如下:
public void dragEnter(DragSourceDragEvent dsde) { //throw new java.lang.UnsupportedOperationException("Method dragEnter() not yet implemented."); DragSourceContext dsc = dsde.getDragSourceContext(); //得到拖拽源的上下文引用 //設(shè)置拖拽時(shí)的光標(biāo)形狀 int action = dsde.getDropAction(); if ((action&DnDConstants.ACTION_COPY)!=0) dsc.setCursor(DragSource.DefaultCopyDrop); else dsc.setCursor(DragSource.DefaultCopyNoDrop); }
放置目標(biāo)的實(shí)現(xiàn):放置目標(biāo)的類中必須先創(chuàng)建一個DragTarget 實(shí)例,來表明本類是放置目標(biāo)組件類或包含放置目標(biāo)組件,實(shí)現(xiàn)如下:
new DropTarget(this.jTextField1,DnDConstants.ACTION_COPY_OR_MOVE,this);
上面的語句表明, 放置目標(biāo)是this.jTextField1 對象, 拖放操作是DnDConstants.ACTION_COPY_OR_MOVE 型的,而實(shí)現(xiàn)DropTargetListener 接口的類是本類。與DrafSourceListener 相對應(yīng),放置目標(biāo)或其所在類一般實(shí)現(xiàn)DropTargetListener 接口。該接口也定義了很多的方法,如dragEnter、dragOver 等,用于處理當(dāng)拖放過程進(jìn)入不同階段時(shí)的事件。本例只關(guān)心drop()方法,即當(dāng)在放置目標(biāo)組件上放開鼠標(biāo)時(shí)的事件處理,一般用來處理傳遞到的數(shù)據(jù),如本例中將在JTextField 組件上顯示傳遞來的文本數(shù)據(jù),其他方法為空方法,具體代碼如下:
public void drop(DropTargetDropEvent dtde) { //throw new java.lang.UnsupportedOperationException("Method drop() not yet implemented."); try{ Transferable tr = dtde.getTransferable(); //得到傳遞來的數(shù)據(jù)對象 //處理數(shù)據(jù)對象,得到其中的文本信息 if (dtde.isDataFlavorSupported(DataFlavor.stringFlavor)){ dtde.acceptDrop(dtde.getDropAction()); String s = (String) tr.getTransferData(DataFlavor.stringFlavor); this.jTextField1.setText(this.jTextField1.getText()+s); //在放置目標(biāo)上顯示從拖拽源傳遞來的文本信息 dtde.dropComplete(true); }else{ dtde.rejectDrop(); } }catch(Exception err){ err.printStackTrace(); } }
程序代碼:
1.新建一個Project,取名為JDragAndDropDemo。
2.新建一個Application,取名為JDragAndDropDemo;主窗口取名為MainFrame,標(biāo)題為JDragAndDropDemo。
3.新建一個Class,取名為DragJLabel,繼承JLabel 類。
4.利用wizards|implements interface 使DragJLabel 類實(shí)現(xiàn)DragGestureListener 、DragSourceListener 接口。
5.在類DragJLabel 中添加新的屬性 DragSource ds,代碼如下:
class DragJLabel extends JLabel implements DragGestureListener, DragSourceListener { DragSource ds = DragSource.getDefaultDragSource(); //創(chuàng)建DragSource 實(shí)例 …… }
6.編寫DragJLabel 類的構(gòu)造方法。
public DragJLabel(String title,int alignment){ super(title,alignment); //使用父類的方法 int action = DnDConstants.ACTION_COPY_OR_MOVE; ds.createDefaultDragGestureRecognizer(this,action,this); //創(chuàng)建 }
7.實(shí)現(xiàn)DragJLabel 類中的dragGestureRecognized()方法,包裝并發(fā)送數(shù)據(jù)。
public void dragGestureRecognized(DragGestureEvent dge) { //throw new java.lang.UnsupportedOperationException("Method dragGestureRecognized() not yet implemented."); try{ Transferable tr = new StringSelection(this.getText()); dge.startDrag(DragSource.DefaultCopyNoDrop,tr,this); }catch(Exception err){ err.printStackTrace(); } }
8.實(shí)現(xiàn)DragJLabel 類中的dragEnter()方法,設(shè)置光標(biāo)的形狀。
public void dragEnter(DragSourceDragEvent dsde) { //throw new java.lang.UnsupportedOperationException("Method dragEnter() not yet implemented."); DragSourceContext dsc = dsde.getDragSourceContext(); int action = dsde.getDropAction(); if ((action&DnDConstants.ACTION_COPY)!=0) dsc.setCursor(DragSource.DefaultCopyDrop); else dsc.setCursor(DragSource.DefaultCopyNoDrop); }
9.在MainFrame 類的設(shè)計(jì)窗口中下部添加一個JTextField 組件,并在類中創(chuàng)建一個DragJLabel實(shí)例,具體代碼如下:
public class MainFrame extends JFrame implements DropTargetListener { private JPanel contentPane; private BorderLayout borderLayout1 = new BorderLayout(); private JTextField jTextField1 = new JTextField(); DragJLabel label = new DragJLabel("Hello World !",SwingConstants.CENTER); …… }
10.編寫MainFrame 類的初始化方法jbInit(),設(shè)置組件的初始屬性,并創(chuàng)建一個新的DropTarget實(shí)例,代碼如下:
private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(MainFrame.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(borderLayout1); this.setSize(new Dimension(410, 114)); this.setTitle("JDragAndDropDemo"); jTextField1.setFont(new java.awt.Font("Dialog", 0, 14)); contentPane.add(jTextField1, BorderLayout.SOUTH); contentPane.add(this.label,BorderLayout.NORTH); new DropTarget(this.jTextField1,DnDConstants.ACTION_COPY_OR_MOVE,this); }
11.利用wizards|implements interface 使MainFrame 類實(shí)現(xiàn)DropTargetListener 接口。
12.實(shí)現(xiàn)繼承自DropTargetListener 接口的方法drop(),處理傳遞來的數(shù)據(jù),具體代碼如下:
public void drop(DropTargetDropEvent dtde) { //throw new java.lang.UnsupportedOperationException("Method drop() not yet implemented."); try{ Transferable tr = dtde.getTransferable(); if (dtde.isDataFlavorSupported(DataFlavor.stringFlavor)){ dtde.acceptDrop(dtde.getDropAction()); String s = (String) tr.getTransferData(DataFlavor.stringFlavor); this.jTextField1.setText(this.jTextField1.getText()+s); dtde.dropComplete(true); }else{ dtde.rejectDrop(); } }catch(Exception err){ err.printStackTrace(); } }
- javafx tableview鼠標(biāo)觸發(fā)更新屬性詳解
- java獲取鼠標(biāo)在屏幕上坐標(biāo)的方法
- 詳解Java圖形化編程中的鼠標(biāo)事件設(shè)計(jì)
- java通過控制鼠標(biāo)實(shí)現(xiàn)屏幕廣播的方法
- Java實(shí)現(xiàn)鼠標(biāo)拖拽移動界面組件
- Java拖曳鼠標(biāo)實(shí)現(xiàn)畫線功能的方法
- java抓取鼠標(biāo)事件和鼠標(biāo)滾輪事件示例
- 深入Java Robot實(shí)現(xiàn)控制鼠標(biāo)和鍵盤的方法詳解
- 超簡單的java獲取鼠標(biāo)點(diǎn)擊位置坐標(biāo)的實(shí)例(鼠標(biāo)在Jframe上的坐標(biāo))
相關(guān)文章
Java實(shí)現(xiàn)對象轉(zhuǎn)CSV格式
CSV是一種逗號分隔值格式的文件,一般用來存儲數(shù)據(jù)的純文本格式文件。Java對象轉(zhuǎn)CSV,有現(xiàn)成的工具包,commons-lang3 的ReflectionToStringBuilder 就可以簡單的解決的對象轉(zhuǎn)CSV,快跟隨小編一起學(xué)習(xí)一下吧2022-06-06Springboot實(shí)現(xiàn)多線程及線程池監(jiān)控
線程池的監(jiān)控很重要,本文就來介紹一下Springboot實(shí)現(xiàn)多線程及線程池監(jiān)控,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01詳細(xì)分析Java并發(fā)集合ArrayBlockingQueue的用法
這篇文章主要介紹了詳細(xì)分析Java并發(fā)集合ArrayBlockingQueue的用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04Java求s=a+aa+aaa+aaaa+aa...a 5個數(shù)相加的值
求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數(shù)字。例如2+22+222+2222+22222(此時(shí)共有5個數(shù)相加),幾個數(shù)相加有鍵盤控制2017-02-02解決java junit單元測試@Test報(bào)錯的問題
今天小編就為大家分享一篇解決java junit單元測試@Test報(bào)錯的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11springcloud-gateway集成knife4j的示例詳解
這篇文章主要介紹了springcloud-gateway集成knife4j的示例詳解,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03