import javax.swing.*; import java.awt.*; public class Example1 { public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); JLabel label = new JLabel("Hello, World!"); panel.add(label); frame.add(panel); frame.setSize(300, 200); frame.setVisible(true); Point point = label.getLocationOnScreen(); System.out.println("Point in screen coordinates: " + point); point = SwingUtilities.convertPoint(label, new Point(0, 0), frame); System.out.println("Point in frame coordinates: " + point); } }
import javax.swing.*; import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class Example2 implements MouseListener { private JFrame frame; private JPanel panel; private JLabel label; public Example2() { frame = new JFrame(); panel = new JPanel(); label = new JLabel("Hello, World!"); panel.add(label); frame.add(panel); frame.setSize(300, 200); frame.addMouseListener(this); frame.setVisible(true); } public void mouseClicked(MouseEvent e) { Point point = e.getPoint(); System.out.println("Mouse clicked at point: " + point); point = SwingUtilities.convertPoint((Component) e.getSource(), point, label); System.out.println("Point in label coordinates: " + point); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public static void main(String[] args) { new Example2(); } }In this example, we create a Swing application with a JFrame, JPanel, and JLabel. We add a mouse listener to the frame. When the mouse is clicked, we get the point at which the mouse was clicked using the getPoint method of the MouseEvent class. We print out this point to the console. Next, we use the convertPoint method to convert the mouse click point from the frame's coordinate system to the label's coordinate system. We print out the resulting point to the console.