int action = DnDConstants.ACTION_COPY_OR_MOVE; //拖放的类型 ds.createDefaultDragGestureRecognizer(this,action,this);
public void dragGestureRecognized(DragGestureEvent dge) {
//throw new java.lang.UnsupportedOperationException("Method dragGestureRecognized() not yet implemented.");
try{
Transferable tr = new StringSelection(this.getText()); //将标签的文本作为数据,由Transferable 对象包装
//开始拖拽,设置光标为DragSource.DefaultCopyNoDrop 形,拖放的数据是tr 对象,DragSourceListener 是本类
dge.startDrag(DragSource.DefaultCopyNoDrop,tr,this);
}catch(Exception err){
err.printStackTrace();
}
}
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);
}
new DropTarget(this.jTextField1,DnDConstants.ACTION_COPY_OR_MOVE,this);
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();
}
}
class DragJLabel extends JLabel implements DragGestureListener, DragSourceListener {
DragSource ds = DragSource.getDefaultDragSource(); //创建DragSource 实例
……
}
public DragJLabel(String title,int alignment){
super(title,alignment); //使用父类的方法
int action = DnDConstants.ACTION_COPY_OR_MOVE;
ds.createDefaultDragGestureRecognizer(this,action,this); //创建
}
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();
}
}
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);
}
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);
……
}
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);
}
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();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有