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