Saturday, 11 August 2012

ActionListener Example

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.ArrayList;


public class makine extends JApplet{

 
 private int counter = 0;
 
 
 private ArrayList<String> liste;
 private String[] yazi;
 private JButton one,two,three,four,five,
 six,seven,eight,nine,zero,
 arti,eksi,bolu,carpi,CE,esit;
 
 
 
 private JTextField ekran;
 private JLabel yazar;
 private DecimalFormat dec;
 
 public String ToString(Double d)
 {
  return d.toString();
 }
 //@override
 public void init()
 { 
  yazi = new String[11];
  liste = new ArrayList<String>();
  for(int i = 0;i<11;i++){
   yazi[i] = "";
  }
  ActionHandler handler = new ActionHandler();
  Container container = this.getContentPane();
  container.setLayout(new FlowLayout(1,8,2));
  
  
  
  dec = new DecimalFormat("0.000");
  one = new JButton();
  one.setText("1");
  one.setSize(20, 20);
  one.setLocation(40, 40);
  container.add(one);
  one.addActionListener(handler);
  
  
  two = new JButton();
  two.setText("2");
  two.setSize(20, 20);
  two.setLocation(40, 40);
  container.add(two);
  two.addActionListener(handler);
  
  three = new JButton();
  three.setText("3");
  three.setSize(20, 20);
  three.setLocation(70, 40);
  container.add(three);
  three.addActionListener(handler);
  
  arti = new JButton();
  arti.setText("+");
  arti.setSize(20, 20);
  arti.setLocation(100, 40);
  container.add(arti);
  arti.addActionListener(handler);
  
  four = new JButton();
  four.setText("4");
  four.setSize(20, 20);
  four.setLocation(10, 70);
  container.add(four);
  four.addActionListener(handler);
  
  five = new JButton();
  five.setText("5");
  five.setSize(20, 20);
  five.setLocation(40, 70);
  container.add(five);
  five.addActionListener(handler);
  
  six = new JButton();
  six.setText("6");
  six.setSize(20, 20);
  six.setLocation(70, 70);
  container.add(six);
  six.addActionListener(handler);
  
  eksi = new JButton();
  eksi.setText("-");
  eksi.setSize(20, 20);
  eksi.setLocation(10, 70);
  container.add(eksi);
  eksi.addActionListener(handler);
  
  seven = new JButton();
  seven.setText("7");
  seven.setSize(20, 20);
  seven.setLocation(10, 100);
  container.add(seven);
  seven.addActionListener(handler);
  

  
  
  eight = new JButton();
  eight.setText("8");
  eight.setSize(20, 20);
  eight.setLocation(40, 100);
  container.add(eight);
  eight.addActionListener(handler);
  
  nine = new JButton();
  nine.setText("9");
  nine.setSize(20, 20);
  nine.setLocation(70, 100);
  container.add(nine);
  nine.addActionListener(handler);
  
  carpi = new JButton();
  carpi.setText("x");
  carpi.setSize(20, 20);
  carpi.setLocation(100, 130);
  container.add(carpi);
  carpi.addActionListener(handler);
  
  CE = new JButton();
  CE.setText("CE");
  CE.setSize(20, 20);
  CE.setLocation(10, 100);
  container.add(CE);
  CE.addActionListener(handler);
  
  zero = new JButton();
  zero.setText("0");
  zero.setSize(20, 20);
  zero.setLocation(40, 130);
  container.add(zero);
  zero.addActionListener(handler);
  
  
  
  
  bolu = new JButton();
  bolu.setText("/");
  bolu.setSize(20, 20);
  bolu.setLocation(100, 100);
  container.add(bolu);
  bolu.addActionListener(handler);
  
  
  
  esit = new JButton();
  esit.setText("=");
  esit.setSize(20, 20);
  esit.setLocation(70, 100);
  container.add(esit);
  esit.addActionListener(handler);
  
  ekran = new JTextField(15);
  ekran.setText(" ");
  ekran.setEditable(false);
  ekran.setDisabledTextColor(Color.BLUE );
  ekran.setVisible(true);
  ekran.setAlignmentY(JTextField.RIGHT_ALIGNMENT);
  ekran.setHorizontalAlignment(JTextField.RIGHT);
  ekran.setLocation(10,10);
  container.add(ekran);
  ekran.addActionListener(handler);
  
  yazar = new JLabel("Tested and Programmed by BY");
  container.add(yazar);
  
 }
 //inner class
 private class ActionHandler implements ActionListener
 {
  public void actionPerformed(ActionEvent event)
  {
   if(event.getSource() == one){
    if(counter<11){
     yazi[counter]+="1";
     ekran.setText(yazi[counter]);
    }else ;
   }if(event.getSource() == two){
    if(counter<11){
     yazi[counter]+="2";
     ekran.setText(yazi[counter]);
    }else ;
   }if(event.getSource() == three){
    if(counter<11){
     yazi[counter]+="3";
     ekran.setText(yazi[counter]);
    }else;
   }if(event.getSource() == four){
    if(counter<11){
     yazi[counter]+= "4";
     ekran.setText(yazi[counter]);
    }else;
   }if(event.getSource() == five){
    if(counter<11){
     yazi[counter] += "5";
     ekran.setText(yazi[counter]);
    }else;
   }if(event.getSource() == six){
    if(counter<11){
     yazi[counter] += "6";
     ekran.setText(yazi[counter]);
    }else;
   }if(event.getSource() == seven){
    if(counter<11){
     yazi[counter] += "7";
     ekran.setText(yazi[counter]);
    }else;
   }if(event.getSource() == eight){
    if(counter<11){
     yazi[counter] += "8";
     ekran.setText(yazi[counter]);
    }else;
   }if(event.getSource() == nine){
    if(counter<11){
     yazi[counter] += "9";
     ekran.setText(yazi[counter]);
    }else;
   }if(event.getSource() == zero){
    if(counter<11){
     yazi[counter] += "0";
     ekran.setText(yazi[counter]);
    }else;
   }if(event.getSource() == arti){
    try{
    if(Double.parseDouble(yazi[counter])>=0){
    counter++;
    yazi[counter] = "+";
    counter++;
    }else;
    }catch(Exception e){
     ekran.setText("Syntax Error");
    }
   }if(event.getSource() == eksi){
    try{
    if(Double.parseDouble(yazi[counter])>=0){
    counter++;
    yazi[counter] = "-";
    counter++;
    }else;
    }catch(Exception e){
     ekran.setText("Syntax Error");
    }
   }if(event.getSource() == bolu){
    try{
    if(Double.parseDouble(yazi[counter])>=0){
    counter++;
    yazi[counter] = "/";
    counter++;
    }else;
    }catch(Exception e){
     ekran.setText("Syntax Error");
    }
   }if(event.getSource() == carpi){
    try{
    if(Double.parseDouble(yazi[counter])>=0){
    counter++;
    yazi[counter] = "*";
    counter++;
    }
    }catch(Exception e){
     ekran.setText("Syntax Error");
    }
   }if(event.getSource() == CE){
    for(int i = 0;i<11;i++)
     yazi[i] = "";
     ekran.setText("");
     counter = 0;
   }if(event.getSource() == esit){
    //try{
    for(int i = 0;i<11;i++){
     liste.add(i,yazi[i]);
    }
    for(int i = 0;i<11;i++){
     try{
     if(yazi[i]=="/"){
       
     yazi[i] = ToString(Double.parseDouble(yazi[i-1])/Double.parseDouble(yazi[i+1]));
       yazi[i-1] = yazi[i];
       yazi[i+1] = yazi[i];
       liste.remove(0);
       liste.add(0,yazi[i]);
      }
     }catch(Exception e){
      ekran.setText("Syntax Error");
      counter = 0;
     }
    }
    for(int i = 0;i<11;i++){
     try{ 
     if(yazi[i]=="*"){
           yazi[i] = ToString(Double.parseDouble(yazi[i-1])*Double.parseDouble(yazi[i+1]));
       yazi[i-1] = yazi[i];
       yazi[i+1] = yazi[i];
       liste.remove(0);
       liste.add(0,yazi[i]);
       
      }
     }
     catch(Exception e){
      ekran.setText("Syntax Error");
      counter = 0;
     }
    }
    for(int i = 0;i<11;i++){
     try{
     if(yazi[i]=="+"){
                         yazi[i] = ToString(Double.parseDouble(yazi[i-1])+Double.parseDouble(yazi[i+1]));
       yazi[i-1] = yazi[i];
       yazi[i+1] = yazi[i];
       liste.remove(0);
       liste.add(0,yazi[i]);
      } 
     }
     catch(Exception e){
      ekran.setText("Syntax Error");
      counter = 0;
     }
    }
    for(int i = 0;i<11;i++){
     try{
     if(yazi[i]=="-"){
        
    yazi[i] = ToString(Double.parseDouble(yazi[i-1])-Double.parseDouble(yazi[i+1]));
       yazi[i-1] = yazi[i];
       yazi[i+1] = yazi[i];
       liste.remove(0);
       liste.add(0,yazi[i]);
      }
     }
     catch(Exception e){
      ekran.setText("Syntax Error");
      counter = 0;
     } 
    } 
     if(liste.isEmpty()){
      ekran.setText("Syntax Error");
      counter = 0;}
     else{
         if(liste.get(0)!="")
                              ekran.setText(liste.get(0).toString());
     }
     counter = 0;
     for(int i = 0;i<11;i++)
      yazi[i] = "";
     if(!liste.isEmpty())
      liste.clear();
      
    /*}catch(Exception e){
     ekran.setText("Syntax Error");
    }*/
   }
   
  }
 } 
}