2014-02-07 84 views
0

我正在嘗試使它能夠在窗口周圍點擊並拖動一個ImageIcon(在這種情況下是一張卡片圖像,但我想通常會學習如何操作),但我真的不知道如何。我希望能夠點擊並按住鼠標按鈕,拖動ImageIcon,並保持它在我釋放鼠標按鈕的位置。ImageIcon點擊並拖動窗口

這是我到目前爲止的代碼:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 

public class MyFirstClass 
{ 

    public static void main(String[] args) 
    { 
     //load the card image from the gif file. 
     final ImageIcon cardIcon = new ImageIcon("cardImages/tenClubs.gif"); 
     JLabel lbl = new JLabel(cardIcon); 
     //create a panel displaying the card image 
     JPanel panel = new JPanel() 
     { 
      //paintComponent is called automatically by the JRE whenever 
      //the panel needs to be drawn or redrawn 
      public void paintComponent(Graphics g) { 
       super.paintComponent(g); 
       cardIcon.paintIcon(this, g, 20, 20); 
      } 
     }; 

     lbl.setTransferHandler(null); 
     MouseListener listener = new MouseAdapter() { 
      public void mousePressed(MouseEvent me) { 
      JComponent comp = (JComponent) me.getSource(); 
      TransferHandler handler = comp.getTransferHandler(); 
      handler.exportAsDrag(comp, me, TransferHandler.COPY); 
      } 
     }; 
     lbl.addMouseListener(listener); 

     //create & make visible a JFrame to contain the panel 
     JFrame window = new JFrame("Cards"); 
     window.add(panel); 
     window.setPreferredSize(new Dimension(200,200)); 
     window.pack(); 
     window.setVisible(true); 
    } 
} 

謝謝。

回答

1

問題是你在混合範例...更不用說你似乎永遠不會將lbl添加到任何東西,所以它永遠不可能接收事件以及panel受佈局管理器控制的事實,使得移動組件非常困難......

在Swing中,至少有三種不同的方式來拖動某些東西,這些東西可以歸結爲您想要實現的東西。

你可以...

使用MouseListenerMouseMotitionListener手動執行的操作。如果你在容器內要物理對象放置的地方,這非常有用,比如你正在嘗試做的,例如...

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Point; 
import java.awt.Rectangle; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class DragMe { 

    public static void main(String[] args) { 
     new DragMe(); 
    } 

    public DragMe() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private BufferedImage img; 
     private Point imgPoint = new Point(0, 0); 

     public TestPane() { 
      try { 
       img = ImageIO.read(new File("Computer.png")); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 

      MouseAdapter ma = new MouseAdapter() { 

       private Point offset; 

       @Override 
       public void mousePressed(MouseEvent e) { 
        Rectangle bounds = getImageBounds(); 
        Point mp = e.getPoint(); 
        if (bounds.contains(mp)) { 
         offset = new Point(); 
         offset.x = mp.x - bounds.x; 
         offset.y = mp.y - bounds.y; 
        } 
       } 

       @Override 
       public void mouseReleased(MouseEvent e) { 
        offset = null; 
       } 

       @Override 
       public void mouseDragged(MouseEvent e) { 
        if (offset != null) { 
         Point mp = e.getPoint(); 
         imgPoint.x = mp.x - offset.x; 
         imgPoint.y = mp.y - offset.y; 
         repaint(); 
        } 
       } 

      }; 
      addMouseListener(ma); 
      addMouseMotionListener(ma); 
     } 

     protected Rectangle getImageBounds() { 
      Rectangle bounds = new Rectangle(0, 0, 0, 0); 
      if (img != null) { 
       bounds.setLocation(imgPoint); 
       bounds.setSize(img.getWidth(), img.getHeight()); 
      } 
      return bounds; 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (img != null) { 
       Graphics2D g2d = (Graphics2D) g.create(); 
       g2d.drawImage(img, imgPoint.x, imgPoint.y, this); 
       g2d.dispose(); 
      } 
     } 
    } 

} 

你可以...

使用核心拖動-n-Drop API。這非常低的水平,併爲您提供了廣泛的靈活性。您可以拖動組件,數據或根據您的需求耍酷......

例如:

如果你是真的很冒險,你可以看看這些...

你可以..

使用新的API轉移的。這個API的目的是使應用程序周圍的數據傳輸更容易。而從技術上講,這樣就可以移動一個組件,這不是它的意圖。

看看...

有關詳細信息...

+0

非常感謝你! – user3281845

+0

玩轉數據轉儲;) – MadProgrammer