Advertisement
zoro-10

MyAdapterClass

Mar 30th, 2024
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.MouseAdapter;
  3. import java.awt.event.MouseEvent;
  4. import javax.swing.*;
  5.  
  6. public class MyAdapterClass {
  7.  
  8.   public static void main(String args[]) {
  9.     SwingUtilities.invokeLater(() -> {
  10.       JFrame frame = new JFrame("MouseAdapter Demo");
  11.       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12.       JLabel label = new JLabel();
  13.       JPanel panel = new JPanel();
  14.       panel.setPreferredSize(new Dimension(300, 200));
  15.       panel.addMouseListener(
  16.         new MouseAdapter() {
  17.           @Override
  18.           public void mouseClicked(MouseEvent e) {
  19.             label.setText(
  20.               "Mouse Clicked at(" + e.getX() + "," + e.getY() + ")"
  21.             );
  22.           }
  23.         }
  24.       );
  25.       panel.add(label);
  26.       frame.add(panel);
  27.       frame.pack();
  28.       frame.setVisible(true);
  29.     });
  30.   }
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement