import javax.swing.*; public class Example { public static void main(String[] args) { JFrame frame = new JFrame("Example"); JPanel panel = new JPanel(); JButton button = new JButton("Button"); panel.add(button); frame.add(panel); frame.pack(); frame.setVisible(true); Point buttonLocation = button.getLocation(); Point screenLocation = SwingUtilities.convertPointToScreen(buttonLocation, button); System.out.println("Button Location: " + buttonLocation); System.out.println("Screen Location: " + screenLocation); } }
import javax.swing.*; import java.awt.event.*; public class Example { public static void main(String[] args) { JFrame frame = new JFrame("Example"); JPanel panel = new JPanel(); JButton button = new JButton("Button"); panel.add(button); frame.add(panel); frame.pack(); frame.setVisible(true); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Point buttonLocation = button.getLocation(); Point screenLocation = SwingUtilities.convertPointToScreen(buttonLocation, button); JOptionPane.showMessageDialog(null, "Button Screen Location: " + screenLocation); } }); } }This example creates a `JFrame` with a `JButton` on it. When the button is clicked, the code gets the location of the button and converts it to screen coordinates. The screen location is then displayed in a message dialog using `JOptionPane.showMessageDialog`. Both examples use the `java.swing` package library.