Java Learning 58th-GUI

Source: Internet
Author: User

Gui

Graghical user Interface (GUI)

Java provides GUI for objects that exist in java.awt and java.swing packages


Java GUI does but C + + etc, do not intend to waste too much time on this

A simple form demonstration

public static void Main (string[] args) {frame f = new Frame ("new form"); F.setlocation (400, 200);//Set the position of the form f.setsize (500, 400); /Set Window size//f.setbounds (400, 200, 500, 400), function equivalent to the above two sentences f.setlayout (New FlowLayout ());//Set streaming layout button BT = New button ("one button") ; F.add (BT); f.setvisible (true);//Display window}

After displaying the form and finding that the form cannot be closed, theEvent Monitoring Mechanism

its composition: Event sources (components), events, listeners (Listener), event handling (post-event handling)

public static void Main (string[] args) {frame f = new Frame ("new form"); F.setlocation (400, 200);//Set the position of the form f.setsize (500, 400); /Set Window size//f.setbounds (400, 200, 500, 400), function equivalent to the above two sentences f.setlayout (New FlowLayout ());//Set streaming layout button BT = New button ("one button") ; F.add (BT);//Because this event is not closed, to register a listener//form adapter class Windowadapter, all methods have been overridden to create listener F.addwindowlistener (new Windowadapter () { public void windowclosing (WindowEvent e) {system.exit (0);}}); /Add Form Listener f.setvisible (TRUE);//Display window}

A user's action on a component is an event, and the component that generates the event is the source of the event.

ActionListener Demo:

public static void Main (string[] args) {frame f = new Frame ("new form"); F.setlocation (400, 200);//Set the position of the form f.setsize (500, 400); /Set Window size//f.setbounds (400, 200, 500, 400), function equivalent to the above two sentences f.setlayout (New FlowLayout ());//Set streaming layout button BT = New button ("one button") ; F.add (BT);//Because this event is not closed, to register a listener//form adapter class Windowadapter, all methods have been overridden to create listener F.addwindowlistener (new Windowadapter () { public void windowclosing (WindowEvent e) {system.exit (0);}}); /Add a form listener//Add a listener event to the button: Click Exit Bt.addactionlistener (New ActionListener () {@Overridepublic void actionperformed ( ActionEvent e) {//system.out.println ("pressed"); System.exit (0);}); F.setvisible (TRUE);//Display window}

Keyboard and mouse monitoring events

Import Java.awt.button;import java.awt.flowlayout;import Java.awt.frame;import Java.awt.textfield;import Java.awt.event.keyadapter;import Java.awt.event.keyevent;import Java.awt.event.mouseadapter;import Java.awt.event.mouseevent;import Java.awt.event.windowadapter;import Java.awt.event.windowevent;public class Main { Private Frame f;private TextField tf;//text box private Button bt;public Main () {init ();} private void Init () {f = new Frame ("Mouse and Keyboard Listener"), BT = New button ("button"), F.setbounds (n, A, A, a); F.setlayout (New Flowlay Out ()); tf = new TextField (40);//Specify Number of columns F.add (TF); F.add (BT); MyEvent (); f.setvisible (true);} private void MyEvent () {//Add keyboard listener Tf.addkeylistener to text box (new Keyadapter () {@Overridepublic void keypressed (KeyEvent e) {// Press//system.out.println ("Key Pressed:" +e.getkeychar () + ":" +e.getkeycode () + ":" +e.getkeytext (E.getkeycode ()));// Press to print/*int code = E.getkeycode (); Code>=keyevent.vk_0 && Code <= keyevent.vk_9)//judgment {SYSTEM.OUT.PRINTLN ("required number"); E.consume ();//Use, Does not follow the default event handlersType}if (E.getkeycode () ==keyevent.vk_enter) {//press ENTER SYSTEM.OUT.PRINTLN ("ENTER ...");} */if (E.iscontroldown () && E.getkeycode () ==keyevent.vk_enter) {//Press CTRL + ENTER SYSTEM.OUT.PRINTLN ("Crtl Enter .....");}}});/ /Add Exit listener F.addwindowlistener on the form (new Windowadapter () {public void windowclosing (WindowEvent e) {system.exit (0);}}); /Add mouse Listener on button Bt.addmouselistener (new Mouseadapter () {private int count = 1;//counter public void mouseentered (MouseEvent e) {/ /The mouse touches the trigger Tf.settext ("Mouse Enter:" +count++);//information added to the text box}public void mouseclicked (MouseEvent e) {//Click if ( E.getclickcount () ==2) {//Get the number of clicks, double-click SYSTEM.OUT.PRINTLN ("mouseclicked double click");} /*else if (E.getclickcount () ==1) {System.out.println ("mouseclicked only Click");} */}});} public static void Main (string[] args) {new Main ();}}

Not finished



Java Learning 58th-GUI

Related Article

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.