Java-based calculator design

Source: Internet
Author: User

I was embarrassed to upload this program. I had learned Java for more than half a month, so some people asked me to help him make a calculator. I couldn't tell what classes I was connected, I searched a lot of information on the Internet and compiled such a calculator based on my "Imagination". After all, I learned it myself.

Import Java. AWT. *; import javax. swing. *; import Java. AWT. event. *; public class jisuan extends jframe implements actionlistener {private jtextfield jieguo; private jtextfield Shu1, shu2; private jbutton OK, cancel, Jia, Jian, Cheng, Chu, Qing; Public jisuan () {super ("jisuanqi. "); Container c = getcontentpane (); C. setlayout (New flowlayout (); Shu1 = new jtextfield (6); shu2 = new jtextfield (6); jieguo = new jtextfield (10 ); OK = new jbutton ("calculation"); cancel = new jbutton ("cancel"); Jia = new jbutton ("addition"); Jian = new jbutton ("subtraction "); cheng = new jbutton ("multiplication"); Chu = new jbutton ("Division"); Qing = new jbutton ("cleared"); C. add (New jlabel ("enter two integers:"); C. add (Shu1); C. add (shu2); C. add (New jlabel ("result"); C. add (jieguo); C. add (JIA); C. add (Jian); C. add (Cheng); C. add (CHU); C. add (Qing); Jia. addactionlistener (this); Jian. addactionlistener (this); Cheng. addactionlistener (this); Chu. addactionlistener (this); Qing. addactionlistener (this);} public static void main (string [] ARGs) {jisuan JS = new jisuan (); Js. addwindowlistener (New windowadapter () {public void windowclosing (invalid wevent e) {system. exit (0) ;}}); Js. setsize (400,100); Js. show ();} public void actionreceivmed (actionevent e) {try {int num1 = integer. parseint (shu1.gettext (); int num2 = integer. parseint (shu2.gettext (); jbutton BTN = (jbutton) E. getsource (); If (BTN = Jia) jieguo. settext (string. valueof (num1 + num2); // system. out. println ("addition"); else if (BTN = Jian) jieguo. settext (string. valueof (num1-num2); // system. out. println ("subtraction"); else if (BTN = Cheng) jieguo. settext (string. valueof (num1 * num2); // system. out. println ("multiplication"); else if (BTN = Chu) jieguo. settext ("vendor:" + String. valueof (num1/num2) + "++ remainder:" + String. valueof (num1% num2); // system. out. println ("Division"); else if (BTN = Qing) {shu1.settext (""); shu2.settext (""); jieguo. settext ("");} // system. out. println ("cleared"); else // system. out. println ("error"); jieguo. settext ("XXXX");} catch (numberformatexception I) {shu1.settext (""); shu2.settext (""); jieguo. settext (""); // system. out. println ("the input data format is incorrect. ");}}}

The result is as follows:

Use it together.

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.