1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
   |  
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class Hy
{
	public static void main(String[] args)
	{
		JFrame frame = new ButtonFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}
 
class ButtonFrame extends JFrame implements MouseMotionListener
{
	private final JButton button; 
	private JPanel p;
 
	public void mouseMoved(MouseEvent e)
	{
		int x=e.getX();
		int y=e.getY();
 
		if (button.contains(x,y) ){button.setBackground(Color.blue);}
		else {button.setBackground(Color.red);}
		repaint() ;
 
	}
	public void mouseDragged(MouseEvent e)
	{}
 
 
	public ButtonFrame()
	{ 
	setTitle("ButtonTest");
	setSize(300, 200);
 
	button = new JButton("clic1 ");	
 
	p = new JPanel();
	p.add(button);
	p.addMouseMotionListener(this);
 
	button.addMouseMotionListener(this);
 
	addWindowListener(new WindowAdapter()
			{ public void windowClosing(WindowEvent e)
			{ System.exit(0);
			}
			} );
 
 
	Container contentPane = getContentPane();
	contentPane.add(p);
	}
} |