تلخص هذه المقالة الطرق المشتركة لكتابة الآلات الحاسبة في Java. شاركه للرجوع إليه ، على النحو التالي:
الطريقة 1:
حزمة wanwa ؛ استيراد java.awt.*؛ استيراد java.awt.event.*؛ استيراد javax.swing.*؛ يمتد آلة حاسبة الفئة العامة jframe {حاوية حاوية خاصة ؛ private gridbaglayout layout private lastcommand ؛ النتيجة ؛ // حفظ النتيجة الناتجة عن بداية منطقية خاصة ؛ // حدد ما إذا كانت بداية عدد من الآلة الحاسبة العامة () {super ("حاسبة") ؛ حاوية = getContentPane () ؛ تخطيط = جديد gridbaglayout () ؛ container.setLayout (layout) ؛ القيود = new gridbagconstraints () ؛ start = 0 ؛ jtextfield (20) ؛ displayfield.sethorizontalalignment (jtextfield.right) ؛ القيود. gridx = 0 ؛ القيود. gridy = 0 ؛ قيود. 100 ؛ Synout.setConstraints (Displayfield ، قيود) ؛ Container.Add (DisplayField) ؛ ActionListener insert = new insertAction () ؛ actionListener command = new commandAction () ؛ // addbutton ("backspace" ، 0 ، 1 ، 2 ، 1 ، 1 ، // addbutton (ce "، 2 ، 1 ، 1 ، 1 ، 1 ، 1 ، insert) ؛ addbutton ("7" ، 0 ، 2 ، 1 ، 1 ، insert) ؛ addbutton ("8" ، 1 ، 2 ، 1 ، 1 ، insert) ؛ addbutton ("9" ، 2 ، 2 ، 1 ، 1 ، insert) ؛ addbutton (/"، 3 ، 1 ، 1 ، addbutton ( insert) ؛ addbutton ("5" ، 1 ، 3 ، 1 ، addbutton ("6" ، 2 ، 3 ، 1 ، 1 ، 1 ، insert) ؛ addbutton ("*" ، 3 ، 3 ، 1 ، command) ؛ addbutton ("1" ، 0 ، 4 ، 1 ، 1 ، addbutton (2 "، 4 ، 1 ، 1 ، 1 ، 1 ، 1 ، 1 ، insert) ؛ addbutton ("-" ، 3 ، 4 ، 1 ، 1 ، command) ؛ addbutton ("0" ، 0 ، 5 ، 1 ، 1 ، insert) ؛ // addbutton ("+/-" ، 1 ، 5 ، 1 ، 1 ، 1 ، 1 ، insert) ؛ insert) ؛ addButton ("+" ، 3 ، 5 ، 1 ، 1 ، command) ؛ addButton ("=" ، 0 ، 6 ، 4 ، 1 ، command) ؛ this.setResizible (false) ؛ setSize (180 ، 200) ؛ setVisible (true) ؛} private void addbutton (string label ، int row ، int ، int with ، int ، int ، actionlistener buttener) {jbutton button = new jbutton (label) ؛ الارتفاع ؛ القيود. = false ؛ if (input.equals ("+/-")) displayfield.settext (displayfield.getText ()+"-") ؛} if (! input.equals ("+/-") {if (input.equals ("backspace")) - 1)) ؛} آخر إذا (input.equals ("ce") || input.equals ("c")) {displayfield.settext ("0") ؛ start = true ؛} elsedisplayfield.settext evt.getActionCommand () ؛ if (start) {lastCommand = command ؛ } آخر {calculate (double.parsedouble (displayfield.getText ())) ؛ lastCommand = command ؛ ابدأ = صحيح ؛ }}} public void حساب (double x) {if (lastCommand.equals (" +")) result += x ؛ else if (lastCommand.equals (" -")). x ؛ displayfield.settext ("" + result) ؛} الفراغ الثابت العام (سلسلة [] args) {calculator calculate = new calculator () ؛ calculator.setDefaultCloseOperation (الطريقة 2:
استيراد java.awt Textfield الخاص TF = New Textfield (30) ؛ نتيجة طويلة خاصة ؛ إلحاق منطقي خاص = خطأ ؛ مشغل char الخاص = '=' ؛ زر خاص [] BTN = زر جديد [15] ؛ public mycalculator () {initComponent () ؛ } private void initComponent () {f = new Frame ("My Calculator v1.0") ؛ F.SetLayout (New BorderLayout ()) ؛ // يستخدم الإطار borderlayout f.addwindowlistener (new windowadapter () {public void windowclosing (windowevent evt) {system.exit (0) ؛}}) ؛ لوحة centerpanel = new panel () ؛ CenterPanel.setLayout (New GridLayout (5 ، 3)) ؛ // تستخدم اللوحة GridLayout NumberListener nl = new NumberListener () ؛ OperatorListener ol = new OperatorListener () ؛ BTN [10] = زر جديد ("+") ؛ BTN [11] = زر جديد ("-") ؛ BTN [12] = زر جديد ("*") ؛ BTN [13] = زر جديد ("/") ؛ BTN [14] = زر جديد ("=") ؛ لـ (int i = 0 ؛ i <= 9 ؛ i ++) {btn [i] = زر جديد (string.valueof (i)) ؛ centerpanel.add (btn [i]) ؛ btn [i] .addActionListener (NL) ؛ if (i ٪ 2 == 1) {centerpanel.add (btn [(i+19)/2]) ؛ BTN [(I+19)/2] .AddActionListener (OL) ؛ }} f.add (centerpanel ، borderlayout.center) ؛ لوحة NorthPanel = New Panel () ؛ tf.setedable (false) ؛ Northpanel.add (TF) ؛ F.Add (Northpanel ، BorderLayout.north) ؛ } public void go () {f.pack () ؛ F.SetVisible (صحيح) ؛ } public static void main (string [] args) {new mycalculator (). go () ؛ } /***استخدم طريقة الفئة الداخلية لتنفيذ واجهة المستمع لتسهيل الوصول إلى الأعضاء الداخليين في الفصل الرئيسي داخل الفصل. *هذه الفئة مسؤولة عن أحداث الاستماع والتعامل مع زر الرقم*/ رقم الفئة تنفذ ActionListener {public void actionperformed (ActionEvent e) {if (! append) {tf.settext ("" ") ؛ إلحاق = صحيح ؛ } string s = tf.getText () ؛ S+= E.GetActionCommand () ؛ tf.settext (s) ؛ if (! btn [10] .isenabled ()) {for (int i = 10 ؛ i <= 14 ؛ i ++) btn [i] .SetEnabled (true) ؛ }}} / *** الفئة الداخلية للأعضاء ، المسؤولة عن معالجة الأحداث لأزرار المشغل* / class purstrateListener تنفذ ActionListener {public void actionperformed (Actionevent e) {if (! append) return ؛ لـ (int i = 10 ؛ i <= 14 ؛ i ++) btn [i] .SetEnabled (false) ؛ سلسلة s = tf.getText () ؛ long num = long.parselong (s) ؛ // احصل على عدد textfield append = false ؛ // SET APPEND SWITCH (Operator) {case '+': result+= num ؛ break ؛ الحالة "-": النتيجة- = num ؛ break ؛ الحالة '*': النتيجة*= num ؛ break ؛ Case '/': {if (num == 0) result = 0 ؛ نتيجة أخرى/= num ؛ استراحة؛ } case '=': result = num ؛ break ؛ } tf.settext (string.valueof (result)) ؛ // قم بتعيين قيمة النتيجة على سلسلة textfield op = e.getActionCommand () ؛ المشغل = op.charat (0) ؛ // SET Operator}}}الطريقة 3:
package wanwa ؛ import java.util.*؛ calc calc {public static void main (string [] args) {scanner input = new scanner (system.in) ؛ system.out.println ("**************************** للاستخدام: 1. الإضافة 2. الطرح 3. الضرب 4. القسم*") ؛ system.out.println ("*/t/t/t/t*") i = 0 ؛ i <100 ؛ i ++) {system.out.print ("/nplease حدد قواعد العملية:") ؛ int num = input.nextint () ؛ Switch (num) {case 1: system.out.println ("/n ****** لقد حددت إضافة *******/n") ؛ system.out.print ("الرجاء إدخال أول adder:") ؛ int jiashu1 = input.nextint () ؛ system.out.print ("الرجاء إدخال الدفرة الثانية:") IS: " + jiashu1 +" + " + jiashu1 +" = " + (jiashu1 + jiashu2)) ؛ break ؛ الحالة 2: system.out.println ("/n ****** لقد حددت الطرح *******/n ") أدخل الطرح: ") ؛ int jianshu2 = input.nextint () ؛ system.out.println (" نتيجة العملية هي: " + jianshu1 +" - " + jianshu2 +" = " + (jianshu1 - jianshu2)) ؛ الضرب *******/n ") ؛ system.out.print (" الرجاء إدخال العامل الأول: ") ؛ int chengfa1 = input.nextint () ؛ system.out.print (" الرجاء إدخال العامل الثاني: ") (chengfa1*chengfa2)) ؛ break ؛ الحالة 4: system.out.println ("/n ****** لقد اخترت الانقسام *******/n") ؛ system.out.print ("الرجاء إدخال المقسوم:") ؛ double chufa1 = input.nextint () input.nextint () ؛ system.out.println ("نتيجة العملية هي:" + chufa1 + " /" + chufa2 + "=" + (chufa1 / chufa2) + "yu" + (chufa1 ٪ chufa2)) ؛الطريقة 4:
حزمة wanwa ؛ استيراد java.awt.*؛ استيراد java.awt.event.*؛ استيراد javax.swing.*؛ يمتد آلة حاسبة الفئة العامة jframe {حاوية حاوية خاصة ؛ private gridbaglayout layout private lastcommand ؛ النتيجة ؛ // حفظ النتيجة الناتجة عن بداية منطقية خاصة ؛ // حدد ما إذا كانت بداية عدد من الآلة الحاسبة العامة () {super ("حاسبة") ؛ حاوية = getContentPane () ؛ تخطيط = جديد gridbaglayout () ؛ container.setLayout (layout) ؛ القيود = new gridbagconstraints () ؛ start = 0 ؛ jtextfield (20) ؛ displayfield.sethorizontalalignment (jtextfield.right) ؛ القيود. gridx = 0 ؛ القيود. gridy = 0 ؛ قيود. 100 ؛ Synout.setConstraints (Displayfield ، قيود) ؛ Container.Add (DisplayField) ؛ ActionListener insert = new insertAction () ؛ actionListener command = new commandAction () ؛ // addbutton ("backspace" ، 0 ، 1 ، 2 ، 1 ، 1 ، // addbutton (ce "، 2 ، 1 ، 1 ، 1 ، 1 ، 1 ، insert) ؛ addbutton ("7" ، 0 ، 2 ، 1 ، 1 ، insert) ؛ addbutton ("8" ، 1 ، 2 ، 1 ، 1 ، insert) ؛ addbutton ("9" ، 2 ، 2 ، 1 ، 1 ، insert) ؛ addbutton (/"، 3 ، 1 ، 1 ، addbutton ( insert) ؛ addbutton ("5" ، 1 ، 3 ، 1 ، addbutton ("6" ، 2 ، 3 ، 1 ، 1 ، 1 ، insert) ؛ addbutton ("*" ، 3 ، 3 ، 1 ، command) ؛ addbutton ("1" ، 0 ، 4 ، 1 ، 1 ، addbutton (2 "، 4 ، 1 ، 1 ، 1 ، 1 ، 1 ، 1 ، insert) ؛ addbutton ("-" ، 3 ، 4 ، 1 ، 1 ، command) ؛ addbutton ("0" ، 0 ، 5 ، 1 ، 1 ، insert) ؛ // addbutton ("+/-" ، 1 ، 5 ، 1 ، 1 ، 1 ، 1 ، insert) ؛ insert) ؛ addButton ("+" ، 3 ، 5 ، 1 ، 1 ، command) ؛ addButton ("=" ، 0 ، 6 ، 4 ، 1 ، command) ؛ this.setResizible (false) ؛ setSize (180 ، 200) ؛ setVisible (true) ؛} private void addbutton (string label ، int row ، int ، int with ، int ، int ، actionlistener buttener) {jbutton button = new jbutton (label) ؛ الارتفاع ؛ القيود. = false ؛ if (input.equals ("+/-")) displayfield.settext (displayfield.getText ()+"-") ؛} if (! input.equals ("+/-") {if (input.equals ("backspace")) - 1)) ؛} آخر إذا (input.equals ("ce") || input.equals ("c")) {displayfield.settext ("0") ؛ start = true ؛} elsedisplayfield.settext evt.getActionCommand () ؛ if (start) {lastCommand = command ؛ } آخر {calculate (double.parsedouble (displayfield.getText ())) ؛ lastCommand = command ؛ ابدأ = صحيح ؛ }}} public void حساب (double x) {if (lastCommand.equals (" +")) result += x ؛ else if (lastCommand.equals (" -")). x ؛ displayfield.settext ("" + result) ؛} الفراغ الثابت العام (سلسلة [] args) {calculator calculate = new calculator () ؛ calculator.setDefaultCloseOperation (آمل أن يكون هذا المقال مفيدًا لبرمجة Java للجميع.