拖拽物品

传递参数物件ID值
修改部分代码:jmetest.awt.swingui.dnd.TestDnd

protected void buildUI() {
new JMEDragAndDrop(this.getDesktop());

JInternalFrame frame = new JInternalFrame("dnd test", true, true);
frame.setLayout(new GridLayout(4, 4));
JInternalFrame frame1 = new JInternalFrame("dnd test1", true, true);
frame1.setLayout(new GridLayout(4, 4));

frame1.setIconifiable(true);
Icon icon1 = getResizedIcon("kofei.png");
frame.add(new DndIcon(this, icon1,201));
Icon icon2 = getResizedIcon("Monkey.png");
frame.add(new DndIcon(this, icon2,205));

// few empty icons to let you play
for (int i = 0; i < 13; i++) {
frame.add(new DndIcon(this, null));
}
Icon icon3 = getResizedIcon("logo.jpg");
frame.add(new DndIcon(this, icon3,209));


DndIcon icons[]=new DndIcon[16];
for (int i = 0; i < 16; i++) {
icons[i]=new DndIcon(this, null,i+1){

@Override
public void receive(int partId) {
System.out.println("received:"+partId);
}
};
frame1.add(icons[i]);
}
frame.setSize(64 * 4, 64 * 4);
frame.setLocation(100, 100);
frame.setVisible(true);

frame1.setSize(64 * 4, 64 * 4);
frame1.setLocation(320, 100);
frame1.setVisible(true);

this.getDesktop().getJDesktop().add(frame);
this.getDesktop().getJDesktop().add(frame1);

MouseInput.get().setCursorVisible(true);
}

private Icon getResizedIcon(String fileName) {
ImageIcon icon = new ImageIcon(this.getClass().getResource("/jmetest/data/images/" + fileName));
icon.setImage(icon.getImage().getScaledInstance(64, 64, 16));
return icon;
}
}

/**
* DndIcon is the drag source and the drop target, so you can easily drag /
* swap icons from different panels
*
* @author Nomis
*/
public class DndIcon extends JLabel implements JMEDragGestureListener, JMEDragSourceListener, JMEDropTargetListener {

private static final long serialVersionUID = 1L;
private JMEDragAndDrop dndSupport;
public int partID=0;
public DndIcon(JMEDesktopState desktopSate, Icon icon) {
this.setIcon(icon);
this.dndSupport = desktopSate.getDesktop().getDragAndDropSupport();
new JMEMouseDragGestureRecognizer(dndSupport, this, DnDConstants.ACTION_COPY_OR_MOVE, this);
this.setBorder(BorderFactory.createLineBorder(Color.black));
}

public DndIcon(JMEDesktopState desktopSate, Icon icon,int partID) {
this.setIcon(icon);
this.dndSupport = desktopSate.getDesktop().getDragAndDropSupport();
new JMEMouseDragGestureRecognizer(dndSupport, this, DnDConstants.ACTION_COPY_OR_MOVE, this);
this.setBorder(BorderFactory.createLineBorder(Color.black));
this.partID=partID;
}

/**
* 接受一个物体
*/
public void drop(JMEDropTargetEvent e) {
TransferableImage t = (TransferableImage) e.getTransferable();
Icon icon = null;
try {
icon = (Icon) t.getTransferData(null);
} catch (UnsupportedFlavorException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("------"+partID);
DndIcon source = (DndIcon) e.getSource();
receive(source.partID);
if (icon != null) {
// Set current icon to the source
source.setIcon(this.getIcon());
this.setIcon(icon);
}

}

public void receive(int partId){

}

/**
* 拉出的时候
* @param dge
*/
public void dragGestureRecognized(JMEDragGestureEvent dge) {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值