JAVA - How To Get Mouse Coordinates In Java

How To Get Mouse Coordinates In Java Netbeans

_________________________________________________

In This Java Tutorial We Will See How To Get X and Y Position Of The Cursor Inside A JFrame Using NetBeans .

Source Code:

package JavaDB_001;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;


public class Project extends JFrame {
 JLabel l = new JLabel("X ][ Y");
 JPanel p = new JPanel();
public Project(){

    setContentPane(p);
    p.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
            l.setText("X = "+e.getX()+"][ Y = "+e.getY());
        }
    });
    p.add(l);
    setSize(400, 120);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setBackground(Color.decode("#bdb76b"));
    setVisible(true);
}
public static void main(String[] args){
   
new Project();
}
}
///////////////////////////////////////////////OUTPUT:
How To Get Mouse Coordinates In Java
X = Coordinates Of X
Y = Coordinates Of Y



Share this

Related Posts

Previous
Next Post »