实现可拖动的窗口,实现拖动窗口,public class
分享于 点击 2292 次 点评:138
实现可拖动的窗口,实现拖动窗口,public class
public class MoveMouseListener implements MouseListener, MouseMotionListener { JComponent target; JFrame frame; public MoveMouseListener(JComponent target, JFrame frame) { this.target = target; this.frame = frame; } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} public void mouseDragged(MouseEvent e) { frame.setLocation(new Point(e.getX( ),e.getY( )); } Point getScreenLocation(MouseEvent e) { Point cursor = e.getPoint( ); Point target_location = this.target.getLocationOnScreen( ); return new Point( (int)(target_location.getX( )+cursor.getX( )), (int)(target_location.getY( )+cursor.getY( ))); } Point start_drag; Point start_loc; public void mousePressed(MouseEvent e) { this.start_drag = this.getScreenLocation(e); this.start_loc = this.getFrame(this.target).getLocation( ); } public void mouseDragged(MouseEvent e) { Point current = this.getScreenLocation(e); Point offset = new Point( (int)current.getX( )-(int)start_drag.getX( ), (int)current.getY( )-(int)start_drag.getY( )); JFrame frame = this.getFrame(target); Point new_location = new Point( (int)(this.start_loc.getX( )+offset.getX( )), (int)(this.start_loc.getY( )+offset.getY( ))); frame.setLocation(new_location); } public static void main(String[] args) { JFrame frame = new JFrame( ); CalendarHack ch = new CalendarHack( ); ch.setDate(new Date( )); frame.getContentPane( ).add(ch);frame.setUndecorated(true); MoveMouseListener mml = new MoveMouseListener(ch, frame); ch.addMouseListener(mml); ch.addMouseMotionListener(mml); frame.pack( ); frame.setVisible(true); } }//该片段来自于http://byrx.net
用户点评