Advertisement
makispaiktis

12. Mouse events - MouseListener and MouseMotionListener

May 30th, 2022 (edited)
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;        // Mouse events
  3. import java.util.logging.Handler;
  4. import javax.swing.*;
  5.  
  6.  
  7. public class Gui extends JFrame{
  8.  
  9.     // Variables
  10.     private JPanel mousepanel;
  11.     private JLabel statusbar;
  12.  
  13.     // Constructor
  14.     public Gui(){
  15.  
  16.         super("JFrame's title");
  17.         // setLayout(new BorderLayout());
  18.         mousepanel = new JPanel();
  19.         mousepanel.setBackground(Color.WHITE);
  20.         add(mousepanel, BorderLayout.CENTER);
  21.  
  22.         statusbar = new JLabel("Default");
  23.         add(statusbar, BorderLayout.SOUTH);
  24.  
  25.         // Implementing 2 classes
  26.         Handlerclass handler = new Handlerclass();
  27.         mousepanel.addMouseListener(handler);
  28.         mousepanel.addMouseMotionListener(handler);
  29.  
  30.  
  31.     }
  32.  
  33.     // New class
  34.     private class Handlerclass implements MouseListener, MouseMotionListener{
  35.  
  36.         // MouseListener has 5 methods
  37.         public void mouseClicked(MouseEvent event){
  38.             statusbar.setText(String.format("Clicked at %d %d", event.getX(), event.getY()));
  39.         }
  40.         public void mousePressed(MouseEvent event){
  41.             statusbar.setText(String.format("Pressed at %d %d", event.getX(), event.getY()));
  42.         }
  43.         public void mouseReleased(MouseEvent event){
  44.             statusbar.setText(String.format("Released at %d %d", event.getX(), event.getY()));
  45.         }
  46.         public void mouseEntered(MouseEvent event){
  47.             // When the mouse enters mousepanel
  48.             statusbar.setText("You entered the area of events");
  49.             mousepanel.setBackground(Color.GREEN);
  50.         }
  51.         public void mouseExited(MouseEvent event){
  52.             // When the mouse enters mousepanel
  53.             statusbar.setText("The mouse has left the window");
  54.             mousepanel.setBackground(Color.RED);
  55.         }
  56.  
  57.         // MouseMotionListener has 2 methods
  58.         @Override
  59.         public void mouseMoved(MouseEvent event) {
  60.             // Without clicking mouse button
  61.             statusbar.setText("You moved the mouse...");
  62.         }
  63.  
  64.         @Override
  65.         public void mouseDragged(MouseEvent event) {
  66.             statusbar.setText("You are dragging your mouse!");
  67.         }
  68.     }
  69.  
  70.     // Main Function
  71.     public static void main(String[] args){
  72.         Gui gui = new Gui();
  73.         gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
  74.         gui.setSize(600, 600);
  75.         gui.setVisible(true);
  76.     }
  77.  
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement