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);
	}
}
peter_budo commented: Misuse of Code snippet -4

Recommended Answers

All 3 Replies

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

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.

you have to add some JComponent to the JFrame, then add MouseXxxListener to this JComponent

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.