By default, frame or JFrame itself has implemented the ability of the mouse to drag and drop the title bar to move the window.
Just, when you are not satisfied with the Java JFrame style, hide the title bar and border, or simply use the JWindow directly, then how can you implement the mouse to drag and drop the purpose of moving the window? In the beginning, I simply Frame.setlocation (E.getx (), E.gety ()) in the Mousedragged method, and as a result, the frame drags and flashes continuously, and the position continues to bounce on the screen. Later on-line search information, found the answer.
Here is a simple example, one can see:
1 PackageCom.jebysun.test.globalhotkey;2 3 ImportJava.awt.Color;4 ImportJava.awt.Cursor;5 ImportJava.awt.Point;6 Importjava.awt.event.MouseEvent;7 8 ImportJavax.swing.JLabel;9 ImportJavax.swing.JWindow;Ten ImportJavax.swing.event.MouseInputListener; One A /** - * Customize the program window, the mouse can be dragged to move its position. - * @authorJeby Sun the * - */ - Public classMyFrameextendsJWindow { - + Private Static Final LongSerialversionuid = 1L; - + JLabel Titlelbl; A at PublicMyFrame () { - //Setting the background color cannot call its SetBackground method directly, but set its contentpane background color. - This. Getcontentpane (). SetBackground (NewColor (0x99ff66)); - This. SetBounds (100,100,600,400); - This. setlayout (NULL); - inTITLELBL =NewJLabel ("Custom window title bar"); -Titlelbl.setopaque (true); toTitlelbl.setbackground (NewColor (0x66cc00)); +Titlelbl.setbounds (0, 0, 600, 30); - This. Add (TITLELBL); the //Mouse Event Handling class *Mouseeventlistener MouseListener =NewMouseeventlistener ( This); $ Titlelbl.addmouselistener (mouselistener);Panax Notoginseng Titlelbl.addmousemotionlistener (mouselistener); - the This. setvisible (true); + } A the /** + * Mouse Event handling - * @authorJeby Sun $ * $ */ - classMouseeventlistenerImplementsMouseinputlistener { - the Point origin; - //mouse Drag the target component you want to moveWuyi MyFrame frame; the - PublicMouseeventlistener (MyFrame frame) { Wu This. frame =frame; -Origin =NewPoint (); About } $ - @Override - Public voidmouseclicked (MouseEvent e) {} - A /** + * Record the point when the mouse is pressed the */ - @Override $ Public voidmousepressed (MouseEvent e) { theOrigin.x =E.getx (); theORIGIN.Y =e.gety (); the } the - @Override in Public voidmousereleased (MouseEvent e) {} the the /** About * When mouse moves into title bar, set mouse icon to move icon the */ the @Override the Public voidmouseentered (MouseEvent e) { + This. Frame.setcursor (Cursor.getpredefinedcursor (cursor.move_cursor)); - } the Bayi /** the * When the mouse moves out of the title bar, set the mouse icon as the default pointer the */ - @Override - Public voidmouseexited (MouseEvent e) { the This. Frame.setcursor (Cursor.getpredefinedcursor (cursor.default_cursor)); the } the the /** - * When the mouse is dragged in the title bar, set the coordinate position of the window the * New coordinate position of the window = move forward coordinate position + (mouse pointer current coordinates-position of pointer when mouse is pressed) the */ the @Override94 Public voidmousedragged (MouseEvent e) { thePoint P = This. Frame.getlocation (); the This. Frame.setlocation ( theP.x + (E.getx ()-origin.x),98P.y + (E.gety ()-origin.y)); About } - 101 @Override102 Public voidmousemoved (MouseEvent e) {}103 104 } the 106 107 Public Static voidMain (string[] args) {108 NewMyFrame ();109 } the 111}
Mouse drag and Drop mobile Java interface components