Java Learning Class 58th session-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's GUI does a really good job of just C + + and so on. Not going to waste too much time on this.

A simple window Demo

public static void Main (string[] args) {frame f = new Frame ("New Window"); F.setlocation (400, 200);//Set the position of the window 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 (" a button "); F.add (BT); f.setvisible (true);//Display window}

When the window is displayed, the window cannot be closed.Event 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 Window"); F.setlocation (400, 200);//Set the position of the window 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 (" a button "); F.add (BT);//Because the event cannot be shut down. To register a listener//window adapter class Windowadapter, the entire method has been overridden to create a listener f.addwindowlistener (new Windowadapter () {public void windowclosing (WindowEvent e) {system.exit (0);}}); /Join Window Monitor f.setvisible (TRUE);//Display window}

User-to-component operations. is an event. Then 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 Window"); F.setlocation (400, 200);//Set the position of the window 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 (" a button "); F.add (BT);//Because this event is not closed, to register a listener//window adapter class Windowadapter, has covered all methods, convenient to create listener F.addwindowlistener (new Windowadapter () {public void windowclosing (WindowEvent e) {system.exit (0);}}); /Join window 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 Flo Wlayout ()); 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)//Inference {System.out.println ("required number"); E.consume ();//use. Doesn't follow the default.Processing mode}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 .....");}}});/ /Join the Exit Listener on the window F.addwindowlistener (new Windowadapter () {public void windowclosing (WindowEvent e) {system.exit (0);}}); /Add Mouse listener Bt.addmouselistener (new Mouseadapter () {private int count = 1;//counter on button 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 ();}}

About the swing package. You need to install the plugin in Ecplice.


Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

Java Learning Class 58th session-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.