2014-02-16 351 views
0

所以我有這樣的代碼,但是我不明白它是如何設置每次鼠標移動時鼠標的座標標籤...獲取鼠標座標每次移動鼠標

timer.schedule(new TimerTask() { 

    @Override 
    public void run() { 
     int mouseX = MouseInfo.getPointerInfo().getLocation().x; 
     int mouseY = MouseInfo.getPointerInfo().getLocation().y; 
     lblInfo.setText("Nada "+mouseX+mouseY); 
    } 

}, 1); 

林甚至不知道代碼是正確的,但我想要做的是每次鼠標移動時在鼠標座標中調用lblInfo。

此代碼做什麼,一旦程序啓動時只顯示它...

+2

參見[如何寫一個'MouseMotionListener'](http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html) –

+0

在run()方法執行多長時間?添加一個println()來查看。 – NormR

回答

3

您需要implements MouseMotionListener,然後添加你的邏輯裏mouseMoved方法,如:

public class MyClass implements MouseMotionListener { 

    public void mouseMoved(MouseEvent e) { 
     System.out.println("X : " + e.getX()); 
     System.out.println("Y : " + e.getY()); 
    } 

    public void mouseDragged(MouseEvent e) { 
     //do something 
    } 

} 

瞭解更多關於MouseMotionListener

+0

這就是問題所在,我擴展了繼承MouseMotionListener的MouseAdapter(如果我沒記錯的話),但是我怎麼告訴它每次鼠標移動時都要設置座標。例如,每當在JPanel中按下鼠標時,我都可以獲得座標,但在屏幕上無處不在。我使用mouseClicked,它工作正常...但無法找到合適的事件,使其每次鼠標移動時更新座標,鼠標移動似乎不工作omg ... – Alpha2k

+0

public void mouseMoved(MouseEvent e) int mouseX = e.getX(); int mouseY = e.getY(); System.out.println(「Test Info:」+ mouseX +「」+ mouseY); igu.lblInfo.setText(「Test Info:」+ mouseX +「」+ mouseY); } – Alpha2k

+0

http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseAdapter.html,mouseMoved不會這樣做「當鼠標光標移動到組件上時調用,但沒有按鈕已被推動。「我不知道爲什麼... – Alpha2k

0

看看這個例子。你首先需要實現mousePresseded然後mouseDragged。第一個獲得初始印刷的點,那麼mouseDragged將使用這些座標。

addMouseListener(new MouseAdapter() { 
    public void mousePressed(MouseEvent me) { 
     // Get x,y and store them 
     pX = me.getX(); 
     pY = me.getY(); 
    } 
}); 
addMouseMotionListener(new MouseAdapter() { 
    public void mouseDragged(MouseEvent me) { 
     frame.setLocation(frame.getLocation().x + me.getX() - pX, 
       frame.getLocation().y + me.getY() - pY); 
    } 
}); 

完整的運行示例。它使用undecorate框架並創建一個JPanel作爲標題,您可以通過拖動來移動框架。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.LineBorder; 

public class UndecoratedExample { 
    static JFrame frame = new JFrame(); 
    static class MainPanel extends JPanel { 
     public MainPanel() { 
      setBackground(Color.gray); 
     } 

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

    static class BorderPanel extends JPanel { 

     JLabel stackLabel; 
     int pX, pY; 

     public BorderPanel() { 
      ImageIcon icon = new ImageIcon(getClass().getResource(
        "/resources/stackoverflow1.png")); 
      stackLabel = new JLabel(); 
      stackLabel.setIcon(icon); 

      setBackground(Color.black); 
      setLayout(new FlowLayout(FlowLayout.RIGHT)); 

      add(stackLabel); 

      stackLabel.addMouseListener(new MouseAdapter() { 
       public void mouseReleased(MouseEvent e) { 
        System.exit(0); 
       } 
      }); 
      addMouseListener(new MouseAdapter() { 
       public void mousePressed(MouseEvent me) { 
        // Get x,y and store them 
        pX = me.getX(); 
        pY = me.getY(); 
       } 
      }); 
      addMouseMotionListener(new MouseAdapter() { 
       public void mouseDragged(MouseEvent me) { 
        frame.setLocation(frame.getLocation().x + me.getX() - pX, 
          frame.getLocation().y + me.getY() - pY); 
       } 
      }); 
     } 
    } 

    static class OutsidePanel extends JPanel { 

     public OutsidePanel() { 
      setLayout(new BorderLayout()); 
      add(new MainPanel(), BorderLayout.CENTER); 
      add(new BorderPanel(), BorderLayout.PAGE_START); 
      setBorder(new LineBorder(Color.BLACK, 5)); 
     } 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 

       frame.setUndecorated(true); 
       frame.add(new OutsidePanel()); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 
}