עבור לתוכן

בעיה עם DRAG AND DROP בJAVA

Featured Replies

פורסם

יש לי אפשרות להזיז כפתורים(JBUTTON) הזזה של הכפתורים מתבצעת ע"י פונקציות DRAG AND DROP כשאני ממזער, או מוחק מהרשימה את כפתור אחד, אז הכפתורים קופצים לנקודה אחרת על המסך בלי שהזזתי

מה הבעיה??

פורסם
  • מחבר

הקוד

/ 

import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;


public class TestDragDrop extends Frame implements ActionListener {
TestPanel testPanel = null;
TestButton testButton = null;
public TestDragDrop() {
setSize(300,300);
testPanel = new TestPanel();
testPanel.setSize(300,300);
testPanel.setBackground(Color.LIGHT_GRAY);
//testButton = new TestButton(new ImageIcon("iBridge.png"));
//testButton.setBorderPainted(false);
//testButton.setContentAreaFilled(false);
//testButton.addActionListener(this);
//testPanel.add(testButton);
add(testPanel);
show();
}
public static void main(String args[]) {
TestDragDrop c=new TestDragDrop();
}

public void actionPerformed(ActionEvent e) {
System.out.println("dfsdfsd");


}
}

class TestPanel extends Panel implements DropTargetListener {
DropTarget dt;
TestButton b;

public TestPanel() {
b=new TestButton(new ImageIcon("iBridge.png"));
add(b);
dt = new DropTarget(this, DnDConstants.ACTION_NONE, this);
}
public void dragEnter(DropTargetDragEvent arg0) {
}

public void dragExit(DropTargetEvent arg0) {
}

public void dragOver(DropTargetDragEvent arg0) {
b.x = arg0.getLocation().x;
b.y = arg0.getLocation().y;
System.out.println(b.x + arg0.getLocation().x);
System.out.println(b.y + arg0.getLocation().y);
}

public void drop(DropTargetDropEvent arg0) {
}

public void dropActionChanged(DropTargetDragEvent arg0) {
}

}
class TestButton extends JButton implements DragSourceListener,
DragGestureListener {
DragSource ds = DragSource.getDefaultDragSource();
int x,y;
public TestButton(ImageIcon label) {
super(label);
ds.createDefaultDragGestureRecognizer(this,DnDConstants.ACTION_MOVE, this);
}
public void dragGestureRecognized(DragGestureEvent arg0) {
Transferable t = new StringSelection(getLabel());
arg0.startDrag(DragSource.DefaultMoveDrop, t, this);
}
public void dragDropEnd(DragSourceDropEvent arg0) {
}

public void dragEnter(DragSourceDragEvent arg0) {
}

public void dragExit(DragSourceEvent arg0) {
}

public void dragOver(DragSourceDragEvent arg0) {
arg0.getDragSourceContext().getComponent().setLocation(x,y);
}

public void dropActionChanged(DragSourceDragEvent arg0) {
}



}
//class Constant {
//public static int x;
//public static int y;
//}

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים