Drag&Drop mit Java realisieren!


 
Neues Thema eröffnen   Neue Antwort erstellen    Webmaster Forum -> Computer & Internet
Vorheriges Thema anzeigen :: Nächstes Thema anzeigen  
Autor Nachricht
John
Gast

BeitragDrag&Drop mit Java realisieren! Antworten mit Zitat

Wie kann ich Drag&Drop mit Java Realisieren?
10 Apr 2004 18:34
wpSEO, das Plugin für WordPress SEO
David
Administrator

BeitragSource-Code für Drag&Drop Antworten mit Zitat

Hi John, hier der Source-Code. Hoffe, dass er funktioniert. (Leider weis ich nicht mehr woher der stammt)

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
public class DragAndDrop extends Frame
{
 JButton button = new JButton("Hello");
 JPanel panel = new JPanel();
 public DragAndDrop()
 {
  addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e)
   {
    dispose();
    System.exit(0);
   }
  });
  this.add(panel);
  panel.add(button);
  setDrag();
 }
 public static void main(String args[])
 {
  System.out.println("Starting Drag And Drop Example ...");
  DragAndDrop mainFrame = new DragAndDrop();
  mainFrame.setSize(400, 400);
  mainFrame.setLocation(100,100);
  mainFrame.setTitle("Drag And Drop Example");
  mainFrame.setVisible(true);
 }
 void setDrag()
 {
 // Внутренние классы для поддержки Drag and Drop
 class ViewTransferable implements Transferable
 {
  DataFlavor[] flavors = new DataFlavor[]{DataFlavor.stringFlavor};
  public ViewTransferable()
  {
  }
  public DataFlavor[] getTransferDataFlavors()
  {
   return flavors;
  }
  public boolean isDataFlavorSupported(DataFlavor flavor)
  {
   if (flavor.equals(flavors[0]))
   {
    return true;
   }
   return false;
  }
  public Object getTransferData(DataFlavor flavor)throws UnsupportedFlavorException
  {
   if (!isDataFlavorSupported(flavor))
   {
    System.out.println("unsuported flavor");
    return null;
   }
   if (flavor.equals(flavors[0]))
   {
    return(null);
   }
   return null;
  }
 }
 class CanvasDragSource implements DragGestureListener, DragSourceListener
 {
  CanvasDragSource()
  {
   DragSource dragSource = DragSource.getDefaultDragSource();
   dragSource.createDefaultDragGestureRecognizer(button,DnDConstants.ACTION_COPY_OR_MOVE,this);
  }
  public void dragGestureRecognized(DragGestureEvent dge)
  {
   Point p = dge.getDragOrigin();
   int x = (int)p.getX();
   int y = (int)p.getY();
   Transferable transferable = new ViewTransferable();
   dge.startDrag(null,transferable,this);
  }
  public void dropActionChanged(DragSourceDragEvent dsde){}
  public void dragEnter(DragSourceDragEvent dsde){}
  public void dragOver(DragSourceDragEvent dsde){}
  public void dragExit(DragSourceEvent dse){}
  public void dragDropEnd(DragSourceDropEvent dsde){}
 }
 class CanvasDropTarget implements DropTargetListener
 {
  DropTarget dropTarget;
  boolean acceptableType;
  CanvasDropTarget()
  {
   dropTarget = new DropTarget(panel, DnDConstants.ACTION_COPY_OR_MOVE,this,true,null);
  }
  public void dragOver(DropTargetDragEvent dtde)
  {
   acceptOrRejectDrag(dtde);
  }
  public void dropActionChanged(DropTargetDragEvent dtde)
  {
   acceptOrRejectDrag(dtde);
  }
  public void dragExit(DropTargetEvent dte){}
  public void drop(DropTargetDropEvent dtde)
  {
   if ((dtde.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0)
   {
    dtde.acceptDrop(dtde.getDropAction());
    dtde.getDropTargetContext().dropComplete(true);
    double x = dtde.getLocation().getX();
    double y = dtde.getLocation().getY();
    button.setLocation((int)x,(int)y);
   }
   else
   {
    dtde.rejectDrop();
   }
  }
  public void dragEnter(DropTargetDragEvent dtde)
  {
   checkTransferType(dtde);
   acceptOrRejectDrag(dtde);
  }
  boolean acceptOrRejectDrag(DropTargetDragEvent dtde)
  {
   int dropAction = dtde.getDropAction();
   int sourceActions = dtde.getSourceActions();
   boolean acceptedDrag = false;
   if (!acceptableType || (sourceActions &DnDConstants.ACTION_COPY_OR_MOVE) == 0)
   {
    dtde.acceptDrag(DnDConstants.ACTION_COPY);
    acceptedDrag = true;
   }
   else if ((dropAction & DnDConstants.ACTION_COPY_OR_MOVE ) == 0)
    {
     dtde.acceptDrag(DnDConstants.ACTION_COPY);
     acceptedDrag = true;
    }
   return acceptedDrag;
  }
  void checkTransferType(DropTargetDragEvent dtde)
  {
   acceptableType = dtde.isDataFlavorSupported(DataFlavor.stringFlavor);
  }
 }
 new CanvasDragSource();
 new CanvasDropTarget();
 }
}

_________________
MfG David Mirzoian
axinio Internet Marketing - professionelle Suchmaschinenoptimierung
10 Apr 2004 18:43
Benutzer-Profile anzeigen Private Nachricht senden Website dieses Benutzers besuchen
David
Administrator

BeitragModifizierter Cource-Code Antworten mit Zitat

Habe gerade auf meiner Platte noch einen etwas modifizierten Source-Code gefunden. Vielleicht kannst du damit mehr anfangen. Smile

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
public class DragAndDrop extends Frame
{
 JButton button = new JButton("Hello");
 JPanel panel = new JPanel();
 ComponentA component1=new ComponentA(button);
 public DragAndDrop()
 {
  addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e)
   {
    dispose();
    System.exit(0);
   }
  });
  this.add(panel);
  panel.add(component1);
  panel.add(button);
  setDrag();
 }
 public static void main(String args[])
 {
  System.out.println("Starting Drag And Drop Example ...");
  DragAndDrop mainFrame = new DragAndDrop();
  mainFrame.setSize(400, 400);
  mainFrame.setLocation(100,100);
  mainFrame.setTitle("Drag And Drop Example");
  mainFrame.setVisible(true);
 }
 void setDrag()
 {
 class ViewTransferable implements Transferable
 {
  DataFlavor[] flavors = new DataFlavor[]{DataFlavor.stringFlavor};
  public ViewTransferable()
  {
  }
  public DataFlavor[] getTransferDataFlavors()
  {
   return flavors;
  }
  public boolean isDataFlavorSupported(DataFlavor flavor)
  {
   if (flavor.equals(flavors[0]))
   {
    return true;
   }
   return false;
  }
  public Object getTransferData(DataFlavor flavor)throws UnsupportedFlavorException
  {
   if (!isDataFlavorSupported(flavor))
   {
    System.out.println("unsuported flavor");
    return null;
   }
   if (flavor.equals(flavors[0]))
   {
    return(null);
   }
   return null;
  }
 }
 class CanvasDragSource implements DragGestureListener, DragSourceListener
 {
  CanvasDragSource()
  {
   DragSource dragSource = DragSource.getDefaultDragSource();
   dragSource.createDefaultDragGestureRecognizer(button,DnDConstants.ACTION_COPY_OR_MOVE,this);
  }
  public void dragGestureRecognized(DragGestureEvent dge)
  {
   Transferable transferable = new ViewTransferable();
   dge.startDrag(null,transferable,this);
  }
  public void dropActionChanged(DragSourceDragEvent dsde){}
  public void dragEnter(DragSourceDragEvent dsde){}
  public void dragOver(DragSourceDragEvent dsde){}
  public void dragExit(DragSourceEvent dse){}
  public void dragDropEnd(DragSourceDropEvent dsde){}
 }
 class CanvasDropTarget implements DropTargetListener
 {
  DropTarget dropTarget;
  boolean acceptableType;
  CanvasDropTarget()
  {
   dropTarget = new DropTarget(panel, DnDConstants.ACTION_COPY_OR_MOVE,this,true,null);
  }
  public void dragOver(DropTargetDragEvent dtde)
  {
   acceptOrRejectDrag(dtde);
   component1.setVisible(true);
component1.setBounds((int)dtde.getLocation().getX()+1,(int)dtde.getLocation().getY()+1,button.getWidth(),button.getHeight());
  }
  public void dropActionChanged(DropTargetDragEvent dtde)
  {
   acceptOrRejectDrag(dtde);
  }
  public void dragExit(DropTargetEvent dte){}
  public void drop(DropTargetDropEvent dtde)
  {
   if ((dtde.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0)
   {
    dtde.acceptDrop(dtde.getDropAction());
    dtde.getDropTargetContext().dropComplete(true);
    double x = dtde.getLocation().getX();
    double y = dtde.getLocation().getY();
    button.setLocation((int)x,(int)y);
    component1.setVisible(false);
   }
   else
   {
    dtde.rejectDrop();
   }
  }
  public void dragEnter(DropTargetDragEvent dtde)
  {
   checkTransferType(dtde);
   acceptOrRejectDrag(dtde);
  }
  boolean acceptOrRejectDrag(DropTargetDragEvent dtde)
  {
   int dropAction = dtde.getDropAction();
   int sourceActions = dtde.getSourceActions();
   boolean acceptedDrag = false;
   if (!acceptableType || (sourceActions &DnDConstants.ACTION_COPY_OR_MOVE) == 0)
   {
    dtde.acceptDrag(DnDConstants.ACTION_COPY);
    acceptedDrag = true;
   }
   else if ((dropAction & DnDConstants.ACTION_COPY_OR_MOVE ) == 0)
    {
     dtde.acceptDrag(DnDConstants.ACTION_COPY);
     acceptedDrag = true;
    }
   return acceptedDrag;
  }
  void checkTransferType(DropTargetDragEvent dtde)
  {
   acceptableType = dtde.isDataFlavorSupported(DataFlavor.stringFlavor);
  }
 }
 new CanvasDragSource();
 new CanvasDropTarget();
 }
}
class ComponentA extends JComponent
{
 Image img;
 JComponent comp;
 ComponentA(JComponent comp)
 {
  this.comp=comp;
  setVisible(false);
 }
 public void paintComponent(Graphics g)
 {
  img = createImage(comp.getWidth(),comp.getHeight());
  comp.paint(img.getGraphics());
  g.drawImage(img,0,0,this);
 }
}

_________________
MfG David Mirzoian
axinio Internet Marketing - professionelle Suchmaschinenoptimierung
10 Apr 2004 18:56
Benutzer-Profile anzeigen Private Nachricht senden Website dieses Benutzers besuchen
axinio Internet Marketing
Beiträge der letzten Zeit anzeigen:   
Neues Thema eröffnen   Neue Antwort erstellen    Webmaster Forum -> Computer & Internet Alle Zeiten sind GMT + 1 Stunde
Seite 1 von 1

 
Gehe zu:  

Ähnliche Beiträge
Thema Webmaster Forum Antw. Autor Verfasst am
Keine neuen Beiträge java script in xhtml ergibt Fehler be... (X)HTML 1 Aschi (Gast) 20 Dez 2010 12:14 Letzten Beitrag anzeigen
Keine neuen Beiträge Java Script ändern, aber wie? Hilfe für Webmaster 9 gitty 21 Feb 2010 14:10 Letzten Beitrag anzeigen
Keine neuen Beiträge Drop down - Menü in Frames CSS 2 LadyMalia 30 Jan 2010 12:23 Letzten Beitrag anzeigen
Keine neuen Beiträge Drag&Drop 2 Bilder gleichzeitig b... Javascript 0 Tolonath 23 Jan 2010 21:15 Letzten Beitrag anzeigen
Keine neuen Beiträge JAVA------Wie füge ich eine Datei in ... Hilfe für Webmaster 8 hundertkilometer 09 Sep 2009 16:45 Letzten Beitrag anzeigen