Java Application Problem
Dear All,
I am trying to write a program called Teaser and it suppose to push the
button away when the crusor is ENTERED. The button suppose to move around in
the Frame and stay in the frame.
My program works but the button does not move around and it hangs in the
left corner of the frame.
Can anyone please help me to solve this problem.
Thanks in advance,
Hamid
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.*;
import java.util.Random;
public class Teaser extends JFrame
{
public Random RND = new Random();
public Teaser()
{
final JButton button = new JButton("OK");
getContentPane().setLayout(new FlowLayout());
getContentPane().add(button);
button.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
int x = RND.nextInt(button.getLocation().x + e.getPoint().y);
int y = RND.nextInt(button.getLocation().y + e.getPoint().x);
button.setLocation (x,y);
}
});
}
public static void main(String[] args)
{
JFrame frame = new Teaser();
frame.setTitle("Teaser");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}
}