Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

starter-simple q! 1

Status
Not open for further replies.

sravi

Programmer
Jun 6, 2001
2
IN
I have created a button . now if i try to move the mouse inside that button. I want the button to move somewhere else. How can I do it?
 
check the mouse position when it moves. if its on or about to touch the button, reset the position of the button.
 
Hi sravi,

Take a look at this. Hope you are not trying to play any pranks on anyone :)

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class MoveButtonFrame extends JFrame
{
public MoveButtonFrame()
{
getContentPane().setLayout(null);
setSize(200,200);
MoveButtonPanel mbp = new MoveButtonPanel();
mbp.setBounds(new Rectangle(0,0,200,200));
getContentPane().add(mbp,null);
}
public static void main(String[] args)
{
MoveButtonFrame mbf = new MoveButtonFrame();
mbf.show();
}
}

class MoveButtonPanel extends JPanel implements MouseListener
{
private JButton b1;
private boolean flag;
public MoveButtonPanel()
{
setLayout(null);
setSize(200,200);
b1 = new JButton("Move over me!?!");
b1.setBounds(new Rectangle(0,0,150,20));
b1.addMouseListener(this);
add(b1, null);

flag = true;
}
public void mouseClicked(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
if (flag == true)
{
b1.setBounds(new Rectangle(0,100,150,20));
flag = false;
}
else
{
b1.setBounds(new Rectangle(0,0,150,20));
flag = true;
}
}
public void mouseExited(MouseEvent e)
{
}
}

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top