-1

I have posted one code.
please help me i can't get msg at mouse pointer.

please tell me how can i get label at the co-ordinates where the event has taken place.

also tell what is the problem with code..it gives output after minimizing and maximizing.

hope for reply.

Thank you.

/* ---------GTU Examination --- June-2011----Q.4(b)---solution-------*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;

public class mouse_evt extends Frame
{
	public String str = " ";
	public int mX = 0 , mY = 0;
	public Label l;
	
	public mouse_evt()
	{
		setTitle("My frame");
		setLayout(new FlowLayout());
		l = new Label(str + mX + mY );
		l.setLocation(mX,mY);
		add(l);
		addWindowListener(new myWindowAdapter());
		addMouseMotionListener(new mymouselistener(this));
		addMouseListener(new mymouse(this));
	}
	
	public static void main(String args[])
	{
		mouse_evt f = new mouse_evt();
		f.setSize(500,400);
		f.setLocation(450,270);
		f.setVisible(true);
		
	}
}

class myWindowAdapter extends WindowAdapter
{
	public void windowClosing(WindowEvent we)
	{
		System.exit(0);
	}
}
	
class mymouselistener extends MouseMotionAdapter
{
	mouse_evt mt;
	public mymouselistener(mouse_evt mt)
	{
		this.mt = mt;
	}
	public void mouseDragged(MouseEvent me)
	{
		mt.mX = me.getX();
		mt.mY = me.getY();
		mt.str = "Mouse Dragged at : ";
		mt.l.setText(mt.str + mt.mX + " " + mt.mY);
		mt.repaint();
	}
}	

class mymouse extends MouseAdapter
{
	mouse_evt mt;
	public mymouse(mouse_evt mt)
	{
		this.mt = mt;
	}
	
	public void mouseClicked(MouseEvent me)
	{
		mt.mX = me.getX();
		mt.mY = me.getY();
		mt.str = "Mouse clicked at:";
		mt.repaint();
		mt.l.setText(mt.str + mt.mX + "," + mt.mY);		
	}		
	public void mousePressed(MouseEvent me)
	{
		mt.repaint();
		mt.mX = me.getX();
		mt.mY = me.getY();
		mt.str = "Mouse pressed at : ";
		mt.l.setText(mt.str + mt.mX + "," + mt.mY);
	}
       	public void mouseReleased(MouseEvent me)
	{
		mt.repaint();
		mt.mX = me.getX();
		mt.mY = me.getY();
		mt.str = "Mouse Released at : ";
		//System.out.println(mt.str + mt.mX + mt.mY);
		mt.l.setText(mt.str + mt.mX + "," + mt.mY);
	}
}

Edited by peter_budo: Do not use CODE SNIPPET to posts questions! Use Discussion Thread

Votes + Comments
Misuse of Code snippet
4
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by mKorbel
1

mmm ... I don't really get what it is you're trying to do. if you want an action to perform on clicking a label, MouseClicked should be able to do that for you

1

how can i get label at the co-ordinates where the event has taken place.

Look at the Container class's API doc.
It has some methods that could be useful.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.