Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

JAVA - panel with one button on it

Status
Not open for further replies.

mr_anderson

Advanced Member level 4
Joined
Jan 23, 2007
Messages
100
Helped
5
Reputation
10
Reaction score
4
Trophy points
1,298
Activity points
2,005
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();
}

}
 

Re: Help in Java

HI
U also need to add action listener of window minimize and window maximize events. In which u also get the mouse location as well.
DO u want that ur mouse and button combitation works when window maximize and not working when its minimize?
if u want this so u also add the focus listener as well.
Thanks
Hamad
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top