Java Learning Note (javase Bi Xiangdong version video) Nine

Source: Internet
Author: User

GUI: Graphical user interface, Java is not commonly used to create desktop applications, so use less

First, overview:

Second, Frame demo:

Third, the event monitoring mechanism

1 ImportJava.awt.Button;2 Importjava.awt.FlowLayout;3 ImportJava.awt.Frame;4 ImportJava.awt.TextField;5 ImportJava.awt.event.KeyAdapter;6 Importjava.awt.event.KeyEvent;7 ImportJava.awt.event.MouseAdapter;8 Importjava.awt.event.MouseEvent;9 ImportJava.awt.event.WindowAdapter;Ten Importjava.awt.event.WindowEvent; One  A  -  Public classFramedemo { -      Public Static voidMain (string[] args) { the demo (); -     } -  -      Public Static voidDemo () { +         //1, create a Frame object, will open a thread -Frame f=NewFRAME ("First frame"); +         //2. Set the size and position of the form AF.setsize (500, 400); atF.setlocation (400, 150); -         //3. Using Flow layout -F.setlayout (NewFlowLayout ()); -         //4. Add Other Components -Button btn=NewButton ("Buttons"); -TextField tf=NewTextField (15); in F.add (TF); - F.add (BTN); to Eventdemo (F,BTN,TF); +         //5. Display Form -F.setvisible (true); the     } *  $      Public Static voidEventdemo (Frame F,button btn,FinalTextField TF) {Panax Notoginseng         /*You can add listeners to component objects, which are objects that handle events and can handle different events differently - * Windowadapter is the adapter object for the form listener the * ActionListener is the active Listener object +          */ AF.addwindowlistener (NewWindowadapter () { the @Override +              Public voidwindowclosing (windowevent e) { -System.exit (0); $             } $         }); - //Btn.addactionlistener (New ActionListener () { - //@Override the //Public void actionperformed (ActionEvent e) { - //system.exit (0);Wuyi //            } the //        }); -         //Mouse Listener WuBtn.addmouselistener (NewMouseadapter () { -             Private intCount=1; About @Override $              Public voidmouseentered (MouseEvent e) -             { -Tf.settext ("" +count++); -             } A  + @Override the              Public voidmouseexited (MouseEvent e) -             { $Tf.settext ("" +count++); the             } the              the         }); the         //Keyboard Listener -Tf.addkeylistener (NewKeyadapter () in         { the             //Some of the keys on the keyboard are display characters, but some are control keys the @Override About              Public voidkeypressed (keyevent e) the             { the //System.out.println (E.getkeychar () + ":" +e.getkeycode ()); the                 intKeycode=E.getkeycode (); +                 if(! (keycode>=keyevent.vk_0&&keycode<=keyevent.vk_9| | keycode==37| | keycode==39| | Keycode==8)){ -                     //prevent execution of default events the E.consume ();BayiSYSTEM.OUT.PRINTLN ("must be a number"); the                 } the             } -         }); -     } the}
frame and Event brief

Java Learning Note (javase Bi Xiangdong version video) Nine

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.