import java.awt.Container;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
public class MotionTest extends JFrame implements MouseMotionListener{
JButton button = new JButton("ok");
public MotionTest()
{
init();
}
public void init()
{
Container con = this.getContentPane();
con.setLayout(null);
button.setBounds(100,100,50,30);
button.setMargin(new Insets(1,1,1,1));
con.add(button);
button.addMouseMotionListener(this);
this.setTitle("鼠标拖动事件");
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void mouseDragged(MouseEvent e){
button.setLocation(e.getX(),e.getY());
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MotionTest();
}
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}
在方法体中还需要写些什么?
拖动按钮时总是不对
帮帮忙