2012年8月23日
package radar;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DragFrame extends JFrame {
private boolean startDrag = false;
private Point p = null;
public static void main(String[] args) {
DragFrame df = new DragFrame();
// df.setUndecorated(true);
df.setAlwaysOnTop(true);
df.setSize(400, 400);
df.setVisible(true);
}
public DragFrame() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
startDrag = true;
p = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
startDrag = false;
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Point p1 = e.getPoint();
Point p2 = getLocation(null);
p2.x += p1.x - p.x;
p2.y += p1.y - p.y;
setLocation(p2);
}
});
}
}
posted @
2012-08-23 20:18 争一代雄风 阅读(132) |
评论 (0) |
编辑 收藏
package com.util;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import javax.swing.JFrame;
public class DragJFrame extends MouseAdapter {
Point loc = null;
Point tmp = null;
boolean isDragged = false;
Frame frame=null;
JFrame jFrame=null;
Component compoent;
/*public DragFrame(Component compoent,Frame frame){
this.compoent=compoent;
this.frame=frame;
this.setDragable(this.compoent,this.frame);
System.out.println("frame");
}*/
/**
* compoent
*/
public DragJFrame(Component compoent,JFrame jFrame){
this.compoent=compoent;
this.jFrame=jFrame;
this.setDragable(this.compoent,this.jFrame);
System.out.println("jframe");
}
private void setDragable(Component compoent, final JFrame jFrame) {
compoent.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent e) {
isDragged = false;
}
public void mousePressed(java.awt.event.MouseEvent e) {
tmp = new Point(e.getX(), e.getY());
isDragged = true;
}
});
compoent.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent e) {
if (isDragged) {
loc = new Point(jFrame.getLocation().x + e.getX() - tmp.x,
jFrame.getLocation().y + e.getY() - tmp.y);
jFrame.setLocation(loc);
}
}
});
}
}
posted @
2012-08-23 20:15 争一代雄风 阅读(128) |
评论 (0) |
编辑 收藏