How To Get Cursor Position In Java
Project Source Code:
package p;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Mouse_CR extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Mouse_CR frame = new Mouse_CR();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Mouse_CR() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 608, 448);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 592, 410);
contentPane.add(panel);
JLabel label = new JLabel(".................");
label.setFont(new Font("Tahoma", Font.BOLD, 20));
panel.add(label);
panel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
label.setText("X = "+ e.getX()+" ; Y = "+e.getY());
}
});
}
}