Advertisement
makispaiktis

13. Adapter class - Override click method only - Counter of clicks

May 30th, 2022 (edited)
1,333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.logging.Handler;
  4. import javax.swing.*;
  5. // Adapter classes is for implemetning a class but overriding only the methods you need
  6.  
  7. public class Gui extends JFrame{
  8.  
  9.     // Variables
  10.     private String details;
  11.     private JLabel statusbar;
  12.  
  13.     // Constructor
  14.     public Gui(){
  15.  
  16.         super("JFrame's title");
  17.         statusbar = new JLabel("This is default");
  18.         add(statusbar, BorderLayout.SOUTH);
  19.         addMouseListener(new Mouseclass());
  20.     }
  21.  
  22.     // New class
  23.     private class Mouseclass extends MouseAdapter{
  24.         // I am allowed to override whatever I want
  25.         public void mouseClicked(MouseEvent event){
  26.             details = String.format("You clicked at %d %d", event.getX(), event.getY());
  27.             details += String.format(" --- Clicks counter =  %d", event.getClickCount());
  28.             if(event.isMetaDown()){
  29.                 // Right mouse button is the meta key
  30.                 details += " --- With right mouse button";
  31.             }
  32.             else if(event.isAltDown()){
  33.                 details += " --- With center mouse button";
  34.             }
  35.             else{
  36.                 details += " --- With left mouse button";
  37.             }
  38.             statusbar.setText(details);
  39.         }
  40.     }
  41.  
  42.     // Main Function
  43.     public static void main(String[] args){
  44.         Gui gui = new Gui();
  45.         gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
  46.         gui.setSize(600, 600);
  47.         gui.setVisible(true);
  48.     }
  49.  
  50. }
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement