Calculator (Java)

Source: Internet
Author: User

Import Java.awt.borderlayout;import java.awt.button;import Java.awt.flowlayout;import Java.awt.Rectangle;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.math.bigdecimal;import Java.math.biginteger;import Javax.swing.jbutton;import Javax.swing.jframe;import Javax.swing.JTextField;public Class Calculator_2 extends JFrame implements ActionListener {private JTextField jtf;private JButton btn1;private jbutton[ ] btn = new Jbutton[18];p rivate string str1 = "789-456x123÷0.c+=";p rivate string str = "";p rivate boolean isfrist = True;p rivate int flag = 1, style = 0; Public calculator_2 () {super ("calculator improved"); This.setbounds (+, +, +); this.setresizable (false); This.setdefaultcloseoperation (exit_on_close);//calculation Interface settings This.getcontentpane (). setlayout (null); JTF = new JTextField (" Jtf.setbounds (New Rectangle (5, 3, 235,)); Jtf.sethorizontalalignment (jtextfield.right); This.getcontentpane (). Add (JTF);//button set for (int i = 1; i <=; i++) {Btn[i] = new JButton (""+ Str1.charat (i-1)); Btn[i].addactionlistener (this); This.getcontentpane (). Add (Btn[i]); if (I <= 4) {Btn[i]. SetBounds (i-1) * (60), 92, 43, 42);} if (i > 4 && I <= 8) {btn[i].setbounds (i-5) * (60), 144, 43, 42);} if (i > 8 && i <=) {btn[i].setbounds (i-9) * (60), 196, 43, 42);}} Btn[13].setbounds (----------------), Btn[14].setbounds (----), Btn[15].setbounds (--------); BTN[16]. SetBounds (Btn[17].setbounds, Max, Max); (New Rectangle (Ten, 102, +)); This.setvisible (true);} public static void Main (string[] args) {new calculator_2 ();} @Overridepublic void actionperformed (ActionEvent e) {if (e.getsource () = = Btn[1]) {str = Jtf.gettext () + "7"; Jtf.settext (S TR);} if (e.getsource () = = btn[2]) {str = Jtf.gettext () + "8"; Jtf.settext (str);} if (e.getsource () = = Btn[3]) {str = Jtf.gettext () + "9"; Jtf.settext (str);} if (e.getsource () = = Btn[5]) {str = Jtf.gettext () + "4"; Jtf.settext (str);} if (e.getsource () = = Btn[6]) {str = JTF.GEttext () + "5"; Jtf.settext (str);} if (e.getsource () = = Btn[7]) {str = Jtf.gettext () + "6"; Jtf.settext (str);} if (e.getsource () = = Btn[9]) {str = Jtf.gettext () + "1"; Jtf.settext (str);} if (e.getsource () = = btn[10]) {str = Jtf.gettext () + "2"; Jtf.settext (str);} if (e.getsource () = = btn[11]) {str = Jtf.gettext () + "3"; Jtf.settext (str);} if (e.getsource () = = btn[13]) {str = Jtf.gettext () + "0"; Jtf.settext (str);} if (e.getsource () = = btn[14] && flag = = 1) {str = Jtf.gettext () + "."; Flag = 0;jtf.settext (str);} if (e.getsource () = = btn[15]) {Jtf.settext ("");} if (e.getsource () = = Btn[4] && isfrist) {str = Jtf.gettext () + "-"; isfrist = False;flag = 1;style = 2;jtf.settext ( STR);} if (e.getsource () = = Btn[8] && isfrist) {str = Jtf.gettext () + "x"; isfrist = False;flag = 1;style = 3;jtf.settext ( STR);} if (e.getsource () = = btn[12] && isfrist) {str = Jtf.gettext () + "÷"; isfrist = False;flag = 1;style = 4;jtf.settext (str);} if (e.getsource () = = btn[16] && isfrist) {str =Jtf.gettext () + "+"; isfrist = False;flag = 1;style = 1;jtf.settext (str);} if (e.getsource () = = Btn[17]) {if (style = = 1) {String s[] = str.split ("+"); BigDecimal B1 =new BigDecimal (S[0]); BigDecimal B2 =new BigDecimal (s[1]); Jtf.settext ("" + B1.add (B2)); B1=b1.add (b2); flag = 1;isfrist = true;} if (style = = 2) {String s[] = Str.split ("-"); BigDecimal B1 =new BigDecimal (S[0]); BigDecimal B2 =new BigDecimal (s[1]); Jtf.settext ("" + b1.subtract (B2)); B1=b1.add (b2); flag = 1;isfrist = true;} if (style = = 3) {String s[] = Str.split ("x"); BigDecimal B1 =new BigDecimal (S[0]); BigDecimal B2 =new BigDecimal (s[1]); Jtf.settext ("" + b1.multiply (B2)); B1=b1.add (b2); flag = 1;isfrist = true;} if (style = = 4) {String s[] = str.split ("÷"); BigDecimal B1 =new BigDecimal (S[0]); BigDecimal B2 =new BigDecimal (s[1]); Jtf.settext ("" + b1.divide (b2,5,bigdecimal.round_half_up)); B1=b1.add (b2); flag = 1 ; isfrist = True;}}}

Calculator (Java)

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.