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
}
}
在方法体中还需要写些什么?
拖动按钮时总是不对
帮帮忙
----------------解决方案--------------------------------------------------------
你想达到什么效果!!
----------------解决方案--------------------------------------------------------
鼠标拖动事件就是mouseDragged
----------------解决方案--------------------------------------------------------
我也说不清楚.你们运行一下就知道了
拖动时按钮的移动不对劲
----------------解决方案--------------------------------------------------------
确实,不知道该怎么改!!
----------------解决方案--------------------------------------------------------
[CODE]import java.awt.Container;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
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);
this.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());
SwingUtilities.updateComponentTreeUI(this);//加这个,强制更新组件树
}
/**
* @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
}
}[/CODE]
----------------解决方案--------------------------------------------------------
加了这句还是不行啊
移动起来还是不对……
SwingUtilities.updateComponentTreeUI(this);//加这个,强制更新组件树
----------------解决方案--------------------------------------------------------
你想怎么移动?
[CODE]import java.awt.Container;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
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){
Point p=button.getLocation();
button.setLocation(p.x+e.getX(),p.y+e.getY());
SwingUtilities.updateComponentTreeUI(this);
}
/**
* @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
}
}[/CODE]
试试这个,自动以左上角定位
----------------解决方案--------------------------------------------------------
我知道我哪儿错了
谢谢
----------------解决方案--------------------------------------------------------