Java AWT Mouse Event Handling In Frame Example

Chapter: AWT Last Updated: 11-06-2016 14:27:38 UTC

Program:

            /* ............... START ............... */
                
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class JavaAWTMouseEventHandlingFrame extends Frame implements MouseListener {

	int x = 0, y = 0;
	String strEvent = "";

	JavaAWTMouseEventHandlingFrame(String title) {
		super(title);
		addWindowListener(new MyWindowAdapter(this));
		addMouseListener(this);
		setSize(300, 300);
		setVisible(true);
	}

	public void mouseClicked(MouseEvent e) {
		strEvent = "MouseClicked";
		x = e.getX();
		y = getY();
		repaint();
	}

	public void mousePressed(MouseEvent e) {
		strEvent = "MousePressed";
		x = e.getX();
		y = getY();
		repaint();

	}

	public void mouseReleased(MouseEvent e) {
		strEvent = "MouseReleased";
		x = e.getX();
		y = getY();
		repaint();

	}

	public void mouseEntered(MouseEvent e) {
		strEvent = "MouseEntered";
		x = e.getX();
		y = getY();
		repaint();

	}

	public void mouseExited(MouseEvent e) {
		strEvent = "MouseExited";
		x = e.getX();
		y = getY();
		repaint();

	}

	public void paint(Graphics g) {
		g.drawString(strEvent + " at " + x + "," + y, 50, 50);
	}

	public static void main(String[] args) {
		JavaAWTMouseEventHandlingFrame window = new JavaAWTMouseEventHandlingFrame("Mouse Event");
	}

}

class MyWindowAdapter extends WindowAdapter {

	JavaAWTMouseEventHandlingFrame window = null;

	MyWindowAdapter(JavaAWTMouseEventHandlingFrame window) {
		this.window = window;
	}

	public void windowClosing(WindowEvent we) {
		window.setVisible(false);
	}
}
                /* ............... END ............... */
        

Output


Java AWT Mouse Event Handling In Frame Example

Tags

AWT Mouse Event Handling In Frame, Java

Similar Programs Chapter Last Updated
Java AWT List Get Selected Index AWT 12-06-2016
Java AWT Get Selected Index From Multi List Select AWT 12-06-2016
Java AWT List Replace Item Example AWT 12-06-2016
Java AWT List Remove An Item AWT 12-06-2016
Java AWT List Remove All Items AWT 12-06-2016
Java AWT List Get Visible Number Of Rows AWT 12-06-2016
Java AWT List Make Item Visible Example AWT 12-06-2016
Java AWT List Insert Item Example AWT 12-06-2016
Java AWT Get Selected Item From Single Selected List AWT 12-06-2016
Java AWT Get Selected Indexes From Multi Select List AWT 12-06-2016
Java AWT Get Item From List AWT 12-06-2016
Java AWT List Multiple Mode Selection Example AWT 11-06-2016
Java AWT List Specifix Index Selection AWT 11-06-2016
Java AWT Multi Select List Example AWT 11-06-2016
Java AWT List Item Count Example AWT 11-06-2016
Java AWT List Example AWT 11-06-2016
Java AWT List Disable Example AWT 11-06-2016
Java AWT List Get All Items Example AWT 11-06-2016
Java AWT Frame Window With Close Event Example AWT 11-06-2016
Java AWT Frame Window Example AWT 11-06-2016
Java AWT Radio Button Checkbox Group Example AWT 08-06-2016
Java AWT Checkbox Hide Example AWT 08-06-2016
Java AWT Checkbox Visible Example AWT 07-06-2016
Java AWT Checkbox Foreground Color Example AWT 07-06-2016
Java AWT Checkbox Enable Example AWT 07-06-2016
Java AWT Checkbox Disable Example AWT 07-06-2016
Java AWT Checked Checkbox Example AWT 07-06-2016
Java AWT Checkbox Font Example AWT 07-06-2016
Java AWT Checkbox Example AWT 07-06-2016
Java AWT Label Visible Example AWT 07-06-2016

1 2