Advertisement
zoro-10

AllMouseEvents.java

Mar 30th, 2024
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. import java.awt.Dimension;
  2. import java.awt.event.MouseAdapter;
  3. import java.awt.event.MouseEvent;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6. import javax.swing.JOptionPane;
  7. import javax.swing.JPanel;
  8. import javax.swing.SwingUtilities;
  9.  
  10. public class AllMouseEvents {
  11.  
  12.   public static void main(String args[]) {
  13.     SwingUtilities.invokeLater(() -> {
  14.       JFrame frame = new JFrame("MouseClickEvent Demo");
  15.       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16.       JPanel panel = new JPanel();
  17.       JLabel j1 = new JLabel();
  18.       panel.add(j1);
  19.       panel.setPreferredSize(new Dimension(300, 200));
  20.       panel.addMouseListener(
  21.         new MouseAdapter() {
  22.           @Override
  23.           public void mouseClicked(MouseEvent e) {
  24.             JOptionPane.showMessageDialog(
  25.               frame,
  26.               "Mouse Clicked at(" + e.getX() + "," + e.getY() + ")"
  27.             );
  28.           }
  29.         }
  30.       );
  31.       panel.addMouseMotionListener(
  32.         new MouseAdapter() {
  33.           @Override
  34.           public void mouseMoved(MouseEvent e) {
  35.             j1.setText("Mouse Moved at(" + e.getX() + "," + e.getY() + ")");
  36.           }
  37.         }
  38.       );
  39.       panel.addMouseListener(
  40.         new MouseAdapter() {
  41.           @Override
  42.           public void mouseEntered(MouseEvent e) {
  43.             frame.setTitle("Mouse Entered");
  44.           }
  45.  
  46.           @Override
  47.           public void mouseExited(MouseEvent e) {
  48.             frame.setTitle("Mouse Exited");
  49.           }
  50.         }
  51.       );
  52.       frame.add(panel);
  53.       frame.pack();
  54.       frame.setVisible(true);
  55.     });
  56.   }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement