Mouse listeners Assignment Help

Assignment Help: >> Component listeners in Java >> Mouse listeners

Mouse listeners:

An important input device when using a graphical user interface is the mouse: it is used to point with, it can be clicked, it can 'drag and drop' items, and so on. In Java there are two interfaces that can be used for mouse events: MouseMotionListener and MouseListener. The former defines two events concerned with dragging and moving the mouse. However, we will concentrate on the second interface, the MouseListener,

which defines five methods that are used for monitoring: 

  • when a mouse button is clicked public abstract void mouseClicked (MouseEvent m)
  • when a mouse enters a component public abstract void mouseEntered (MouseEvent m)
  • when a mouse exits a component public abstract void mouseExited (MouseEvent m)
  • when a mouse is pressed public abstract void mousePressed (MouseEvent m)
  • when a mouse button is released public abstract void mouseReleased (MouseEvent m)

Any listener that wants to react to any of the above events must implement the MouseListener interface, which contains the definition of the above five methods. An example of the use of this interface is shown below:

import java.awt. *;
import java.awt.event. *;
import javax.swing. *;

public class MouseFrame extends JFrame
{
private JLabel yCoordLabel;

private JLabel xCoordLabel;

private JPanel c;

public MouseFrame (String title)
{
super(title);

setSize(300, 300);

yCoordLabel = new JLabel("");

xCoordLabel = new JLabel("");

c = new JPanel( );

c.setBackground(Color.yellow);

getContentPane( ).setLayout(new GridLayout(3, 1));

getContentPane( ).add(yCoordLabel);

getContentPane( ).add(xCoordLabel);

getContentPane( ).add(c);

c.addMouseListener(new MouseWatcher( ));
}

private class MouseWatcher implements MouseListener

{
public void mouseClicked (MouseEvent e)
{
int xCoordinate = e.getX( );

int yCoordinate = e.getY( );

yCoordLabel.setText(yCoordinate + "");

xCoordLabel.setText(xCoordinate + "");
}

public void mouseEntered (MouseEvent e)
{
}

public void mouseReleased (MouseEvent e)
{
}

public void mousePressed (MouseEvent e)
{
}

public void mouseExited (MouseEvent e)
{
}
}
}

public class MouseFrameTest
{
public static void main (String [] args)
{
MouseFrame world = new MouseFrame("Mouse Watcher");
world.setVisible(true);
}
}

The code defines a window that contains two labels and a panel, with the panel being coloured yellow (this is achieved by means of the method setBackground, which uses the static variable Color.yellow). When the mouse is clicked within the panel the labels display the x- and y-coordinates of the point within the panel that the mouse was clicked in. The methods getX and getY from the class MouseEvent are used to retrieve these values from the MouseEvent argument. Figure (overleaf) shows the program working.

732_mouse listener.png

The class MouseFrame provides implementations for the five methods that are contained in MouseListener. However, since we are not reacting to four of the events, the code for the four corresponding methods is empty.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Mouse listeners homework and assignments? Live Mouse listeners experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Mouse listeners homework help, java assignment help and Mouse listeners projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd