0% found this document useful (0 votes)
16 views

Lecture 14 - Awt - Part 2

Uploaded by

letdoi62
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lecture 14 - Awt - Part 2

Uploaded by

letdoi62
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Topics to be Covered:

• Java MouseListener Interface


• Java MouseMotionListener Interface
• Java WindowListener Interface
• Java Adapter Classes
Java MouseListener Interface
Java MouseListener Interface
The Java MouseListener is notified whenever you change the state of the mouse.

It is notified against MouseEvent. The MouseListener interface is found in java. awt. event
package.

Methods of MouseListener Interface :

1) public abstract void mouseClicked(MouseEvent e);

2) public abstract void mouseEntered(MouseEvent e);

3) public abstract void mouseExited(MouseEvent e);

4) public abstract void mousePressed(MouseEvent e);

5) public abstract void mouseReleased(MouseEvent e);


Java MouseListener Interface: Example 01
import java.awt.*; public void mouseClicked(MouseEve
import java.awt.event.*; nt e)
public class MouseEx1 extends Frame implement {
s MouseListener{ l.setText("Mouse Clicked");
Label l; }
MouseEx1() public void mouseEntered(MouseEve
{ nt e)
addMouseListener(this); {
l=new Label(); l.setText("Mouse Entered");
l.setBounds(20,50,100,20); }
add(l); public void mouseExited(MouseEvent
setSize(300,300); e)
setLayout(null); {
setVisible(true); l.setText("Mouse Exited");
} }
public static void main(String[] args) public void mousePressed(MouseEve
{ nt e)
new MouseEx1(); {
} l.setText("Mouse Pressed");
} }
public void mouseReleased(MouseEv
Java MouseListener Interface: Example 02
import java.awt.*;
import java.awt.event.*; public void mouseEntered(MouseEvent
public class MouseEx2 extends Frame implements e) {}
MouseListener public void mouseExited(MouseEvent
{ e) {}
MouseEx2() public void mousePressed(MouseEve
{ nt e) {}
addMouseListener(this); public void mouseReleased(MouseEv
setSize(300,300); ent e) {}
setLayout(null);
setVisible(true);
} public static void main(String[] args)
public void mouseClicked(MouseEvent e) {
{ new MouseEx2();
Graphics g=getGraphics(); }
g.setColor(Color.BLUE); }
g.fillOval(e.getX(),e.getY(),30,30);
}
Java MouseMotionListener
Interface
Java MouseMotionListener Interface

The Java MouseMotionListener is notified whenever you move or drag mouse.

It is notified against MouseEvent. The MouseMotionListener interface is found in


java.awt.event package.

Methods of MouseMotionListener Interface :

1) public abstract void mouseDragged(MouseEvent e);


2) public abstract void mouseMoved(MouseEvent e);
Java MouseMotionListener Interface: Example 01
import java.awt.*;
import java.awt.event.*;
public class MouseMotionEx1 extends Frame implements MouseMotionListener
{
MouseMotionEx1()
{
addMouseMotionListener(this);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseDragged(MouseEvent e)
{
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),20,20);
}
public void mouseMoved(MouseEvent e) {}
public static void main(String[] args)
{
new MouseMotionEx1();
}
}
Java WindowListener
Interface
Java WindowListener Interface
The Java WindowListener is notified whenever you change the state of window. It is notified
against WindowEvent. The WindowListener interface is found in java.awt.event package.
Methods of WindowListener interface
SNo Method Name Description
1. public abstract void windowActivated It is called when the Window is set to be
(WindowEvent e); an active Window.
2. public abstract void windowClosed It is called when a window has been
(WindowEvent e); closed as the result of calling dispose on
the window.
3. public abstract void windowClosing It is called when the user attempts to
(WindowEvent e); close the window from the system menu
of the window.
4. public abstract void windowDeactivated It is called when a Window is not an
(WindowEvent e); active Window anymore.
5. public abstract void windowDeiconified It is called when a window is changed
(WindowEvent e); from a minimized to a normal state.
6. public abstract void windowIconified It is called when a window is changed
(WindowEvent e); from a normal to a minimized state.
Java WindowListener Interface: Example
import java.awt.*; public void windowClosed (WindowEvent
import java.awt.event.WindowEvent; w) {
import java.awt.event.WindowListener; System.out.println("closed");
public class WindowEx extends Frame implements }
WindowListener public void windowClosing (WindowEven
{ t w) {
WindowExample() System.out.println("closing");
{ dispose();
addWindowListener(this); }
setSize (400, 400); public void windowDeactivated (Window
setLayout (null); Event w){
setVisible (true); System.out.println("deactivated");
} }
public static void main(String[] args) public void windowDeiconified (WindowE
{ vent w) {
new WindowExample(); System.out.println("deiconified");
} }
public void windowActivated (WindowEvent w) public void windowIconified(WindowEve
{ nt w) {
System.out.println("activated"); System.out.println("iconified");
} }
public void windowOpened(WindowEven
Java Adapter Classes
Java Adapter Classes
Java adapter classes provide the default implementation of listener interfaces. If you
inherit the adapter class, you will not be forced to provide the implementation of all the
methods of listener interfaces. So it saves code.

The adapter classes are found in java.awt.event,


java.awt.dnd and javax.swing.event packages.
java.awt.event Adapter classes
SNo Adapter class Listener interface

1. WindowAdapter WindowListener

2. KeyAdapter KeyListener

3. MouseAdapter MouseListener

4. MouseMotionAdapte MouseMotionListener
r
5. FocusAdapter FocusListener

6. ComponentAdapter ComponentListener

7. ContainerAdapter ContainerListener
Java MouseAdapter Class: Example
import java.awt.*;
import java.awt.event.*;
public class MouseAdapterExample extends MouseAdap
ter
{
Frame f;
MouseAdapterExample()
{
f = new Frame ("Mouse Adapter");
f.addMouseListener(this);
f.setSize (300, 300); Output:
f.setLayout (null);
f.setVisible (true);
}
public void mouseClicked (MouseEvent e)
{
Graphics g = f.getGraphics();
g.setColor (Color.BLUE);
g.fillOval (e.getX(), e.getY(), 30, 30);
}
public static void main(String[] args)
{
new MouseAdapterExample();
}
}
Java MouseMotionAdapter Class: Example
import java.awt.*;
import java.awt.event.*;
public class MouseMotionAdapterExample extends MouseMotionAda
pter
{
Frame f; // object of Frame class
MouseMotionAdapterExample()
{
f = new Frame ("Mouse Motion Adapter"); // Setting title
f.addMouseMotionListener (this); // adding MouseMotionListene
r to the Frame Output:
f.setSize (300, 300);
f.setLayout (null);
f.setVisible (true);
}
public void mouseDragged (MouseEvent e)
{
Graphics g = f.getGraphics();

g.setColor (Color.ORANGE); // setting the color of graphics object

g.fillOval (e.getX(), e.getY(), 20, 20); // setting the shape of graphics


object
}
public static void main(String[] args)
Thank You

You might also like