JAVA learning lesson 58th-GUI, javagui

Source: Internet
Author: User

JAVA learning lesson 58th-GUI, javagui

GUI

Graghical User Interface (graphical User Interface)

The objects provided by java for the GUI are included in the java. awt and java. swing packages.


Java's GUI does not do anything like C ++, and does not waste too much time on it.

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 the window size // f. setBounds (400,200,500,400); the function is equivalent to the above two sentences. setLayout (new FlowLayout (); // sets the streaming layout. Button bt = new Button ("one Button"); f. add (bt); f. setVisible (true); // display window}

The event listening mechanism is used when the form cannot be closed.

Composition:Event source (component), Event (Event), Listener (Listener), Event processing (post-processing method for events)

 

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 the window size // f. setBounds (400,200,500,400); the function is equivalent to the above two sentences. setLayout (new FlowLayout (); // sets the streaming layout. Button bt = new Button ("one Button"); f. add (bt); // because the event cannot be closed, you need to register a listener // form adapter class WindowAdapter, which has covered all methods and is convenient for creating the listener f. addWindowListener (new WindowAdapter () {public void windowClosing (invalid wevent e) {System. exit (0) ;}}); // Add a form to listen to f. setVisible (true); // display window}

If a user operates on a component, it is an event, and the component that generates the event is the event source.

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 the window size // f. setBounds (400,200,500,400); the function is equivalent to the above two sentences. setLayout (new FlowLayout (); // sets the streaming layout. Button bt = new Button ("one Button"); f. add (bt); // because the event cannot be closed, you need to register a listener // form adapter class WindowAdapter, which has covered all methods and is convenient for creating the listener f. addWindowListener (new WindowAdapter () {public void windowClosing (invalid wevent e) {System. exit (0) ;}}); // Add a form listener // Add a listener event on the button: Click it to exit bt. addActionListener (new ActionListener () {@ Overridepublic void actionreceivmed (ActionEvent e) {// System. out. println ("pressed"); System. exit (0) ;}}); f. setVisible (true); // display window}

Keyboard and mouse listening 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. using wevent; 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 (400,200,500,400); f. setLayout (new FlowLayout (); tf = new TextField (40); // specify the number of columns f. add (tf); f. add (bt); myEvent (); f. setVisible (true);} private void myEvent () {// Add a keyboard listener tf to the text box. addKeyListener (new KeyAdapter () {@ Overridepublic void keyPressed (KeyEvent e) {// press // System. out. println ("key Pressed:" + e. getKeyChar () + ":" + e. getKeyCode () + ":" + E. getKeyText (e. getKeyCode (); // press it to print/* int code = e. getKeyCode (); if (! (Code> = KeyEvent. VK_0 & code <= KeyEvent. VK_9) // judge {System. out. println ("numbers required"); e. consume (); // used, will not follow the default event processing method} 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 the exit listener on the Form f. addWindowListener (new WindowAdapter () {public void windowClosing (invalid wevent e) {System. exit (0) ;}}); // Add the mouse listener bt on the button. addMouseListener (new MouseAdapter () {private int count = 1; // counter public void mouseEntered (MouseEvent e) {// trigger tf upon mouse touch. setText ("mouse enter:" + count ++); // Add the information to the text box} public void mouseClicked (MouseEvent e) {// click if (e. getClickCount () = 2) {// obtain the number of clicks and 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 ();}}

For the Swing package, you need to install the plug-in Ecplice.



How to Learn Java GUI programming?

If you want to develop towards java web, you can understand the principle mode of gui. No need to learn well.
Java se desktop software (guis required), with few market demands.

Is GUI important for learning JAVA?

Obsolete ??? As far as I know, although java is not as useful as many other languages for graphic user interfaces, it is still very useful when developing local applications, especially the improvements made by JDK.
Of course, you can use other languages for interface things, but in this way, your program will be hard to understand.
I learned software and got used to standardized development.

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.