2014-02-16 299 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); 
      } 
     }); 
    } 
}