mr_anderson
Advanced Member level 4
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 to its original location ?
may I know why this is happening?
and how to stop it?
Best Regards.
this is the 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
// 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();
}
}
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 to its original location ?
may I know why this is happening?
and how to stop it?
Best Regards.
this is the 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
// 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();
}
}