Graphical User Interface (I) GUI Elements

Source: Internet
Author: User

Creating a GUI program in Java requires at least three types of objects:

* Component

* Events

* Listener

Pushcounter is a startup class and only displays a button (marked as "Push me !"), Press the button to update the counter to display the latest results.

// Pushcounter. Java
// Demonstrate a graphical user interface and listener

Import javax. Swing. jframe;

Public class pushcounter {

// Create a presentation framework
Public static void main (string [] ARGs ){

Jframe frame = new jframe ("Push counter ");
Frame. setdefaclocloseoperation (jframe. exit_on_close );

Pushcounterpanel Panel = new pushcounterpanel ();
Frame. getcontentpane (). Add (panel );

// The pack method is used to set the frame size according to the content in the frame.
Frame. Pack ();
Frame. setvisible (true );
}
}

 

// Pushcounterpanel. Java

Import java. AWT. color;
Import java. AWT. dimension;
Import java. AWT. event. actionevent;
Import java. AWT. event. actionlistener;

Import javax. Swing. jbutton;
Import javax. Swing. jlabel;
Import javax. Swing. jpanel;

Public class pushcounterpanel extends jpanel {
Private int count;
Private jbutton push;
Private jlabel label;
 
Public pushcounterpanel (){
Count = 0;

Push = new jbutton ("push me! ");
Push. addactionlistener (New buttonlistener ());

Label = new jlabel ("pushes:" + count );

Add (push );
Add (Label );

Setbackground (color. Cyan );
Setpreferredsize (new dimension (300, 40 ));
}
 
// Update the tag when the button is pressed.

Private class buttonlistener implements actionlistener {

 
Public void actionreceivmed (actionevent arg0 ){
Count ++;
Label. settext ("pushes:" + count );
}

}
}

Components used in this program include: a button, a tag for displaying the count, a panel for holding the buttons and labels, and a frame for displaying the panel.

Jframe is like a building, a panel is like a room, and things are placed in the room

Graphical things have a special attraction. After all, they are real things that can be seen.

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.