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
| 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.show();
}
}
class ButtonFrame extends JFrame
{ public ButtonFrame()
{ setTitle("ButtonTest");
setSize(300, 200);
JFrame frame = new JFrame("exemple");
final JButton button = new JButton("clic1 ");
final JButton button1 = new JButton("clic2 ");
JPanel p = new JPanel();
p.add(button);
button.addMouseMotionListener(new MouseMotionAdapter()
{
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() ;
}
} );
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
Container contentPane = getContentPane();
contentPane.add(p);
}
} |
Partager