我在JFrame上放了一个JLabel
在鼠标press这个JLabel时,获得当前点的坐标,和JFrame的位置坐标
然后,drag这个JLabel时候,根据当前点的坐标变化,来设置JFrame的位置...
应该是获得坐标往往是point类型的,不够精确吧...所以,效果非常不理想,不知道各位有啥好办法
按下鼠标时的操作
private void jLabel9MousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
this.setPoint = new Point(evt.getX(), evt.getY());
location = new Point2D.Double(this.getLocation().getX(), this.getLocation().getY());
}
private void jLabel9MouseDragged(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
this.setLocation((int)(this.location.x + (evt.getX() - this.setPoint.x)), (int)(this.location.y + (evt.getY() - this.setPoint.y)));
}
[quote="keating"]楼上正解啊....不过,我觉得有些怪异...是我理解的不对吧?
这样,过程A:一直press鼠标,...
firstPoint是鼠标press的时候,得到的,在过程A中,一直不变;
location是drag时得到的,一直都在变化,当然evt获得的坐标也是不断变化的。
也就是说,location也应该在press的时候获得啊,为什么那样偏偏不对?[/quote]
firstPoint 确切的说应该命名为:currentPoint,因为鼠标按下时就得到当前的鼠标相对于当前窗口的位置。
当你时而drag,时而停下时,firstPoint也是在改变的。
而location是当前窗口左上角相对于整个屏幕的位置。它是根据鼠标的位置变化而变化的,主要是根据鼠标位置变化的偏移量来改变的。拖动窗口是 鼠标位置不断的变化,造成Drag事件不断的促发,也就是不断的调用this.setLocation()这个方法让窗口不断的改变位置。
private void jLabel9MousePressed(java.awt.event.MouseEvent evt) { // TODO add your handling code here: this.setPoint = new Point(evt.getX(), evt.getY()); location = new Point2D.Double(this.getLocation().getX(), this.getLocation().getY()); }
private void jLabel9MouseDragged(java.awt.event.MouseEvent evt) { // TODO add your handling code here: this.setLocation((int)(this.location.x + (evt.getX() - this.setPoint.x)), (int)(this.location.y + (evt.getY() - this.setPoint.y))); }
我使用和你类似的方法,可以很好的实现拖拽窗口功能你参考一下:
首先定义一个全局私有的变量用来存储初始的鼠标位置:
private Point firstPoint = null;
然后在鼠标的MousePressed 事件中初始化
private void formMousePressed(java.awt.event.MouseEvent evt) { // TODO add your handling code here firstPoint =evt.getPoint(); }
最后在鼠标的MouseDragged事件中更新locction
private void formMouseDragged(java.awt.event.MouseEvent evt) { Point location = this.getLocation(); this.setLocation(location .x+(evt.getX()-firstPoint.x),location .y+(evt.getY()-firstPoint.y)); }
我是让整个JFrame监听鼠标的事件,这样就不用使用JLabel了。
private void jLabel9MousePressed(java.awt.event.MouseEvent evt) { // TODO add your handling code here: this.setPoint = new Point(evt.getX(), evt.getY()); location = new Point2D.Double(this.getLocation().getX(), this.getLocation().getY()); }
private void jLabel9MouseDragged(java.awt.event.MouseEvent evt) { // TODO add your handling code here: this.setLocation((int)(this.location.x + (evt.getX() - this.setPoint.x)), (int)(this.location.y + (evt.getY() - this.setPoint.y))); }
我使用和你类似的方法,可以很好的实现拖拽窗口功能你参考一下:
首先定义一个全局私有的变量用来存储初始的鼠标位置:
private Point firstPoint = null;
然后在鼠标的MousePressed 事件中初始化
private void formMousePressed(java.awt.event.MouseEvent evt) { // TODO add your handling code here firstPoint =evt.getPoint(); }
最后在鼠标的MouseDragged事件中更新locction
private void formMouseDragged(java.awt.event.MouseEvent evt) { Point location = this.getLocation(); this.setLocation(location .x+(evt.getX()-firstPoint.x),location .y+(evt.getY()-firstPoint.y)); }
我是让整个JFrame监听鼠标的事件,这样就不用使用JLabel了。
我按照上面的试了一下,怎么不管用呢
public class TestJFrame extends JFrame { private Point firstPoint = null; public void startFrame() { setTitle("Test Drag"); setSize(200, 100); addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { formMousePressed(e); } @Override public void mouseDragged(MouseEvent e) { formMouseDragged(e); } }); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); setVisible(true); } private void formMousePressed(java.awt.event.MouseEvent evt) { // TODO add your handling code here firstPoint = evt.getPoint(); } private void formMouseDragged(java.awt.event.MouseEvent evt) { Point location = this.getLocation(); this.setLocation(location.x + (evt.getX() - firstPoint.x), location.y + (evt.getY() - firstPoint.y)); } public static void main(String[] args) { new TestJFrame().startFrame(); } }