by Dinesh Thakur Category: Swing

 

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class JMouseActionsJavaExample extends JFrame implements MouseListener
   {
         private final int MAXX = 30;
         private final int STRTX = 20;
         private final int STRTY = 30;
         private int x, y;
         private String message[] = new String[MAXX];
         private int msgCount = 0;
         public JMouseActionsJavaExample()
           {
                setTitle("Mouse Actions in Java With Swing");
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                addMouseListener(this);
           }
                  public void mouseClicked(MouseEvent e)
                      {
                           int WchBtn = e.getButton();
                           String msg;
                           if(msgCount == MAXX)
                           clearScreen();
                           message[msgCount] = "You pressed the mouse.";
                           if(WchBtn == MouseEvent.BUTTON1)
                           msg = "Button One.";
                           else if(WchBtn == MouseEvent.BUTTON1)
                           msg = "Button Two.";
                           else msg = "Button Three.";
                           message[msgCount] = message[msgCount] +" You used " + msg;
                           message[msgCount] = message[msgCount] +" You are at position " +e.getX() + ", " + e.getY() + ".";
                           if(e.getClickCount() == 2)
                           message[msgCount] = message[msgCount] +" You Clicked Double";
                             else
                            message[msgCount] = message[msgCount] +" You Clicked Single";++msgCount;
                            repaint();
                      }
                            public void mouseEntered(MouseEvent e)
                              {
                                   if(msgCount == MAXX)
                                   clearScreen();
                                   message[msgCount] = "You Enterd in Frame";
                                  ++msgCount;
                                   repaint();
                             }
                                   public void mouseExited(MouseEvent e)
                                       {
                                           if(msgCount == MAXX)
                                           clearScreen();
                                           message[msgCount] = "Now You Exit From Frame";
                                           ++msgCount;
                                           repaint();
                                       }
                                           public void mousePressed(MouseEvent e){}
                                           public void mouseReleased(MouseEvent e){}
                                           public void paint(Graphics gr)
                                              {
                                                 final int SIZE = 25;
                                                 super.paint(gr);
                                                 x = STRTX;
                                                 y = STRTY;
                                                 for(int a = 0; a < msgCount; ++a)
                                                 gr.drawString(message[a], x, y += SIZE);
                                              }
                                                 public void clearScreen()
                                                   {
                                                       msgCount = 0;
                                                       for(int a = 0; a < MAXX; ++a)
                                                       message[a] = " ";
                                                       repaint();
                                                   }
                                                       public static void main(String[] as)
                                                            {
                                                                 JMouseActionsJavaExample frm = new JMouseActionsJavaExample();
                                                                 final int WIDTH = 750;
                                                                 final int HEIGHT = 300;
                                                                 frm.setSize(500,500);
                                                                 frm.setVisible(true);
                                                             }
    }

Mouse Actions in Java Swing Example



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles