Java Learning Diary---------------------------------operations on JFrame

Source: Internet
Author: User

Set a background picture, add music, monitor the keyboard, change font style, and more!

ImportJava.applet.Applet;ImportJava.applet.AudioClip;Importjava.awt.*;Importjava.awt.event.*;ImportJava.io.File;Importjava.net.MalformedURLException;ImportJava.net.URL;Importjavax.sound.sampled.*;Importjavax.swing.*; Public classPassword_lockextendsJFrameImplementsactionlistener{PrivateJLabel Label1,label2; PrivateJTextField Text_login; PrivateJPasswordField Text_password; PrivateJButton button_login,button_exit,button_q; PrivateJPanel Panel; //private image image = null;    PrivateImageIcon imageicon=NewImageIcon ("D:/java/eclipse/christmas_eve/kulou.jpg"); PrivateJLabel label_background=NewJLabel (ImageIcon);  PublicPassword_lock () {Super("Password Lock");  This. setlocation (0,0);  This. SetSize (Imageicon.geticonwidth (), Imageicon.geticonheight ());  This. setundecorated (true);//go to Windows retouching               /*This.addkeylistener (New Keyadapter () {//panel not recognized, KeyListener needs focus, if you press a button or something * (which usually gives the button the focus), his parent component (such as the PA Nel) cannot capture keyboard events public void keypressed (KeyEvent e) {if (E.getkeycode () ==keyevent.vk_es            CAPE) system.exit (0); }        });*/init (); }         Public voidinit () {Panel=NewJPanel ();  This. Add (Panel, "Center"); //this.setlayout (null);Panel.setlayout (NULL); Label1=NewJLabel ("User name:"); Label1.setbounds (NewRectangle (580,455,90,50)); Label1.setfont (NewFont ("Arial", font.bold,16));        Label1.setforeground (Color.green);                Panel.add (Label1); Text_login=NewJTextField ("Computer 131"); Text_login.seteditable (false); Text_login.setfont (NewFont ("Arial", font.bold,16));        Text_login.setforeground (color.red); Text_login.setbounds (NewRectangle (650,465,110,30));                Panel.add (Text_login); Label2=NewJLabel ("Password:"); Label2.setbounds (NewRectangle (580,495,90,60)); Label2.setfont (NewFont ("Arial", font.bold,16));        Label2.setforeground (Color.green);                Panel.add (Label2); Text_password=NewJPasswordField (); Text_password.setfont (NewFont ("Arial", font.bold,30)); Text_password.setbounds (NewRectangle (650,510,110,30));                Panel.add (Text_password); Button_login=NewJButton ("OK"); Button_login.setbounds (NewRectangle (570,565,90,30)); Button_login.setfont (NewFont ("Arial", font.bold,16));        Button_login.setforeground (color.red); Button_login.addactionlistener ( This); //This.addkeylistener (this);Panel.add (Button_login); Button_exit=NewJButton ("Reset"); Button_exit.setbounds (NewRectangle (695,565,90,30)); Button_exit.setfont (NewFont ("Arial", font.bold,16));        Panel.add (Button_exit); Button_exit.addactionlistener ( This); Button_q=NewJButton ("Exit"); Button_q.setbounds (NewRectangle (0,0,2,5));        Button_q.setbackground (Color.Black);        Panel.add (BUTTON_Q); Button_q.addactionlistener ( This); Toolkit.getdefaulttoolkit (). Addawteventlistener (NewAwteventlistener () {//I'll go, I'm going to have to, add a keyboard. Global Monitoring             Public voideventdispatched (awtevent e) {if(((KeyEvent) e). Getkeychar () = =Keyevent.vk_escape) {System.exit (0); }                if(((KeyEvent) e). Getkeychar () = =keyevent.vk_enter)                {test ();                }}},awtevent.key_event_mask); Label_background.setbounds (NewRectangle (0,0, Imageicon.geticonwidth (), Imageicon.geticonheight ())); //This.getlayeredpane (). Add (label_background);                 This. Getlayeredpane (). Add (Label_background,NewInteger (Integer.min_value));//Add to Getlayeredpane layer of JFrame((JPanel) This. Getcontentpane ()). Setopaque (false);//TransparentPanel.setopaque (false);  This. setvisible (true); }     Public voidactionperformed (ActionEvent e) {//TODO auto-generated Method Stub            if(E.getsource () = =Button_login)    {test (); }    Else if(E.getsource () = =button_q) {System.exit (0); }    Else if(E.getsource () = =button_exit) {Text_password.settext (NULL); }    } Public voidTest () {if(Text_password.gettext (). Equals ("6472")){         This. setvisible (false); NewNewframe (); }    Else{Text_password.settext (NULL); Joptionpane.showmessagedialog ( This, "Password entered incorrectly, please re-enter"); }    } classNewframeextendsJFrameImplementsactionlistener{PrivateImageIcon imageicon1=NewImageIcon ("d:/java/eclipse/christmas_eve/Christmas Eve. jpg"); PrivateJLabel label_background1=NewJLabel (Imageicon1); PrivateJPanel panel_1=NewJPanel (); PrivateJButton button_q=NewJButton ("Exit");  PublicNewframe () {Super("You won!" ");  This. SetBounds (0, 0, Imageicon1.geticonwidth (), Imageicon1.geticonheight ());  This. setundecorated (true); Panel_1.setlayout (NULL);  This. Getcontentpane (). Add (Panel_1); Button_q.setbounds (NewRectangle (0,0,2,5));        Button_q.setbackground (Color.Blue);        Panel_1.add (BUTTON_Q); Button_q.addactionlistener ( This); Playmusic ();//                    //This.getcontentpane (). Add (label_background1);Label_background1.setbounds (NewRectangle (0,0, Imageicon.geticonwidth (), Imageicon.geticonheight ()));  This. Getlayeredpane (). Add (Label_background1,NewInteger (Integer.min_value));//Add to Getlayeredpane layer of JFrame((JPanel) This. Getcontentpane ()). Setopaque (false);//TransparentPanel_1.setopaque (false);  This. setvisible (true); } @Override Public voidactionperformed (ActionEvent e) {//TODO auto-generated Method Stub        if(E.getsource () = =button_q) {System.exit (0); }    }} Public voidPlaymusic () {URL CB=NULL; File F=NewFile ("C:\\users\\xiehe\\music\\wav\\12.wav");//inside the quotes is the absolute deer tendon where the music files are located.        Try{CB=F.tourl (); } Catch(malformedurlexception e) {//TODO auto-generated Catch blockE.printstacktrace ();         } AudioClip AAU; AAU=Applet.newaudioclip (CB); //Aau.play ();Aau.loop (); //Loop play Aau.play () Single aau.stop () Stop playback} Public Static voidMain (string[] args) {NewPassword_lock (); }}

Java Learning Diary---------------------------------operations on JFrame

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.