• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

see this very simple code.

 
nima lama
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello guys,
this is a very simple code, but i am confused can you help me out?
in this code, i wanted to changed the color of the button when mouse entered over the button.but problem with the below code
is that when i move mouse over one button both the button color get changes. so, please see this.
thank you.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class but extends Applet implements MouseListener
{
Button b1,b2;
public void init()
{
b1=new Button("click1");
b2=new Button("click2");

b1.setBackground(Color.gray);
b2.setBackground(Color.gray);

b1.addMouseListener(this);
b2.addMouseListener(this);
add(b1);
add(b2);
}
public void mousePressed(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void mouseClicked(MouseEvent e)
{}
public void mouseEntered(MouseEvent e)
{
/*
how to change the particular button color which has the mouse over it. at present whenever the mouse come over either of the button, both the color changes.
*/

b1.setBackground(Color.cyan);
b2.setBackground(Color.cyan);

}
public void mouseExited(MouseEvent e)
{

b1.setBackground(Color.gray);
b2.setBackground(Color.gray);

}

}
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nima,
Make use of the getSource method of the MouseEvent:

Do the same thing for the mouseEntered and mouseExited and you will be done.
Regards,
Manfred.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic