الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

نموذج الة حاسبة بلغة الجافا

بدأه raja2 في 18 أبريل 2008 · 16 رد · 9,482 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

اريد ان اقوم بعمل الة حاسبة

و لكني لا اعرف جزئية رئيسية بالموضوع ارجو تعليمي الطريقة لاستطيع اكمال الالة الحاسبة

قمنا بانشاء Button

عليه رقم صفر

اريد عند الضغط عليه يظهر الرقم بالتكست الذي عرفته

كيف؟

ساكون شاكرة لكم كثيرا

تحياتي

#2

عرفي متغير نصي عام و اسمه مثلا num

String num = "";

عند الضغط على الزر رقم 0

يقوم بالتالي .

num += "0";
textfied1.setText(num);

and so on to another buttons

#3

بالطبع لا تنسي أن تضيفي حدث على الزر الخاص بالصفر

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#4
 

Button1 = new Button("0");
  Button1.addActionListener(new ActionListener() {
   public void actionPerformed (ActionEvent event){ 
	Text1.setText("0");
	}  
   }
  );
#5

و نصيحتي ان تضعي كل عملية من العمليات الحسابية في كلاس مستقل و استخدمي انترفايس و بوليمورفيزم عند الاستدعاء :calc:

mofeedchaar@hotmail.com

#6

الف شكر عالمساعدة

صدقا ساعدتوني كتير

بس في مشكلة متلا انا عاملة 3text

واحد للرقم الاول

والتاني للرقم التاني

والتالت للنتيجة

....

بدي ادمجهم بتكست واحد

مشكلة تانية بدي التكست يستوعي اني كبست رقم 13 متلا مش ازا حطيت رقم واحد بعدين 3 يمسح الواحد و يحط التلاتة

...

#7

داخل الليستنر الموجود للبوتوم 3 مثلا

text3.setText(text3.getText()+3)

تم تعديل هذه المشاركة بواسطة mofeed chaar في 19 أبريل 2008 في 12:18

mofeedchaar@hotmail.com

#8

الف شكر اخواني عالمساعدة

بتمنى تشوفولي هذا الكود

لالة حاسبة ما في error

لكن يوجد به اخطاء

ارجو تفيهمي اين اخطأت و كيفية تعديل الخطأ حتى استطيع اكمال الالة الحاسبة

هذا الكود

/**

* @(#)RR.java

*

* RR Applet application

*

* @author

* @version 1.00 2008/4/26

*/

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class RR extends JApplet implements ActionListener
{
	JLabel lb1,lb2,lb3;
 JTextArea tf1,tf2,tf3;
 JButton btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,btn13,btn14,
btn15;
 Container c;
 int  x1,x2,sum;
String Calc="";


 public void init()
 {
  setSize(200,200);
  c=getContentPane();
  lb1=new JLabel("Number1 : ");
  tf2=new JTextArea(1,10);
  lb2=new JLabel("Number2 : ");
  tf3=new JTextArea(1,10);
  lb3=new JLabel("Result = ");
  tf1=new JTextArea(1,10);
  btn0=new JButton("0");
  btn1=new JButton("1");
  btn2=new JButton("2");
  btn3=new JButton("3");
  btn4=new JButton("4");
  btn5=new JButton("5");
  btn6=new JButton("6");
  btn7=new JButton("7");
  btn8=new JButton("8");
  btn9=new JButton("9");
  btn10=new JButton("+");
  btn11=new JButton("-");
  btn12=new JButton("*");
  btn13=new JButton("/");
  btn14=new JButton("=");
  btn15=new JButton("C");

  tf1.setEditable(false);


  btn0.addActionListener(this);
  btn2.addActionListener(this);
  btn3.addActionListener(this);
  btn4.addActionListener(this);
  btn5.addActionListener(this);
  btn6.addActionListener(this);
  btn7.addActionListener(this);
  btn8.addActionListener(this);
  btn9.addActionListener(this);
  btn10.addActionListener(this);
  btn11.addActionListener(this);
  btn12.addActionListener(this);
  btn13.addActionListener(this);
  btn14.addActionListener(this);
  btn15.addActionListener(this);

  

  c.setLayout(new FlowLayout());
  c.add(lb1);
  c.add(tf2);
  c.add(lb2);
  c.add(tf3);
  c.add(lb3);
  c.add(tf1);
  c.add(btn0);
  c.add(btn1);
  c.add(btn2);
  c.add(btn3);
  c.add(btn4);
  c.add(btn5);
  c.add(btn6);
  c.add(btn7);
  c.add(btn8);
  c.add(btn9);
  c.add(btn10);
  c.add(btn11);
  c.add(btn12);
  c.add(btn13);
  c.add(btn14);
	 c.add(btn15);
 }


  


   public void actionPerformed (ActionEvent event){ 


	if(event.getSource()==btn0)
	tf2.append("0"); 



	if(event.getSource()==btn1)
   tf2.append(""+1);


 if(event.getSource()==btn2)
 tf2.append(""+2);



	if(event.getSource()==btn3)
	tf2.append(""+3);



	if(event.getSource()==btn4)
	tf2.append("4");




 if(event.getSource()==btn5)
 tf2.append("5");



	if(event.getSource()==btn6)
	tf2.append("6");



 if(event.getSource()==btn7)
 tf2.append("7");


	if(event.getSource()==btn8)
	tf2.append("8");


	if(event.getSource()==btn9)
	{tf2.append("9");

	}
	if(event.getSource()==btn14){
	 tf1.setText(""+sum); 
	}



  if(event.getSource()==btn15)
	{ tf1.setText("");
	 tf2.setText("");
	 }



  x1=Integer.parseInt(tf2.getText());
  x2=Integer.parseInt(tf2.getText());


	  if(event.getSource()==btn10)
	   sum=add(x1,x2);

   else
	if(event.getSource()==btn11)
	sum=sub(x1,x2);


	else
	 if(event.getSource()==btn12)
	 sum=multi(x1,x2);

	  else
	   if(event.getSource()==btn13)
	  sum=t(x1,x2);
	tf2.setText(""); 



   }



 int add(int  x1,int  x2)
 {
  return (x1+x2);
 }
 int sub(int  x1,int  x2)
 {
  return (x1-x2);
 }
 int multi(int x1,int  x2)
 {
  return (x1*x2);
 }
 int t(int  x1,int  x2)
 {
  return (x1/x2);
 }

}
#9

لدي بعض الملاحظات حول الالة الحاسبة

اول ملاحظة وهو لماذا يكون هنالك 2TextArea ... من المعروف ان الالة الحاسبة لديها شاشة واحدة فقط ... هي للادخال والاخراج في نفس الوقت

وجميع الارقام تظهر في حقل واحد وهو الذي في الاعلى ... اما عن الحقل الثاني للرقم فلا يحدث عليه اي تغير ... بالاضافة الى انه لا يوجد حدث عند النقر على رقم 1

والامر الاخر وهو عملية القسم ... فمن غير اللائق جعل الناتج عو عبارة عن رقم من نوع int ... لان القسمة في بعض الاحيان لابد ان يحتوي الناتج على كسور اي فواصل

والامر الاخير يوجد مشكلة لم اتوصل اليها وهي عند عملية الجمع بين الرقم 3 و 7 لان الناتج هو 6... وعملية الجمع بين 3 و 9 لان الناتج يكون 6

اما عن الكود فلقد جولته الى JFram بدلاً من JApplet وهو كالتالي :

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Cal extends JFrame implements ActionListener
{
JLabel lb1,lb2,lb3;
JTextArea tf1,tf2,tf3;
JButton btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,btn13,btn14,
btn15;
Container c;
int  x1,x2,sum;
String Calc="";


public Cal()
{

  c=getContentPane();
  lb1=new JLabel("Number1 : ");
  tf2=new JTextArea(1,10);
  lb2=new JLabel("Number2 : ");
  tf3=new JTextArea(1,10);
  lb3=new JLabel("Result = ");
  tf1=new JTextArea(1,10);
  btn0=new JButton("0");
  btn1=new JButton("1");
  btn2=new JButton("2");
  btn3=new JButton("3");
  btn4=new JButton("4");
  btn5=new JButton("5");
  btn6=new JButton("6");
  btn7=new JButton("7");
  btn8=new JButton("8");
  btn9=new JButton("9");
  btn10=new JButton("+");
  btn11=new JButton("-");
  btn12=new JButton("*");
  btn13=new JButton("/");
  btn14=new JButton("=");
  btn15=new JButton("C");

  tf1.setEditable(false);


  btn0.addActionListener(this);
  btn2.addActionListener(this);
  btn3.addActionListener(this);
  btn4.addActionListener(this);
  btn5.addActionListener(this);
  btn6.addActionListener(this);
  btn7.addActionListener(this);
  btn8.addActionListener(this);
  btn9.addActionListener(this);
  btn10.addActionListener(this);
  btn11.addActionListener(this);
  btn12.addActionListener(this);
  btn13.addActionListener(this);
  btn14.addActionListener(this);
  btn15.addActionListener(this);



  c.setLayout(new FlowLayout());
  c.add(lb1);
  c.add(tf2);
  c.add(lb2);
  c.add(tf3);
  c.add(lb3);
  c.add(tf1);
  c.add(btn0);
  c.add(btn1);
  c.add(btn2);
  c.add(btn3);
  c.add(btn4);
  c.add(btn5);
  c.add(btn6);
  c.add(btn7);
  c.add(btn8);
  c.add(btn9);
  c.add(btn10);
  c.add(btn11);
  c.add(btn12);
  c.add(btn13);
  c.add(btn14);
  c.add(btn15);
}





   public void actionPerformed (ActionEvent event){ 


if(event.getSource()==btn0){
   tf2.append("0");
	}
if(event.getSource()==btn1){
	tf2.append(""+1);
	}
if(event.getSource()==btn2){
tf2.append(""+2);
   }
if(event.getSource()==btn3){
	tf2.append(""+3);
	}
if(event.getSource()==btn4){
	tf2.append("4");
	}
if(event.getSource()==btn5){
tf2.append("5");
}
if(event.getSource()==btn6){
	tf2.append("6");
	}
if(event.getSource()==btn7){
tf2.append("7");
}
if(event.getSource()==btn8){
	tf2.append("8");
	}
if(event.getSource()==btn9){
	tf2.append("9");
	}
if(event.getSource()==btn14){
	 tf1.setText(""+sum); 
	}
if(event.getSource()==btn15){ 
	tf1.setText("");
	 tf2.setText("");
	 }



  x1=Integer.parseInt(tf2.getText());
  x2=Integer.parseInt(tf2.getText());


   if(event.getSource()==btn10){
	sum = add(x1,x2);
	  }	 
   else 
	if(event.getSource()==btn11){
	sum = sub(x1,x2);
	}
	else
	 if(event.getSource()==btn12) {
	 sum = multi(x1,x2);
	 }
else
	  if(event.getSource()==btn13){
	  sum = t(x1,x2);
	  tf2.setText(""); 
	  }
	  }

int add(int  x1,int  x2)
{
  return (x1+x2);
}
int sub(int  x1,int  x2)
{
  return (x1-x2);
}
int multi(int x1,int  x2)
{
  return (x1*x2);
}
int t(int  x1,int  x2)
{
  return (x1/x2);
}
public static void main (String[] args) {
Cal cal = new Cal();
cal.setSize(200,250);
cal.setVisible(true);

}
}

تم تعديل هذه المشاركة بواسطة Jsp في 19 أبريل 2008 في 23:39

#10

هاد اخر شي طلع معي بالالة الحاسبة

في عندي شي غلط مو مصل يا ريت حدا يجرب البرنامج على جهازه حتى يحدد لي الاخطاء

و شكار لكم جميعا على مجهودكم معي

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class RR extends JApplet implements ActionListener
{
  

 

 
 JTextArea tf1;
 JButton btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,btn13,btn14,
btn15;
 Container c;
 int  x1,x2,sum;
String Calc="0";


 public void init()
 {
  setSize(200,200);
  c=getContentPane();


  tf1=new JTextArea(1,15);
  btn0=new JButton("0");
  btn1=new JButton("1");
  btn2=new JButton("2");
  btn3=new JButton("3");
  btn4=new JButton("4");
  btn5=new JButton("5");
  btn6=new JButton("6");
  btn7=new JButton("7");
  btn8=new JButton("8");
  btn9=new JButton("9");
  btn10=new JButton("+");
  btn11=new JButton("-");
  btn12=new JButton("*");
  btn13=new JButton("/");
  btn14=new JButton("=");
  btn15=new JButton("C");

  tf1.setEditable(false);


  btn0.addActionListener(this);
  btn1.addActionListener(this);
  btn2.addActionListener(this);
  btn3.addActionListener(this);
  btn4.addActionListener(this);
  btn5.addActionListener(this);
  btn6.addActionListener(this);
  btn7.addActionListener(this);
  btn8.addActionListener(this);
  btn9.addActionListener(this);
  btn10.addActionListener(this);
  btn11.addActionListener(this);
  btn12.addActionListener(this);
  btn13.addActionListener(this);
  btn14.addActionListener(this);
  btn15.addActionListener(this);

  

  c.setLayout(new FlowLayout());

  c.add(tf1);
  c.add(btn0);
  c.add(btn1);
  c.add(btn2);
  c.add(btn3);
  c.add(btn4);
  c.add(btn5);
  c.add(btn6);
  c.add(btn7);
  c.add(btn8);
  c.add(btn9);
  c.add(btn10);
  c.add(btn11);
  c.add(btn12);
  c.add(btn13);
  c.add(btn14);
	 c.add(btn15);
 }
 


   public void actionPerformed (ActionEvent event){ 



	if(event.getSource()==btn0)
	tf1.append("0"); 



	if(event.getSource()==btn1)
   tf1.append("1");



 if(event.getSource()==btn2)
 tf1.append("2");


	if(event.getSource()==btn3)
	tf1.append("3");



	if(event.getSource()==btn4)
	tf1.append("4");



 if(event.getSource()==btn5)
 tf1.append("5");


	if(event.getSource()==btn6)
	tf1.append("6");


 if(event.getSource()==btn7)
 tf1.append("7");


	if(event.getSource()==btn8)
	tf1.append("8");


	if(event.getSource()==btn9)
	tf1.append("9");



	if(event.getSource()==btn14)
	{tf1.setText(""+Calc);
	Calc="0";
   } 




  if(event.getSource()==btn15)
	{ tf1.setText("0");

	 }


   x1=Integer.parseInt(tf1.getText());




	  if(event.getSource()==btn10)
	  {
	   tf1.setText("");
	   sum=add(x1,x2);
	  Calc+=sum;
	  }

  if(event.getSource()==btn11)
  {tf1.setText("");
   sum=sub(x1,x2);
   Calc+=sum;
	  }  

  if(event.getSource()==btn12)
  {tf1.setText("");
   sum=multi(x1,x2);
   Calc+=sum;
	  }  

  if(event.getSource()==btn13)
  {tf1.setText("");
   sum=t(x1,x2);
   Calc+=sum;
	  }

	   x2=Integer.parseInt(tf1.getText());

   }



 int add(int  x1,int  x2)
 {
  return (x1+x2);
 }
 int sub(int  x1,int  x2)
 {
  return (x1-x2);
 }
 int multi(int x1,int  x2)
 {
  return (x1*x2);
 }
 int t(int  x1,int  x2)
 {
  return (x1/x2);
 }

}
#11
 

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Cal extends JFrame {


public JTextField Text3;
public JButton Button0,Button1,Button2,Button3,Button4,Button5,Button6,
Button7,Button8,Button9,btn1,btn2,btn3,btn4,clear,equal;

public String Calc = "", opration = "";
public double number1 = 0,number2 = 0;

 public Cal (){
  super("Calculator");


  Container container = getContentPane();
  container.setLayout(new FlowLayout());

 Text3= new JTextField(15);
 Text3.setEditable(false);
 container.add(Text3);


  Button0 = new JButton("0");
  container.add(Button0);
  Button0.addActionListener(new ActionListener() {
  public void actionPerformed (ActionEvent event){
	Calc+= 0;
	Text3.setText(Calc);
	}
   }
  );

  Button1 = new JButton("1");
  container.add(Button1);
  Button1.addActionListener(new ActionListener() {
  public void actionPerformed (ActionEvent event){
	Calc+= 1;
	Text3.setText(Calc);
	}
   }
  );


  Button2 = new JButton("2");
  container.add(Button2);
  Button2.addActionListener(new ActionListener() {
  public void actionPerformed (ActionEvent event){
	Calc+= 2;
	Text3.setText(Calc);
	}
   }
  );

  Button3 = new JButton("3");
  container.add(Button3);
  Button3.addActionListener(new ActionListener() {
  public void actionPerformed (ActionEvent event){
	Calc+= 3;
	Text3.setText(Calc);
	}
   }
  );

 Button3 = new JButton("3");
  container.add(Button3);
  Button3.addActionListener(new ActionListener() {
  public void actionPerformed (ActionEvent event){
	Calc+= 3;
	Text3.setText(Calc);
	}
   }
  );

  Button4 = new JButton("4");
  container.add(Button4);
  Button4.addActionListener(new ActionListener() {
  public void actionPerformed (ActionEvent event){
	Calc+= 4;
	Text3.setText(Calc);
	}
   }
  );

  Button5 = new JButton("5");
  container.add(Button5);
  Button5.addActionListener(new ActionListener() {
  public void actionPerformed (ActionEvent event){
	Calc+= 5;
	Text3.setText(Calc);
	}
   }
  );

  Button6 = new JButton("6");
  container.add(Button6);
  Button6.addActionListener(new ActionListener() {
  public void actionPerformed (ActionEvent event){
	Calc+= 6;
	Text3.setText(Calc);
	}
   }
  );

  Button7 = new JButton("7");
  container.add(Button7);
  Button7.addActionListener(new ActionListener() {
  public void actionPerformed (ActionEvent event){
	Calc+= 7;
	Text3.setText(Calc);
	}
   }
  );

  Button8 = new JButton("8");
  container.add(Button8);
  Button8.addActionListener(new ActionListener() {
  public void actionPerformed (ActionEvent event){
	Calc+= 8;
	Text3.setText(Calc);
	}
   }
  );

  Button9 = new JButton("9");
  container.add(Button9);
  Button9.addActionListener(new ActionListener() {
  public void actionPerformed (ActionEvent event){
	Calc+= 9;
	Text3.setText(Calc);
	}
   }
  );

  btn1 = new JButton("+");
  container.add(btn1);
  btn1.addActionListener(new ActionListener() {
  public void actionPerformed (ActionEvent event){
	number1 = Integer.parseInt(Text3.getText());
	opration = "+";
	Calc = "";
   // Text3.setText("");
	}
   }
  );

  btn2 = new JButton("-");
  container.add(btn2);
  btn2.addActionListener(new ActionListener() {
  public void actionPerformed (ActionEvent event){
	number1 = Integer.parseInt(Text3.getText());
	opration = "-";
	Calc = "";
   // Text3.setText("");
	}
   }
  );

   btn3 = new JButton("*");
  container.add(btn3);
  btn3.addActionListener(new ActionListener() {
  public void actionPerformed (ActionEvent event){
	number1 = Integer.parseInt(Text3.getText());
	opration = "*";
	Calc = "";
   // Text3.setText("");
	}
   }
  );

 btn4 = new JButton("/");
  container.add(btn4);
  btn4.addActionListener(new ActionListener() {
  public void actionPerformed (ActionEvent event){
	number1 = Integer.parseInt(Text3.getText());
	opration = "/";
	Calc = "";
   // Text3.setText("");
	}
   }
  );

	clear = new JButton("C");
  container.add(clear);
  clear.addActionListener(new ActionListener() {
  public void actionPerformed (ActionEvent event){
	number1 = 0;
	number2 = 0;
	Calc = "";
	Text3.setText("");
	}
   }
  );

 equal = new JButton("=");
  container.add(equal);
  equal.addActionListener(new ActionListener() {
  public void actionPerformed (ActionEvent event){
   number2 = Integer.parseInt(Text3.getText());
   if (opration == "+"){
	Text3.setText(number1 + number2+"");
   }
   if (opration == "-"){
	Text3.setText(number1 - number2+"");
   }
   if (opration == "*"){
	Text3.setText(number1 * number2+"");
   }
   if (opration == "/"){
	Text3.setText(number1 / number2+"");
   }
	}
   }
  );
 }

 public static void main (String[] args) {
 Cal cal = new Cal ();
 cal.setSize(220,220);
 cal.setVisible(true);
 cal.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

}
1
#12

جربت البرنامج

ما في errors

لكن ما بنفذ اي شيء

!!!!!

#13

يمكن ان تكون اعدادات البرنامج خاطئة وولهذا لا يقوم بعملية Run للبرنامج

فهو يقوم Compile ولا يقوم بالعملية السابقة

هذا والله اعلم

#14

الكود عدلت عليه بعض الشغلات و اختصرت منه شوي

والحين شغاله 100%

بس ما ادري وش فيه الكود لما اضيفه في الرد يطلع متلخبط و ما ينقري

على العموم الكود في المرفقات

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;public class RR extends JApplet implements ActionListener
{

JTextArea tf1;
JButton btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,btn13,btn14,
btn15;
Container c;
double  x1,x2;
String Calc;
boolean isFixing;
String text01;
String text02;


//  CONSTRUCTOR ,,,,,,,,,,,,,

public RR(){
  x1 = 0;
  x2 =0;
  isFixing=true;
  Calc="";
  text01 = "0";
  text02 = "0";


}



public void init()
{
  setSize(200,200);
  c=getContentPane();


  tf1=new JTextArea(1,15);
  btn0=new JButton("0");
  btn1=new JButton("1");
  btn2=new JButton("2");
  btn3=new JButton("3");
  btn4=new JButton("4");
  btn5=new JButton("5");
  btn6=new JButton("6");
  btn7=new JButton("7");
  btn8=new JButton("8");
  btn9=new JButton("9");
  btn10=new JButton("+");
  btn11=new JButton("-");
  btn12=new JButton("*");
  btn13=new JButton("/");
  btn14=new JButton("=");
  btn15=new JButton("C");

  tf1.setEditable(false);


  btn0.addActionListener(this);
  btn1.addActionListener(this);
  btn2.addActionListener(this);
  btn3.addActionListener(this);
  btn4.addActionListener(this);
  btn5.addActionListener(this);
  btn6.addActionListener(this);
  btn7.addActionListener(this);
  btn8.addActionListener(this);
  btn9.addActionListener(this);
  btn10.addActionListener(this);
  btn11.addActionListener(this);
  btn12.addActionListener(this);
  btn13.addActionListener(this);
  btn14.addActionListener(this);
  btn15.addActionListener(this);


c.setLayout(new FlowLayout());

  c.add(tf1);
  c.add(btn0);
  c.add(btn1);
  c.add(btn2);
  c.add(btn3);
  c.add(btn4);
  c.add(btn5);
  c.add(btn6);
  c.add(btn7);
  c.add(btn8);
  c.add(btn9);
  c.add(btn10);
  c.add(btn11);
  c.add(btn12);
  c.add(btn13);
  c.add(btn14);
     c.add(btn15);
}




private double Calculation(String Calc, double x1, double x2)
   {
            if ("+".equals(Calc)) x1 = x1 + x2;
       else if ("-".equals(Calc)) x1 = x1 - x2;
       else if ("*".equals(Calc)) x1 = x1 * x2;
       else if ("/".equals(Calc)) x1 = x1 / x2;
       else x1 = x2;
            return x1;
   }



   public void actionPerformed (ActionEvent event){ 
    String op = event.getActionCommand();

    if("C".equals(op)){
  x1=0;
  x2=0;
  isFixing = true;
  Calc="";
  tf1.setText("0");


    } 

    else if (("0".equals(op)) | ("1".equals(op)) | ("2".equals(op))
               | ("3".equals(op)) | ("4".equals(op)) | ("5".equals(op))
               | ("6".equals(op)) | ("7".equals(op)) | ("8".equals(op))
               | ("9".equals(op)) | (".".equals(op)))


    {


     if(isFixing)

      text01 = (String)op; 

     // x1=Double.parseDouble(theTextF.getText());


     else

      text01 = tf1.getText()+op;

     tf1.setText(text01);    

     isFixing=false;


    }


    else if (("+".equals(op)) | ("-".equals(op))

      | ("*".equals(op)) | ("/".equals(op)) | ("=".equals(op)))

    {


     text02 = tf1.getText();

     x2 = Double.valueOf(text02).doubleValue();

     x1 = Calculation(Calc, x1, x2);

     Double dTemp = new Double(x1);

     text01 = dTemp.toString();

     tf1.setText(text01);
     Calc = (String)op;
     isFixing = true;   
    }
}
}

تم تعديل هذه المشاركة بواسطة sultan_almutairi في 26 أبريل 2008 في 07:10

#15

لقد رجيته ولكن كيف اطبقه على واجهات جافا

هل ممكن الاخت تبعت لنا مرفق بالبرنامج ويعيطك العافية

لأني بذلت جهد في فهمه و احبطت عندما لم يطبق

استغفر الله واتوب اليه

اللهم اتنا في الدنيا حسنة وفي الاخرة حسنة وقنا عذاب النار

لااله الا انت سبحانك اني كنت من الظالمين

اللهم اني اسألك الهدى والتقى والعفاف والغنى

اللهم استجب دعـــــــــــــائي الصالح يارب

#16

ممكن حد يضيفلي الاوامر على الازرار في هاي الالة الحاسبة ؟؟؟

import java.awt.*;

import javax.swing.*;

class calc extends JFrame{

Container c;

JTextField result ;

JPanel p = new JPanel();

JButton b[] = new JButton [16];

String s[]={"1","2","3","4","5","6","7","8","9","0",".","+","-","/","*","="};

public calc(){

super ("Simple Calculator");

c =getContentPane();

result = new JTextField();

result.setEditable(false);

result.setBackground(Color.yellow);

setResizable(false);

p.setLayout(new GridLayout(4,4));

for(int i=0;i<16;i++){

b =new JButton(s);

p.add(b);

}

c.add(result,BorderLayout.NORTH);

c.add(p);

setSize(200,200);

setVisible(true);

}

public static void main (String[] args) {

new calc();

}}

تم تعديل هذه المشاركة بواسطة Medo 1994 في 29 سبتمبر 2013 في 20:39

#17

أفضل طريقة تجدينها على الرابط التالي :

 

/index.php/topic/273499-%D8%AA%D9%86%D9%81%D9%8A%D8%B0-%D9%86%D8%B5-%D9%83%D9%85%D8%B9%D8%A7%D8%AF%D9%84%D8%A9-%D8%B1%D9%8A%D8%A7%D8%B6%D9%8A%D8%A9/

 

ضعي في المتغير math1 أي عملية رياضية وأنظري للحل ، مثلًا 5+20 أو 9-6 أو ..... إلخ

 

وهذي حاسبة مقتبسة من الطريقة أعلاه :

package Calculator;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;



public class NewJFrame extends javax.swing.JFrame {

  
    public NewJFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        t1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        t2 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();
        jButton13 = new javax.swing.JButton();
        jButton14 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("=");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("3");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("2");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("1");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("4");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("5");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setText("6");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton8.setText("7");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton9.setText("8");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        jButton10.setText("9");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        jButton11.setText("+");
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });

        jButton12.setText("x");
        jButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton12ActionPerformed(evt);
            }
        });

        jButton13.setText("/");
        jButton13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton13ActionPerformed(evt);
            }
        });

        jButton14.setText("مسح");
        jButton14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton14ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(112, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jButton14, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton12, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
                            .addComponent(jButton11, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
                            .addComponent(jButton13, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(t2, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(t1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)))
                .addGap(51, 51, 51))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(t2, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       ScriptEngineManager mgr = new ScriptEngineManager();
       ScriptEngine engine = mgr.getEngineByName("JavaScript");
        try {
             t2.setText(String.valueOf(engine.eval(t1.getText())));
        } catch (ScriptException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                        

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       t1.setText(t1.getText()+"1");
    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        t1.setText(t1.getText()+"2");
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        t1.setText(t1.getText()+"3");
    }                                        

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       t1.setText(t1.getText()+"4");
    }                                        

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        t1.setText(t1.getText()+"5");
    }                                        

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       t1.setText(t1.getText()+"6");
    }                                        

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        t1.setText(t1.getText()+"7");
    }                                        

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        t1.setText(t1.getText()+"8");
    }                                        

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        t1.setText(t1.getText()+"9");
    }                                         

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {                                          
       t1.setText(t1.getText()+"+");
    }                                         

    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        t1.setText(t1.getText()+"*");
    }                                         

    private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        t1.setText(t1.getText()+"/");
    }                                         

    private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {                                          
       t1.setText("");
       t2.setText("");
    }                                         

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton13;
    private javax.swing.JButton jButton14;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JTextField t1;
    private javax.swing.JTextField t2;
    // End of variables declaration                   

}

تم تعديل هذه المشاركة بواسطة هدف الشمالي في 12 أكتوبر 2013 في 11:51

مواضيع مشابهة