想做一个鼠标拖动控件的效果,应该怎么做。 依次建几个按钮,这样界面上就有一排按钮了。我想用户能够使用鼠标拖动其中的一个按钮到另外的某个按钮上,松开鼠标,这两个按钮的位置互换。可以做到吗?具体如何做,应该使用什么api,继承哪些事件。有代码最好。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 当然用到MoseListener和ActionListener拉...首先记录每个JButton的location.其中鼠标移动的时候可以记下getX()和getY(),拖动按钮时候如果和getX()跟getY相同那就隐藏一个按钮就行拉 让按钮跟着鼠标移动:import java.awt.*;import java.awt.event.*;import javax.swing.*;public class TestDraged extends JFrame { public Button button; Panel panel; static Point origin = new Point(); int x=0; int y=0; public TestDraged() { super("TestMouseDragged"); this.setSize(600,400); button=new Button("Test MouseDragged"); panel=new Panel(); panel.add(button); getContentPane().setLayout(new BorderLayout()); getContentPane().add("Center",panel); button.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { origin.x = e.getX(); origin.y = e.getY(); } }); button.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { Point p = button.getLocation(); button.setLocation(p.x + e.getX() - origin.x, p.y + e.getY()- origin.y); repaint(); } }); } public static void main(String args[]) { TestDraged app=new TestDraged(); app.setVisible(true); app.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); }} 发现一个问题,当拖拽一个按钮的时候,另一个按钮不能得到鼠标消息。只能判断鼠标位置是否在另一个按钮范围内。不过有个问题,如何得到按钮的范围。我现在只能得到按钮的位置,也就是left和top。如何得到它的width和height? 通过drop接口可以判断是否释放到某个button上,应该不需要判断光标位置吧。 通过drop接口可以判断是否释放到某个button上,应该不需要判断光标位置吧。不行,同一时刻,只能有一个按钮捕获鼠标 求助!!!鼠标响应键盘不响应 ecliplse struts 模板配置错误 java中在什么时候可以设置线程优先级? 包内的类如何引用没有包的类 高手请回复,讨论进行中!! 给大家出个题目,娱乐一下:)也散分 数组列表的问题!!! 有实力的进来看看.分不够再送. (SOS高手请进进,指点一二),关于RMI配置的问题: 请问我在那里能找到关于TCP/IP 和 socket 接口的编程书籍? 书上的程序,编译不了! 怎么找不到.class文件呢???
import java.awt.event.*;
import javax.swing.*;public class TestDraged extends JFrame
{
public Button button;
Panel panel;
static Point origin = new Point();
int x=0;
int y=0;
public TestDraged()
{
super("TestMouseDragged");
this.setSize(600,400);
button=new Button("Test MouseDragged");
panel=new Panel();
panel.add(button);
getContentPane().setLayout(new BorderLayout());
getContentPane().add("Center",panel);
button.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
origin.x = e.getX();
origin.y = e.getY();
}
});
button.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
Point p = button.getLocation();
button.setLocation(p.x + e.getX() - origin.x, p.y + e.getY()
- origin.y);
repaint();
}
});
}
public static void main(String args[])
{
TestDraged app=new TestDraged();
app.setVisible(true);
app.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
不行,同一时刻,只能有一个按钮捕获鼠标