-
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){}
}
-
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 09:21 AM.
eschew obfuscation
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks