2015-10-11 43 views
0

當用戶點擊/按住鼠標按鈕時,我正嘗試使用鼠標移動圖像。當用戶將鼠標放下(其中它使用鼠標實時更新)時,我設法做到了這一點,但是,當我點擊圖像時,圖像將其位置調整到更新的區域,這不是我希望它能做到。如果用戶點擊時唯一想要圖片移動的地方是用戶第二次再次點擊。因此,可以說如果用戶點擊位於(0,0)處的圖像,如果用戶再次在屏幕上的其他位置單擊,則位置現在位於(x,y)處。點擊時使用MouseListener移動圖像

以下是我有:

@Override 
public void mouseClicked(MouseEvent e) { 

    clickCount++; 
    if(clickCount % 2 == 0){ 
     p.setLocation(e.getX(), e.getY());//p is just a panel that contains the img 
     repaint(); 
    } 
    System.out.println("mouse clicked..."); 

} 

更新的代碼:

public void mouseClicked(MouseEvent e) { 

    Object o = e.getSource(); 
    if(o instanceof JPanel) 
     clickCount++; 

    if(clickCount % 2 == 0 && clickCount != 0){ 
     p.setLocation(e.getX(), e.getY()); 
     repaint(); 
    } 
    System.out.println("mouse clicked " + clickCount + " times"); 
} 

如果你點擊在屏幕任意位置(之後的clickCount%2 ==這是接近但工作,0 ),那麼圖像會移動。

回答

2

當調用mouseClicked時,確定是否先前點擊過某個對象,如果是,則將該對象移動到當前位置,如果不是,則檢查用戶是否點擊了某些東西是可移動的並將其分配給變量(你以後用來檢查)。

一旦物體移動時,參考設置爲null

private JPanel clicked; 

@Override 
public void mouseClicked(MouseEvent e) { 

    if (clicked != null) { 
     clicked.setLocation(e.getX(), e.getY()); 
     clicked = null; 
    } else { 
     // Figure out if any panel was clicked and assign 
     // a reference to clicked 
    } 

} 

Runnable的例子...

所以,它聽起來就像你試圖同時支持點擊和拖動搬遷,這是種類...難,因爲兩者所需的鼠標操作不同,所以您需要監視多個狀態並作出關於您可能處於的狀態的決定,例如...

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Point; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class MouseTest { 

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

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

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

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(null); 
      JPanel panel = new JPanel(); 
      panel.setBackground(Color.RED); 
      panel.setSize(50, 50); 
      panel.setLocation(50, 50); 
      add(panel); 

      MouseAdapter ma = new MouseAdapter() { 
       private Point offset; 
       private Point clickPoint; 
       private JPanel clickedPanel; 

       @Override 
       public void mousePressed(MouseEvent e) { 
        // Get the current clickPoint, this is used to determine if the 
        // mouseRelease event was part of a drag operation or not 
        clickPoint = e.getPoint(); 
        // Determine if there is currently a selected panel or nor 
        if (clickedPanel != null) { 
         // Move the selected panel to a new location 
         moveSelectedPanelTo(e.getPoint()); 
         // Reset all the other stuff we might other was have set eailer 
         offset = null; 
         clickedPanel = null; 
        } else { 
         // Other wise, find which component was clicked 
         findClickedComponent(e.getPoint()); 
        } 
       } 

       @Override 
       public void mouseReleased(MouseEvent e) { 
        // Check to see if the current point is equal to the clickedPoint 
        // or not. If it is, then this is part of a "clicked" operation 
        // meaning that the selected panel should remain "selected", otherwise 
        // it's part of drag operation and should be discarded 
        if (!e.getPoint().equals(clickPoint)) { 
         clickedPanel = null; 
        } 
        clickPoint = null; 
       } 

       @Override 
       public void mouseDragged(MouseEvent e) { 
        // Drag the selected component to a new location... 
        if (clickedPanel != null) { 
         moveSelectedPanelTo(e.getPoint()); 
        } 
       } 

       protected void findClickedComponent(Point p) { 
        Component comp = getComponentAt(p); 
        if (comp instanceof JPanel && !comp.equals(TestPane.this)) { 
         clickedPanel = (JPanel) comp; 
         int x = p.x - clickedPanel.getLocation().x; 
         int y = p.y - clickedPanel.getLocation().y; 
         offset = new Point(x, y); 
        } 

       } 

       private void moveSelectedPanelTo(Point p) { 
        if (clickedPanel != null) { 
         int x = p.x - offset.x; 
         int y = p.y - offset.y; 
         System.out.println(x + "x" + y); 
         clickedPanel.setLocation(x, y); 
        } 
       } 

      }; 

      addMouseListener(ma); 
      addMouseMotionListener(ma); 
     } 

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

    } 

} 
+0

對不起,你爲什麼要設置點擊爲空?那麼你不會只能進入IF一次嗎? –

+0

一旦你移動它,你不再想繼續移動(我假設),所以下一次點擊會選擇其他面板被點擊。如果您想使用拖動,請使用'MouseMotionListener'和'mouseDragged'事件 – MadProgrammer

+0

非常感謝您,這正是我一直在尋找的! :D –