ألسلام عليكم :::
الكود::
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Color;
public class Calculater extends JFrame implements ActionListener{
String num1,num2,oper;
JTextField text;
public Calculater (){
//create the first panel
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout (4,3,2,2));
//add the numbes buttons in the fist panel
JButton jbt1=new JButton("1");
p1.add(jbt1);
jbt1.addActionListener(this);
jbt1.setBackground(new Color (0,75,151));
jbt1.setForeground(new Color (255,255,255));
JButton jbt2=new JButton("2");
p1.add(jbt2);
jbt2.addActionListener(this);
jbt2.setBackground(new Color (0,75,151));
jbt2.setForeground(new Color (255,255,255));
JButton jbt3=new JButton("3");
p1.add(jbt3);
jbt3.addActionListener(this);
jbt3.setBackground(new Color (0,75,151));
jbt3.setForeground(new Color (255,255,255));
JButton jbt4=new JButton("4");
p1.add(jbt4);
jbt4.addActionListener(this);
jbt4.setBackground(new Color (0,88,176));
jbt4.setForeground(new Color (255,255,255));
JButton jbt5=new JButton("5");
p1.add(jbt5);
jbt5.addActionListener(this);
jbt5.setBackground(new Color (0,88,176));
jbt5.setForeground(new Color (255,255,255));
JButton jbt6=new JButton("6");
p1.add(jbt6);
jbt6.addActionListener(this);
jbt6.setBackground(new Color (0,88,176));
jbt6.setForeground(new Color (255,255,255));
JButton jbt7=new JButton("7");
p1.add(jbt7);
jbt7.addActionListener(this);
jbt7.setBackground(new Color (0,88,176));
jbt7.setForeground(new Color (255,255,255));
JButton jbt8=new JButton("8");
p1.add(jbt8);
jbt8.addActionListener(this);
jbt8.setBackground(new Color (0,102,204));
jbt8.setForeground(new Color (255,255,255));
JButton jbt9=new JButton("9");
p1.add(jbt9);
jbt9.addActionListener(this);
jbt9.setBackground(new Color (0,102,204));
jbt9.setForeground(new Color (255,255,255));
JButton jbt0=new JButton("0");
p1.add(jbt0);
jbt0.addActionListener(this);
jbt0.setBackground(new Color (0,88,176));
jbt0.setForeground(new Color (255,255,255));
JButton jbtm=new JButton("+/-");
p1.add(jbtm);
jbtm.addActionListener(this);
jbtm.setBackground(new Color (0,102,204));
jbtm.setForeground(new Color (255,255,255));
JButton jbtn=new JButton(".");
p1.add(jbtn);
jbtn.addActionListener(this);
jbtn.setBackground(new Color (0,102,204));
jbtn.setForeground(new Color (255,255,255));
//create the second panel
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout (4,2,2,2));
JButton jbtd=new JButton("/");
p2.add(jbtd);
jbtd.addActionListener(this);
jbtd.setBackground(new Color (0,75,151));
JButton jbtsq=new JButton(""+"sqrt");
p2.add(jbtsq);
jbtsq.addActionListener(this);
jbtsq.setBackground(new Color (0,75,151));
JButton jbts=new JButton("*");
p2.add(jbts);
jbts.addActionListener(this);
jbts.setBackground(new Color (0,88,176));
JButton jbtr=new JButton("%");
p2.add(jbtr);
jbtr.addActionListener(this);
jbtr.setBackground(new Color (0,88,176));
JButton jbtM=new JButton("-");
p2.add(jbtM);
jbtM.addActionListener(this);
jbtM.setBackground(new Color (0,102,204));
JButton jbt1x=new JButton("+/-");
p2.add(jbt1x);
jbt1x.addActionListener(this);
jbt1x.setBackground(new Color (0,102,204));
JButton jbtp=new JButton("+");
p2.add(jbtp);
jbtp.addActionListener(this);
jbtp.setBackground(new Color (0,118,236));
JButton jbte=new JButton("=");
p2.add(jbte);
jbte.addActionListener(this);
jbte.setBackground(new Color (0,118,236));
//create the third panel
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout (4,1,2,2));
JButton jbtmc=new JButton(""+"MC");
p3.add(jbtmc);
jbtmc.addActionListener(this);
jbtmc.setBackground(new Color (0,75,151));
JButton jbtmr=new JButton("MR");
p3.add(jbtmr);
jbtmr.addActionListener(this);
jbtmr.setBackground(new Color (0,75,151));
JButton jbtms=new JButton ("MS");
p3.add(jbtms);
jbtms.addActionListener(this);
jbtms.setBackground(new Color (0,75,151));
JButton jbtmm=new JButton("M+");
p3.add(jbtmm);
jbtmm.addActionListener(this);
jbtmm.setBackground(new Color (0,75,151));
//create the fourth panel
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout (1,4,2,2));
JButton jbtback=new JButton("BackSpace");
p4.add(jbtback);
jbtback.addActionListener(this);
JButton jbtce=new JButton("CE");
p4.add(jbtce);
jbtce.addActionListener(this);
JButton jbtC=new JButton("C");
p4.add(jbtC);
jbtC.addActionListener(this);
//create the fifth panel
JPanel p5 = new JPanel();
p5.setLayout(new BorderLayout());
p5.add(p1,BorderLayout.CENTER);
p5.add(p2,BorderLayout.EAST);
p5.add(p3,BorderLayout.WEST);
p5.add(p4,BorderLayout.NORTH);
//create the sixth panel
JPanel p6 = new JPanel();
p6.setLayout(new BorderLayout());
text=new JTextField(30);
p6.add(text,BorderLayout.NORTH);
p6.add(p5,BorderLayout.CENTER);
setLayout(new FlowLayout());
add(p6);
}
public static void main(String[] args){
Calculater frame = new Calculater();
frame.setSize(345,200);
frame.setTitle("Calculater");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e){
String str = e.getActionCommand();
if (str=="1"){
String temp=text.getText();
temp+="1";
text.setText(temp);
}
else if(str=="2"){
String temp=text.getText();
temp+="2";
text.setText(temp);
}
else if(str=="3"){
String temp=text.getText();
temp+="3";
text.setText(temp);
}
else if(str=="4"){
String temp=text.getText();
temp+="4";
text.setText(temp);
}
else if(str=="5"){
String temp=text.getText();
temp+="5";
text.setText(temp);
}
else if(str=="6"){
String temp=text.getText();
temp+="6";
text.setText(temp);
}
else if(str=="7"){
String temp=text.getText();
temp+="7";
text.setText(temp);
}
else if(str=="8"){
String temp=text.getText();
temp+="8";
text.setText(temp);
}
else if(str=="9"){
String temp=text.getText();
temp+="9";
text.setText(temp);
}
else if(str=="0"){
String temp=text.getText();
temp+="0";
text.setText(temp);
}
else if(str=="."){
String string=text.getText();
int ret=string.indexOf(".");
if (ret<0){
text.setText(string+".");
}
else if(ret>=0){
text.setText(string);
}
}
else if(str=="+"){
num1=text.getText();
oper="+";
text.setText("");
}
else if(str=="-"){
num1=text.getText();
oper="-";
text.setText("");
}
else if(str=="*"){
num1=text.getText();
oper="*";
text.setText("");
}
else if(str=="/"){
num1=text.getText();
oper="/";
text.setText("");
}
else if(str=="%"){
num1=text.getText();
oper="%";
text.setText("");
}
else if(str=="="){
double n1=Double.parseDouble(num1);
num2=text.getText();
double n2=Double.parseDouble(num2);
if(oper=="+"){
double sum=n1+n2;
String s=""+sum;
text.setText(s);
}
else if(oper=="-"){
double sum=n1-n2;
String s=""+sum;
text.setText(s);
}
else if(oper=="*"){
double sum=n1*n2;
String s=""+sum;
text.setText(s);
}
else if(oper=="/"){
double sum=n1/n2;
String s=""+sum;
text.setText(s);
}
else if(oper=="%"){
int nn1=Integer.parseInt(num1);
int nn2=Integer.parseInt(num2);
int sum=nn1%nn2;
String s=""+sum;
text.setText(s);
}
}
else if (str=="BackSpace"){
String b1=text.getText();
b1= b1.substring(0,b1.length()-1);
text.setText(b1);
}
else if (str=="CE"||str=="C"){
text.setText("");
}
else if (str=="+/-"){
String plusOrMin=text.getText();
double dPlusOrMin=Double.parseDouble(plusOrMin);
double plOrMin= dPlusOrMin*-1;
String strpOrM=""+plOrMin;
text.setText(strpOrM);
}
else if (str=="sqrt"){
String strSq=text.getText();
double doubleSq=Double.parseDouble(strSq);
double res =Math.sqrt(doubleSq);
String sqrt=""+res;
text.setText(sqrt);
}
}
}
}

