Java examples
Home
Welcome to
Java Examples
Take a cup of tea and Let's Start programming
Showing posts with label
windowadapter
.
Show all posts
Showing posts with label
windowadapter
.
Show all posts
Write a program to create a frame with exit capabilities.
Handle events for mouse pressed, mouse released, mouse clicked
and mouse dragged by displaying appropriate message describing
the event at the coordinates where the event has taken place..
import java.awt.*; import java.awt.event.*; class frame_window extends Frame implements MouseListener,MouseMotionListener { String strEvent= ""; int x=0,y=0; frame_window(String title) { super(title); addWindowListener(new MyWindowAdapter(this)); addMouseListener(this); addMouseMotionListener(this); setSize(500,500); setVisible(true); setLocation(300,100); } public void mouseClicked(MouseEvent me) { strEvent = "mouse clicked "; x = me.getX(); y = me.getY(); repaint(); } public void mousePressed(MouseEvent me) { strEvent = "mouse pressed "; x = me.getX(); y = me.getY(); repaint(); } public void mouseReleased(MouseEvent me) { strEvent = "mouse released "; x = me.getX(); y = me.getY(); repaint(); } public void mouseEntered(MouseEvent me){} public void mouseExited(MouseEvent me){} public void mouseDragged(MouseEvent me) { strEvent = "mouse dragged "; x = me.getX(); y = me.getY(); repaint(); } public void mouseMoved(MouseEvent me){} public void paint(Graphics g) { g.drawString(strEvent + "at " + x +","+ y,x,y); } public static void main(String [] args) { frame_window j = new frame_window("frame window...."); } } class MyWindowAdapter extends WindowAdapter { frame_window j = null; MyWindowAdapter(frame_window j) { this.j = j; } public void windowClosing(WindowEvent we) { System.exit(0); } }
output
Older Posts
Home
Search Form
Popular Posts
Design a class named Fan to represent a fan....
abstract class
program to display employee who have salary more than 20000
Define the Rectangle class
Logic behind pattern...