更新時間:2019-09-10 17:46:32 來源:動力節(jié)點 瀏覽2603次
今天動力節(jié)點Java培訓(xùn)機構(gòu)小編為大家介紹“Java實現(xiàn)鼠標拖放功能的方法”,希望通過此文能夠幫助到大家,下面就隨小編一起看看Java實現(xiàn)鼠標拖放功能的方法。
本文實例講述了Java利用鼠標的拖放來實現(xiàn)交換程序數(shù)據(jù)的方法,即所謂的鼠標拖放功能。鼠標的拖放功能在圖形化系統(tǒng)中非常常用,Java 提供了java.awt.dnd 和java.awt.datatransfer 包來支持該功能。本例演示如何在程序中實現(xiàn)拖放的實現(xiàn)方法,當在窗口上部的“Hello World!”標簽點下鼠標,并拖至窗口下部的文本框放開,則在文本框中將添加“Hello World !”文本;繼續(xù)上述過程,將繼續(xù)添加該文本。
該程序功能具體的實現(xiàn)思路和方法為:在鼠標拖放的實現(xiàn)中,兩個最重要的概念是拖拽源和放置目標,即drag source 和drop target。拖拽源和放置目標都是與可視化的組件相關(guān)聯(lián)的(如果不可視,還怎么拖呢?!)。拖放技術(shù)的實質(zhì)就是將拖拽源組件上的數(shù)據(jù)傳遞到放置目標組件上,因此從底層看,拖放和上例中的剪貼板技術(shù)很接近。
拖拽源的實現(xiàn):拖拽源類必須先創(chuàng)建一個DragGestureRecognizer 實例,表明該類是拖拽源組件類或包含拖拽源組件??梢酝ㄟ^調(diào)用DataSource 對象的
createDefaultDragGestureRecognizer()方法實現(xiàn)。具體的實現(xiàn)如下:
int action = DnDConstants.ACTION_COPY_OR_MOVE; //拖放的類型
ds.createDefaultDragGestureRecognizer(this,action,this);
上面的語句表明, 拖拽源組件是本類自身的實例對象, 要完成的拖放的種類是DnDConstants.ACTION_COPY_OR_MOVE 型的,實現(xiàn)DragGestureListener 接口的類是本類。拖拽源一般實現(xiàn)DragGestureListener 接口,該接口中定義了一個dragGestureRecognized()方法,當開始拖拽是,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()); //將標簽的文本作為數(shù)據(jù),由Transferable 對象包裝
//開始拖拽,設(shè)置光標為DragSource.DefaultCopyNoDrop 形,拖放的數(shù)據(jù)是tr 對象,DragSourceListener 是本類
dge.startDrag(DragSource.DefaultCopyNoDrop,tr,this);
}catch(Exception err){
err.printStackTrace();
}
}
拖拽源還得實現(xiàn)DragSourceListener 接口,該接口定義了拖拽相關(guān)的各狀態(tài)的事件處理方法。如dragEnter、dragOver、dropActionChanged、dragExit等方法。本例中,將實現(xiàn)dragEnter()方法來設(shè)置拖拽時的光標形狀,其他的方法為空方法。具體實現(xiàn)代碼如下:
public void dragEnter(DragSourceDragEvent dsde) {
//throw new java.lang.UnsupportedOperationException("Method dragEnter() not yet implemented.");
DragSourceContext dsc = dsde.getDragSourceContext(); //得到拖拽源的上下文引用
//設(shè)置拖拽時的光標形狀
int action = dsde.getDropAction();
if ((action&DnDConstants.ACTION_COPY)!=0)
dsc.setCursor(DragSource.DefaultCopyDrop);
else
dsc.setCursor(DragSource.DefaultCopyNoDrop);
}
放置目標的實現(xiàn):放置目標的類中必須先創(chuàng)建一個DragTarget 實例,來表明本類是放置目標組件類或包含放置目標組件,實現(xiàn)如下:
new DropTarget(this.jTextField1,DnDConstants.ACTION_COPY_OR_MOVE,this);
上面的語句表明, 放置目標是this.jTextField1 對象, 拖放操作是DnDConstants.ACTION_COPY_OR_MOVE 型的,而實現(xiàn)DropTargetListener 接口的類是本類。與DrafSourceListener 相對應(yīng),放置目標或其所在類一般實現(xiàn)DropTargetListener 接口。該接口也定義了很多的方法,如dragEnter、dragOver 等,用于處理當拖放過程進入不同階段時的事件。本例只關(guān)心drop()方法,即當在放置目標組件上放開鼠標時的事件處理,一般用來處理傳遞到的數(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); //在放置目標上顯示從拖拽源傳遞來的文本信息
dtde.dropComplete(true);
}else{
dtde.rejectDrop();
}
}catch(Exception err){
err.printStackTrace();
}
}
程序代碼:
1、新建一個Project,取名為JDragAndDropDemo。
2、新建一個Application,取名為JDragAndDropDemo;主窗口取名為MainFrame,標題為JDragAndDropDemo。
3、新建一個Class,取名為DragJLabel,繼承JLabel 類。
4、利用wizards|implements interface 使DragJLabel 類實現(xiàn)DragGestureListener 、DragSourceListener 接口。
5、在類DragJLabel 中添加新的屬性 DragSource ds,代碼如下:
class DragJLabel extends JLabel implements DragGestureListener, DragSourceListener {
DragSource ds = DragSource.getDefaultDragSource(); //創(chuàng)建DragSource 實例
……
}
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、實現(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、實現(xiàn)DragJLabel 類中的dragEnter()方法,設(shè)置光標的形狀。
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è)計窗口中下部添加一個JTextField 組件,并在類中創(chuàng)建一個DragJLabel實例,具體代碼如下:
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實例,代碼如下:
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 類實現(xiàn)DropTargetListener 接口。
12、實現(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();
}
}
以上就是動力節(jié)點Java培訓(xùn)機構(gòu)小編介紹的“Java實現(xiàn)鼠標拖放功能的方法”的內(nèi)容,希望對大家有幫助,更多Java最新資訊請繼續(xù)關(guān)注動力節(jié)點Java培訓(xùn)機構(gòu)官網(wǎng),每天會有精彩內(nèi)容分享與你。
相關(guān)視頻教程推薦
Java免費視頻教程下載:http://www.dabaquan.cn/video.html
相關(guān)閱讀