[Solved] Tiny Problem with JFrame
Hi
I am writing a simple program in java which is simply:
an panel with one button on it, when you drag the mouse over the panel, the button should follow the mouse ( it is very simple ), the program is working fine, but when I minimize the window or maximize it, the button jumps back to its original location ?
may I know why this is happening?
and how to solve it?
Best Regards.
this is the code:
Code:
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
public class MoveButtons extends JFrame{
private JButton move=new JButton("move");
JPanel panel=new JPanel();
// a listener is added to the panel, when the mouse is dragged over the panel the button "move" is set to to location of the mouse the problem is that when I minimize the window and restore it, the button "move" jump to its original location this problem occurs also when I maximize the window
public MoveButtons(){
panel.add(move);
panel.addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
move.setLocation(e.getX(),e.getY());
}
public void mouseMoved(MouseEvent e) {
}
});
add(panel);
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
setVisible(true);
}
public static void main(String [] args){
new MoveButtons();
}
}
Moderator Edit: Code tags added