Java floating window Effect

Source: Internet
Author: User

Import java. AWT .*;
Import java. AWT. event. mouseevent;

Import javax. Swing. jframe;
Import javax. Swing. jlabel;
Import javax. Swing. event. mouseinputadapter;

Public class undecoratedframe extends jframe
{
Private Static final int title_height = 20;

Public undecoratedframe () throws headlessexception
{
Super ();
Setundecorated (true );

Mousehandler ML = new mousehandler ();
Addmouselistener (ML );
Addmousemotionlistener (ML );
}

Public undecoratedframe (String title) throws headlessexception
{
Super (title );
Setundecorated (true );

Mousehandler ML = new mousehandler ();
Addmouselistener (ML );
Addmousemotionlistener (ML );
}

Public insets getinsets ()
{
Return new insets (title_height, 1, 1, 1 );
}

Public void paint (Graphics g)
{
Super. Paint (g );
G. setcolor (new color (0, 0,128 ));
G. drawrect (0, 0, getwidth ()-1, getheight ()-1 );
G. fillrect (0, 0, getwidth (), title_height );

Fontmetrics fm = G. getfontmetrics ();
G. setcolor (color. White );
G. drawstring (gettitle (), 2, (title_height-FM. getheight ()/2 + FM. getascent ());
}

Private class mousehandler extends mouseinputadapter
{
Private point;

Public void mousepressed (mouseevent E)
{
If (E. Gety () <= title_height ){
This. Point = E. getpoint ();
}
}

Public void mousedragged (mouseevent E)
{
If (point! = NULL ){
Setcursor (cursor. getpredefinedcursor (cursor. move_cursor ));

Point P = E. getpoint ();
Int dx = p. x-point. X;
Int DY = P. Y-point. Y;

Int x = getx ();
Int y = Gety ();
Setlocation (x + dx, Y + dy );
}
}

Public void mousereleased (mouseevent E)
{
Point = NULL;
Setcursor (cursor. getdefacurcursor ());
}
}

Public static void main (string [] ARGs)
{
Jframe F = new undecoratedframe ("undecorated frame ");
F. getcontentpane (). Add (New jlabel ("Hello world! ", Jlabel. Center), borderlayout. center );
F. setsize (400,400 );
F. setlocationrelativeto (null );
F. setdefaclocloseoperation (jframe. exit_on_close );
F. setvisible (true );
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.