Java automatic prompt box automatically disappears and click disappears gradient effect

Source: Internet
Author: User

 

Import java. AWT. borderlayout;
Import java. AWT. color;
Import java. AWT. dimension;
Import java. AWT. Font;
Import java. AWT. gradientpaint;
Import java. AWT. graphics;
Import java. AWT. graphics2d;
Import java. AWT. Point;
Import java. AWT. renderinghints;
Import java. AWT. event. actionevent;
Import java. AWT. event. actionlistener;
Import java. AWT. event. keyadapter;
Import java. AWT. event. keyevent;
Import java. AWT. event. mouseevent;

Import javax. Swing. imageicon;
Import javax. Swing. jbutton;
Import javax. Swing. jdialog;
Import javax. Swing. jframe;
Import javax. Swing. jlabel;
Import javax. Swing. jpanel;
Import javax. Swing. event. mouseinputadapter;

Public class myjdialog extends jdialog {
/**
*
*/
Private Static final long serialversionuid = 1l;
Public static final Boolean noautoclose = true;
Private dimension size = new dimension (200,120 );

// The option window that won't auto close Constructor
Public myjdialog (Boolean donotautoclose, jframe parentjframe,
String message ){
Super (parentjframe, true );
This. setundecorated (true );
Size. width = 280;
Size. Height = 140;
Setsize (size );

// Due to the unexpected condition happened in the underground panel,
// So move the move listeners into the Dialog
Mouseinputadaptert mouselisener = new mouseinputadaptert ();
This. addmouselistener (mouselisener );
This. addmousemotionlistener (mouselisener );

// Set the location
If (parentjframe! = NULL ){
This. setlocation (parentjframe. getlocationonscreen (). x
+ (Parentjframe. getsize (). width/2)-size. width/2,
Parentjframe. getlocationonscreen (). Y
+ (Parentjframe. getsize (). Height/2)-size. Height
/2 );
} Else {
This. setlocation (500,300 );
}

// The Underground panel
Undergroundpanel mainpanel = new undergroundpanel ();
Mainpanel. setlayout (null );

// The close button and confirm button
Jbutton closebutton = new jbutton (New imageicon ("images/close1.jpg "));
Closebutton. setroalow.con (New imageicon ("images/close2.jpg "));
Closebutton. setpressedicon (New imageicon ("images/close3.jpg "));
Closebutton. setfocusable (false );
Closebutton. setborderpainted (false );
Closebutton. setbounds (this. getbounds (). Width-51, 7, 36, 18 );
Closebuttonlistener cblistener = new closebuttonlistener ();
Closebutton. addactionlistener (cblistener );
Mainpanel. Add (closebutton );

Jbutton confirmbutton = new jbutton ("OK ");
Confirmbutton. setlocation (size. width/2-50, size. Height-50 );
Confirmbutton. setfocusable (true );
Confirmbutton. requestfocus ();
Confirmbutton. addkeylistener (New keyadapter (){
Public void keypressed (keyevent e ){
If (E. getkeycode () = keyevent. vk_enter ){
Close ();
}
}
});
// Confirmbutton. setmnemonic (keyevent. vk_enter );
Confirmbutton. addactionlistener (cblistener );
Mainpanel. Add (confirmbutton );

// The message label.
// Handle the string according to the length to show the complete
// Message.
If (message. Length ()> 10 ){
String message1 = message, message2;
Message1 = message. substring (0, 9 );
Message2 = message. substring (9, message. Length ());
// Show the two message labels
Jlabel messagelabel1 = new jlabel (message1 );
Messagelabel1.setforeground (color. Black );
Messagelabel1.setfont (new font ("", Font. Bold, 18 ));
Messagelabel1.setbounds (size. width/2
-Messagelabel1.getpreferredsize (). width/2, size. Height
/2-messagelabel1.getpreferredsize (). Height/2-25,
Messagelabel1.getpreferredsize (). Width, messagelabel1
. Getpreferredsize (). Height );
Mainpanel. Add (messagelabel1, borderlayout. center );

Jlabel messagelabel2 = new jlabel (message2 );
Messagelabel2.setforeground (color. Black );
Messagelabel2.setfont (new font ("", Font. Bold, 18 ));
Messagelabel2.setbounds (size. width/2
-Messagelabel2.getpreferredsize (). width/2, size. Height
/2-messagelabel2.getpreferredsize (). Height/2 + 4,
Messagelabel2.getpreferredsize (). Width, messagelabel2
. Getpreferredsize (). Height );
Mainpanel. Add (messagelabel2, borderlayout. center );

} Else {
Jlabel messagelabel = new jlabel (Message );
Messagelabel. setforeground (color. Black );
Messagelabel. setfont (new font ("", Font. Bold, 18 ));
Messagelabel. setbounds (size. width/2
-Messagelabel. getpreferredsize (). width/2, size. Height
/2-messagelabel. getpreferredsize (). Height/2-12,
Messagelabel. getpreferredsize (). Width, messagelabel
. Getpreferredsize (). Height );
Mainpanel. Add (messagelabel, borderlayout. center );
}

This. getcontentpane (). Add (mainpanel );

// Set the window transparent
Com. Sun. AWT. awtutilities. setwindowopaque (this, false );

Setvisible (true );

}

// The auto closing option window Constructor
Public myjdialog (jframe parentjframe, string message ){
Super (parentjframe );
This. setundecorated (true );
Setsize (size );
If (parentjframe! = NULL ){
This. setlocation (parentjframe. getlocationonscreen (). x
+ (Parentjframe. getsize (). width/2)-size. width/2,
Parentjframe. getlocationonscreen (). Y
+ (Parentjframe. getsize (). Height/2)-size. Height
/2 );
} Else {
This. setlocation (500,300 );
}
// The Underground panel
Undergroundpanel mainpanel = new undergroundpanel ();

// The message label
Jlabel messagelabel = new jlabel (Message );
// Messagelabel. setmaximumsize (new dimension (220,170 ));
Messagelabel. setforeground (color. Black );
Messagelabel. setfont (new font ("", Font. Bold, 18 ));
Messagelabel. setbounds (size. width/2
-Messagelabel. getpreferredsize (). width/2, size. Height/2
-Messagelabel. getpreferredsize (). Height/2-6, messagelabel
. Getpreferredsize (). Width,
Messagelabel. getpreferredsize (). Height );

Mainpanel. setlayout (null );

Mainpanel. Add (messagelabel, borderlayout. center );

Setlayout (New borderlayout ());
Add (mainpanel, borderlayout. center );

// Set the window transparent
Com. Sun. AWT. awtutilities. setwindowopaque (this, false );

// Display the gradient effect
Float translucent = 0.01f;
Com. Sun. AWT. awtutilities. setwindowopacity (this, 0.0f );
Setvisible (true );
While (translucent <1 ){
Com. Sun. AWT. awtutilities. setwindowopacity (this, translucent );
Translucent + = 0.04f;
}
//........................

// Stop some time for lew.user get the message
Try {
Thread. Sleep (1300 );
} Catch (interruptedexception e ){
// Auto-generated Catch Block
E. printstacktrace ();
}
Close ();

}

/*
* Public void setvisible (Boolean in ){
*
*
*
*}
*/

// Close the window
Public void close (){
// Gradient effect
Float translucent = 1.0f;
While (translucent> 0 ){
Com. Sun. AWT. awtutilities. setwindowopacity (this, translucent );
Translucent-= 0.04f;
}
//........................
Dispose ();

}

Private class undergroundpanel extends jpanel {

Private Static final long serialversionuid = 1l;

// Paint the underground
Protected void paintcomponent (Graphics g ){
Graphics2d g2 = (graphics2d) g;
Int width = getwidth ();
Int Height = getheight ();
Gradientpaint gradient = new gradientpaint (0.0f, 0.0f, new color (
230,230,230,230), 0.0f, (float) height, new color (230,
230,230,230 ));

G2.setpaint (gradient );

// Flexible tailoring
G2.setrenderinghint (renderinghints. key_antialiasing,
Renderinghints. value_antialias_on );

// Draw the background
G2.fillroundrect (7, 7, width-14, height-14, 20, 20 );

// Draw the border lines
G2.setcolor (new color (2, 10,170 ));
G2.drawroundrect (6, 6, width-14, height-14, 20, 20 );
G2.setcolor (new color (77, 82,170,200 ));
G2.drawroundrect (5, 5, width-12, height-12, 21, 21 );
G2.setcolor (new color (77, 82,170,160 ));
G2.drawroundrect (4, 4, width-10, height-10, 22, 22 );
G2.setcolor (new color (77, 82,170,120 ));
G2.drawroundrect (3, 3, width-8, height-8, 23, 23 );
G2.setcolor (new color (77, 82,170, 80 ));
G2.drawroundrect (2, 2, width-6, height-6, 24, 24 );
G2.setcolor (new color (77, 82,170, 40 ));
G2.drawroundrect (1, 1, width-4, height-4, 25, 25 );
G2.setcolor (new color (77, 82,170, 10 ));
G2.drawroundrect (0, 0, width-2, height-2, 26, 26 );

}

}

Private class closebuttonlistener implements actionlistener {

@ Override
Public void actionreceivmed (actionevent arg0 ){
Close ();

}

}

// Listen and implement the moving of the dialog
Private class mouseinputadaptert extends mouseinputadapter {
Private point oldcursorposition;

Public void mousedragged (mouseevent e ){
Point tempscreen = E. getlocationonscreen ();
// Change the position of the frame
Myjdialog. This. setlocation (myjdialog. This. getlocationonscreen (). x
+ Tempscreen. X-oldcursorposition. X,
Myjdialog. This. getlocationonscreen (). Y + tempscreen. Y
-Oldcursorposition. y );
Oldcursorposition = tempscreen;
}

Public void mousepressed (mouseevent e ){
// Record the last cursor position
Oldcursorposition = E. getlocationonscreen ();
}

}

Public static void main (string [] ARGs ){
Myjdialog test = new myjdialog (myjdialog. noautoclose, null, "ddddddddddd ");
Myjdialog Test2 = new myjdialog (null, "ddddddddd ");

}
}

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.