# الأقسام التقنية > طلبات البرامج >  Implementing a calculator

## Caps Lock

السلام عليكم . 

عندي كود للآله حاسبه وناقص . 

ممكن تساعدوني فيه ؟ 

// Program creates a GUI that resembles a calculator.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CalculatorFrame extends JFrame
{
   private JButton[] keys;
   private JPanel keyPadJPanel;
   private JTextField lcdJTextField;
   Object source; //<<<<<<<<<<<<<<<<<<
   int op1, op2;  //<<<<<<<<<<<<<<<<<<
   char operator; //<<<<<<<<<<<<<<<<<<

   // constructor sets up GUI
   public CalculatorFrame()
   {
      super( "Calculator" );

      lcdJTextField = new JTextField( 20 ); // create lcdJTextField
      lcdJTextField.setEditable( false ); // allow user input
      lcdJTextField.setText( "0" ); //<<<<<<<<<<<<<<<<<<
      lcdJTextField.setBackground(Color.black);
      lcdJTextField.setForeground(Color.white);

      keys = new JButton[ 16 ]; // array keys contains 16 JButtons

    //<<<< create the buttonHandler of class ButtonHandler
      ButtonHandler buttonHandler =new ButtonHandler();

      // initialize all digit key buttons
      for ( int i = 0; i <= 9; i++ )
      {
    	  //<<< create the keys; use String.valueOf(i).
               keys[i]= new JButton(String.valueOf(i)); 
        		//<<< add action listner
               keys[i].addActionListener(buttonHandler);
  }

      // initialize all function key buttons
      keys[ 10 ] = new JButton("/");
    keys[ 10 ].addActionListener(buttonHandler); //<<<<<<<<<<<<<
    keys[ 10 ].setToolTipText("Integer division.");


      // key[11]
        keys[ 11] = new JButton("*");
    	keys[ 11 ].addActionListener(buttonHandler); //<<<<<<<<<<<<<
    	 keys[ 11 ].setToolTipText("Integer division.");  

      // key[12]
     keys[ 12 ] = new JButton("-");
    keys[ 12 ].addActionListener(buttonHandler); //<<<<<<<<<<<<<
    keys[ 12 ].setToolTipText("Integer division.");
      // key[13]
     keys[ 13 ] = new JButton("+");
    keys[ 13 ].addActionListener(buttonHandler); //<<<<<<<<<<<<<
     keys[ 13 ].setToolTipText("Integer division.");

      // key[14]
     keys[ 14 ] = new JButton("=");
    keys[ 14 ].addActionListener(buttonHandler); //<<<<<<<<<<<<<
     keys[ 14 ].setToolTipText("Integer division.");

      // key[15]
     keys[ 15 ] = new JButton(".");
    keys[ 15 ].addActionListener(buttonHandler); //<<<<<<<<<<<<<
    keys[ 15 ].setToolTipText("Integer division.");


      // set keyPadJPanel layout to grid layout
      keyPadJPanel = new JPanel();
      keyPadJPanel.setLayout( new GridLayout( 4, 4 ) );
      //4rows w 4 colomn

      // add buttons to keyPadJPanel panel
      // 7, 8, 9, divide
      for(int i=7; i<=10;i++)
    	  keyPadJPanel.add(keys[i]);
      // 4, 5, 6 multiply
      for (int i=4;i<=6;i++)
    	  keyPadJPanel.add(keys[i]);
    	  keyPadJPanel.add(keys[11]);
      // 1, 2, 3 subtract
      for(int i=1; i<=3; i++)
    	  keyPadJPanel.add(keys[i]);
      keyPadJPanel.add(keys[12]);

      keyPadJPanel.add(keys[0]);// 0
      keyPadJPanel.add(keys[15]); // ., =, add
      keyPadJPanel.add(keys[14]);
      keyPadJPanel.add(keys[13]);


      // add components to (default) border layout
    	  add(keyPadJPanel, BorderLayout.CENTER);
    	  add(lcdJTextField, BorderLayout.NORTH);

   } // end CalculatorFrame constructor

   private class ButtonHandler implements ActionListener //<<<<<<<<<<<<<
   {
	   public void actionPerformed(ActionEvent event) {
		   JOptionPane.showMessageDialog(null,String.format(
			"You pressed: %s", event.getActionCommand() ));

		 // lcdJTextField.setText(even.getActionCommand());
	   }

   }
} // end class CalculatorFrame

----------


## Caps Lock

وهـاد التستر 


// Program creates a GUI that resembles a calculator.
import javax.swing.JFrame;

public class Calculator
{
   public static void main( String[] args )
   {
      CalculatorFrame calculatorFrame = new CalculatorFrame();
      calculatorFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      calculatorFrame.setSize( 200, 225 ); // set frame size
      calculatorFrame.setVisible( true ); // display frame
   } // end main
}  // end class Calculator

----------

