Move a button with mouse


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Move a button with mouse

  1. #1
    Join Date
    Feb 2005
    Posts
    4

    Move a button with mouse

    //I want to have a button så when I puss the mouse I can move it
    //on the area. I dont know how to add a button as an argument
    //g.drawXXXX(mybutton,Xpos,Ypos). I dont know how to go on. Please
    //help me!


    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;

    public class SnabbKnapp extends JFrame
    {
    JButton btn;


    public SnabbKnapp(){
    JFrame f = new JFrame();

    f.getContentPane().add(new Mintest("test",btn));
    f.setSize(300,300);
    f.setVisible(true);
    f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
    }

    public static void main(String[]args){
    SnabbKnapp sk = new SnabbKnapp();
    }
    }




    class Mintest extends JComponent implements MouseMotionListener
    {
    String meddelande;
    JButton btn;

    int koorX = 50;
    int koorY = 50;


    public Mintest(String s,JButton b){
    meddelande = s;
    addMouseMotionListener(this);


    btn = b;

    JButton btn = new JButton();
    add(btn);
    btn.addMouseMotionListener(this);
    }


    public void paintComponent(Graphics g){
    g.drawString(meddelande,koorX,koorY);
    //g.(bnp,koorX+30,koorY+30);//????????????????????????????????
    }


    public void mouseDragged(MouseEvent e){
    koorX = e.getX();
    koorY = e.getY();
    repaint();
    }

    public void mouseMoved(MouseEvent e){}
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    What shall it be used for, - is it going to behave like a standard button, shall
    it implement methods like addActionListener....

    The thing is, there is no way of adding a button like you descibe, you have to
    make it yourself, paint it with text, background, animate clickbehaviour, the works. The code will have to implement MouseListener,
    MouseMotionListener, dragging logic and paint logic.
    Last edited by sjalle; 03-02-2005 at 10:21 AM.
    eschew obfuscation

  3. #3
    Join Date
    Feb 2005
    Posts
    4
    I want to do a window whit a button so when you push the button it will move in the window and tell me the time it took between the pushes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center