Java image interface development simple example-simple application of jbutton and events

Source: Internet
Author: User

Java image interface development example

Simple Application of jbutton and events, a small example of a calculator, the Code is as follows:

Import java. AWT. borderlayout;
Import java. AWT. gridlayout;
Import java. AWT. event. actionevent;
Import java. AWT. event. actionlistener;

Import javax. Swing. jbutton;
Import javax. Swing. jframe;
Import javax. Swing. jpanel;

/**
* Calculator instance
* @ Author Zuo Jie jdk5.0
*/
Public class calculatorframe extends jframe {
/**
*
*/
Private Static final long serialversionuid = 1l;

Public calculatorframe (){
Settitle ("Calculator ");
Calculatorpanel Panel = new calculatorpanel ();
Add (panel );
Pack ();
}

Public static void main (string [] ARGs ){
Calculatorframe frame = new calculatorframe ();
Frame. setdefaclocloseoperation (jframe. exit_on_close );
Frame. setvisible (true );
}
}

/**
* Create a display calculator panel
*/
Class calculatorpanel extends jpanel {
/**
*
*/
Private Static final long serialversionuid = 1l;

Private jbutton display; // used to display results
Private jpanel; // create a panel for displaying numbers and symbol buttons
Private double result; // used to store calculation results
Private string lastcommand; // record the clicked symbol
Private Boolean start; // The start flag.

Public calculatorpanel (){
Setlayout (New borderlayout ());
Result = 0;
Lastcommand = "= ";
Start = true;

// Add
Display = new jbutton ("0 ");
Display. setenabled (false );
Add (display, borderlayout. North );

// Create an event listening object
Actionlistener insert = new insertaction ();
Actionlistener command = new commandaction ();

// Panel for adding numbers and symbol buttons
Panel = new jpanel ();
Panel. setlayout (New gridlayout (4, 4); // set the panel layout to four rows and four columns
// Add digit and symbol buttons
Addbutton ("7", insert );
Addbutton ("8", insert );
Addbutton ("9", insert );
Addbutton ("/", command );

Addbutton ("4", insert );
Addbutton ("5", insert );
Addbutton ("6", insert );
Addbutton ("*", command );

Addbutton ("1", insert );
Addbutton ("2", insert );
Addbutton ("3", insert );
Addbutton ("-", command );

Addbutton ("0", insert );
Addbutton (".", insert );
Addbutton ("=", command );
Addbutton ("+", command );

Add (panel, borderlayout. center );
}

/**
* Used to set buttons and add corresponding listeners
*
* @ Param label
* @ Param listener: Corresponding event listening object
*
*/
Private void addbutton (string label, actionlistener listener ){
Jbutton button = new jbutton (Label );
Button. addactionlistener (listener );
Panel. Add (button );
}

/**
* Click the number button event to display the number and set the start flag to false;
*/
Private class insertaction implements actionlistener {
Public void actionreceivmed (actionevent event ){
String input = event. getactioncommand ();
If (start ){
Display. settext ("");
Start = false;
}
Display. settext (display. gettext () + input );
}
}

/**
* Click the symbol button event for calculation.
*/
Private class commandaction implements actionlistener {
Public void actionreceivmed (actionevent event ){
String command = event. getactioncommand ();
// If start is set to true, it indicates start. Then, click "-" to indicate a negative number.
If (start ){
If (command. Equals ("-")){
Display. settext (command );
Start = false;
} Else
Lastcommand = command;
} Else {// start is false for calculation.
Calculate (double. parsedouble (display. gettext ()));
Lastcommand = command;
Start = true;
}
}
}

/**
* The calculation method is more input data and symbols for calculation.
*
* @ Param X refers to the number entered.
*
*/
Public void calculate (Double X ){
If (lastcommand. Equals ("+ "))
Result + = X;
Else if (lastcommand. Equals ("-"))
Result-= X;
Else if (lastcommand. Equals ("*"))
Result * = X;
Else if (lastcommand. Equals ("/"))
Result/= X;
Else if (lastcommand. Equals ("= "))
Result = X;
Display. settext ("" + result );
}
}

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.