Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

ajeyagv

ajeyagv Avatar

*
New Member

2


December 2011
hey guys I have written a java program which tries to handle mouse events. It should tell constantly where my mouse pointer is, inside the frame that i have created and should print messages like pressed or dragged whenever such events take place. but they are not happening. only frame is created and then nothing else happens when i click the mouse or drag the pointer. So kindly read my code and tell me where i should correct it so that my program works the way i want it to.
i think my problem is that the way I implemented the paintComponent() method is wrong. Kindly advise. here is the code-


import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.io.*;
import java.util.*;
import java.net.*;

public class mouse
{
JFrame frame;
String msg= " here";
int x=0, y=0,i;
public void go()
{
frame= new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(500,500);
frame.addMouseListener(new mouset());
frame.addMouseMotionListener(new mouset());
}

public static void main(String args[])
{
mouse m=new mouse();
m.go();
}
public class mouset implements MouseListener, MouseMotionListener
{

/*mouset()
{
addMouseListener(this);
addMouseMotionListener(this);
}*/
public void mouseClicked(MouseEvent me)
{
x=0;
y=10;
msg="Mouse Clicked";
frame.repaint();
}

public void mouseEntered(MouseEvent me)
{
x=0;
y=10;
msg="Mouse Entered";
frame.repaint();
}

public void mouseExited(MouseEvent me)
{
x=0;
y=10;
msg="Mouse Exited";
frame.repaint();
}

public void mousePressed(MouseEvent me)
{
x=me.getX();
y=me.getY();
msg="Down";
frame.repaint();
}

public void mouseReleased(MouseEvent me)
{
x=me.getX();
y=me.getY();
msg="Up";
frame.repaint();
}

public void mouseDragged(MouseEvent me)
{
x=me.getX();
y=me.getY();
msg="Dragged";
frame.repaint();
}

public void mouseMoved(MouseEvent me)
{
i=0;
/*me.showStatus("Moving Mouse at "+me.getX()+","+me.getY());*/
}
}
public class draw1 extends JPanel{
public void MouseActionPerformed(Graphics g)
{
g.drawString(msg,x,y);
/*putValue(Action.NAME, msg);*/

}
}
}


Eric

Eric Avatar



1,442


November 2005
Well your draw1 class isn't used at all.

The message (msg) is being set, but it's not being put anywhere.

You should use a JLabel, add that to the JFrame, then use setText to assign the new message to it.

ajeyagv

ajeyagv Avatar

*
New Member

2


December 2011
Can I just completely let go of draw1 and instead use jlabel to show the strings ?

Eric

Eric Avatar



1,442


November 2005
ajeyagv Avatar
Can I just completely let go of draw1 and instead use jlabel to show the strings ?
Yeah.

newBookmarkLockedFalling