如何实现按钮拖拽? 改如何实现像在flash中button的拖拽功能?要以什么作为按钮的容器? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没做过,但是可以提供思路(做过关于Panel的例子)1.产生一个BUTTON2.面板注册鼠标拖拽事件3.对比现有BUTTON和鼠标位置,并进行判断。4.if(true),则删除原来的BUTTON,把现在的BUTTON加到面板上。else则不动 import java.awt.FlowLayout;import java.awt.Point;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.JButton;import javax.swing.JFrame;public class DragButtonTest extends JFrame { private JButton dragBtn; public DragButtonTest() { this.initComponent(); this.getContentPane().setLayout(new FlowLayout()); this.getContentPane().add(this.dragBtn); } private void initComponent() { if (this.dragBtn == null) { this.dragBtn = new JButton("dragBtn"); this.dragBtn.addMouseListener(new MouseAdapter() { private Point oldPoint = new Point(); @Override public void mousePressed(MouseEvent e) { this.oldPoint.x = e.getX(); this.oldPoint.y = e.getY(); } @Override public void mouseReleased(MouseEvent e) { if (this.oldPoint.x != e.getX() || this.oldPoint.y != e.getY()) { DragButtonTest.this.dragBtn.setLocation(new Point( DragButtonTest.this.dragBtn.getLocation().x + e.getX() - this.oldPoint.y, DragButtonTest.this.dragBtn.getLocation().y + e.getY() - this.oldPoint.y)); this.oldPoint.x = e.getX(); this.oldPoint.y = e.getY(); DragButtonTest.this.repaint(); } } @Override public void mouseDragged(MouseEvent e) { } }); } } /** * @param args */ public static void main(String[] args) { DragButtonTest dbt = new DragButtonTest(); dbt.setVisible(true); dbt.setSize(300, 300); dbt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}如果想让按钮跟着鼠标走,那么应该将逻辑放到mouseDragged中,不过这样性能会差一些 行,按照你的代码可以实现拖动了,可是我把mouseReleased中的内容拷到mouseDragged里面,为什么不能实现让按钮跟着鼠标走?而且好像无论把什么内容加到mouseDragged中都没有作用 import java.awt.FlowLayout; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JButton; import javax.swing.JFrame; public class DragButtonTest extends JFrame { private JButton dragBtn; public DragButtonTest() { this.initComponent(); this.getContentPane().setLayout(new FlowLayout()); this.getContentPane().add(this.dragBtn); } private void initComponent() { if (this.dragBtn == null) { this.dragBtn = new JButton("dragBtn"); this.dragBtn.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { private Point oldPoint = new Point(); @Override public void mouseDragged(MouseEvent e) { if (e.getX() - this.oldPoint.x >= 5 || e.getY() - this.oldPoint.y >= 5 || e.getX() - this.oldPoint.x <= -5 || e.getY() - this.oldPoint.y <= -5){ DragButtonTest.this.dragBtn.setLocation(new Point( DragButtonTest.this.dragBtn.getLocation().x + e.getX() - this.oldPoint.x, DragButtonTest.this.dragBtn.getLocation().y + e.getY() - this.oldPoint.y)); DragButtonTest.this.repaint(); } System.out.println("HELLO"); } } ); } } /** * @param args */ public static void main(String[] args) { DragButtonTest dbt = new DragButtonTest(); dbt.setVisible(true); dbt.setSize(300, 300); dbt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } 可实现连续拖拽 gbk转utf-8问题,看了好多帖但均没有很好的答案,请帮帮忙, 如何进行ExecutorService的内存管理? 关于接口的概念问题 有关applet与servlet连接问题 用什么方法把一窜字符窜写成数组? 请问一个关于JMenuBar的问题~!!! 急!!关于JSR82 LocalDevice 获取本地名称 如何实现应用程序自动将主数据库切换到备库? 菜鸟问题:2个语句的区别 安装了Jdk后,如何配置java环境常数? java万年历的问题 求英汉字典的源码 100分
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;import javax.swing.JButton;
import javax.swing.JFrame;public class DragButtonTest extends JFrame { private JButton dragBtn; public DragButtonTest() {
this.initComponent();
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(this.dragBtn);
} private void initComponent() {
if (this.dragBtn == null) {
this.dragBtn = new JButton("dragBtn");
this.dragBtn.addMouseListener(new MouseAdapter() {
private Point oldPoint = new Point(); @Override
public void mousePressed(MouseEvent e) {
this.oldPoint.x = e.getX();
this.oldPoint.y = e.getY();
} @Override
public void mouseReleased(MouseEvent e) {
if (this.oldPoint.x != e.getX() || this.oldPoint.y != e.getY()) {
DragButtonTest.this.dragBtn.setLocation(new Point(
DragButtonTest.this.dragBtn.getLocation().x
+ e.getX()
- this.oldPoint.y,
DragButtonTest.this.dragBtn.getLocation().y
+ e.getY()
- this.oldPoint.y));
this.oldPoint.x = e.getX();
this.oldPoint.y = e.getY();
DragButtonTest.this.repaint();
}
} @Override
public void mouseDragged(MouseEvent e) { }
});
}
} /**
* @param args
*/
public static void main(String[] args) {
DragButtonTest dbt = new DragButtonTest();
dbt.setVisible(true);
dbt.setSize(300, 300);
dbt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}如果想让按钮跟着鼠标走,那么应该将逻辑放到mouseDragged中,不过这样性能会差一些
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import javax.swing.JButton;
import javax.swing.JFrame; public class DragButtonTest extends JFrame { private JButton dragBtn; public DragButtonTest() {
this.initComponent();
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(this.dragBtn);
} private void initComponent() {
if (this.dragBtn == null) {
this.dragBtn = new JButton("dragBtn");
this.dragBtn.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
private Point oldPoint = new Point();
@Override
public void mouseDragged(MouseEvent e) {
if (e.getX() - this.oldPoint.x >= 5 || e.getY() - this.oldPoint.y >= 5
|| e.getX() - this.oldPoint.x <= -5 || e.getY() - this.oldPoint.y <= -5){
DragButtonTest.this.dragBtn.setLocation(new Point(
DragButtonTest.this.dragBtn.getLocation().x
+ e.getX()
- this.oldPoint.x,
DragButtonTest.this.dragBtn.getLocation().y
+ e.getY()
- this.oldPoint.y));
DragButtonTest.this.repaint();
}
System.out.println("HELLO");
}
}
);
}
} /**
* @param args
*/
public static void main(String[] args) {
DragButtonTest dbt = new DragButtonTest();
dbt.setVisible(true);
dbt.setSize(300, 300);
dbt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} }
可实现连续拖拽