0

hi :)))
I have a pro. here which I can't use the keyListener and the buttons (ActionListener )at the same time !!
I can use once a time either the keylistener OR ActionListener !
and in my prog. I should use both of them at the same time !
what happened to me >>is that when I use the keyboard everything goes right >>but if I pressed into For eg :
8 (Keyboard) + (buttons -> (KeyListener)) then if I try to press to 9 (Keyboard) it will not press and set anything to the textarea and also I can't use all of the keyListener again until I'll run the prog. once again !
I don't know why this is happening !!

plz help me :(
thanks :))
and this is the code ^_^

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

    public class calculator extends JFrame {
    private JTextField T;
    private String Operator =  "/*-+=";
    private String number = ".0123456789";
    private String  command = "+";              // Intaialization
    private String change = "N";
    private String txt = "";
    private String temp = "";

    private String arg = "";
    private Double result = 0.0;
  // KeyBoard
    private JButton B_On,B_Shift,B_exp,B_Log,Btan_1,Bcos_1;
    private JButton Bsin_1,B_sqrt,B_tan,B_sin,B_cos,Bsinch;
    private JButton B_cosh, B_tansh,Bfactorial,BInverseOfX,BAc,BDel;
    private JButton B_9,B_8,B_7,B_6,B_5,B_4;
    private JButton B_3,B_2,B_1,B_0,B_Multi,B_Div;
    private JButton B_add,B_sub,B_equal,B_answ,B_dot,B_power;
    private JButton B_abs,B_Topower;
    private Double tot2=0.0;
    private Double tot1 = 0.0;
    private char operator ;
    public calculator() {

    super("Calculator");
    setVisible(true);

    setLayout(null);
    T = new JTextField("0");
    T.setHorizontalAlignment(FlowLayout.TRAILING);
    T.setSize(360, 30);
    T.setLocation(40, 20);
    add(T);
    B_On=new JButton("ON");
     B_On.setSize(60,30);
     B_On.setLocation(340,60);
     add(B_On);
     B_Shift=new JButton("Shift");
     B_Shift.setSize(60,30);
     B_Shift.setLocation(40,60);
     add(B_Shift);
     B_exp=new JButton("exp");
     B_exp.setSize(60,30);
     B_exp.setLocation(340,100);
     add(B_exp);
     B_Log=new JButton("Log");
     B_Log.setSize(70,30);
     B_Log.setLocation(240,100);
     add(B_Log);
     Btan_1=new JButton("tan-1");
     Btan_1.setSize(70,30);
     Btan_1.setLocation(240,140);
     add(Btan_1);
     Bcos_1=new JButton("cos-1");
     Bcos_1.setSize(70,30);
     Bcos_1.setLocation(140,140);
     add(Bcos_1);
     Bsin_1=new JButton("Sin-1");
     Bsin_1.setSize(70,30);
     Bsin_1.setLocation(40,140);
     add(Bsin_1);
     B_sqrt=new JButton("sqrt");
     B_sqrt.setSize(60,30);
     B_sqrt.setLocation(340,180);
     add(B_sqrt);
     B_tan=new JButton("Tan");
     B_tan.setSize(70,30);
     B_tan.setLocation(240,180);
     add(B_tan);
     B_sin=new JButton("Sin");
     B_sin.setSize(70,30);
     B_sin.setLocation(140,180);
     add(B_sin);
     B_cosh=new JButton("Cosh");
     B_cosh.setSize(70,30);
     B_cosh.setLocation(40,180);
     add( B_cosh);
     Bsinch=new JButton("sinch");
     Bsinch.setSize(70,30);
     Bsinch.setLocation(140,100);
     add( Bsinch);
     B_cos=new JButton("cos");
     B_cos.setSize(60,30);
     B_cos.setLocation(340,140);
     add( B_cos);
     B_tansh=new JButton("tansh");
     B_tansh.setSize(70,30);
     B_tansh.setLocation(40,100);
     add( B_tansh);
     Bfactorial=new JButton("X!");
     Bfactorial.setSize(60,30);
     Bfactorial.setLocation(340,220);
     add( Bfactorial);
     BInverseOfX=new JButton("1/x");
     BInverseOfX.setSize(70,30);
     BInverseOfX.setLocation(240,220);
     add( BInverseOfX);
     B_abs=new JButton("| |");
     B_abs.setSize(70,30);
     B_abs.setLocation(140,220);
     add( B_abs);

     B_Topower=new JButton("^");
     B_Topower.setSize(70,30);
     B_Topower.setLocation(40,220);
     add( B_Topower);

     BAc=new JButton("Ac");
     BAc.setSize(60,30);
     BAc.setLocation(340,260);
     add( BAc);
     BDel=new JButton("Del");
     BDel.setSize(60,30);
     BDel.setLocation(265,260);
     add( BDel);
     B_9=new JButton("9");
     B_9.setSize(60,30);
     B_9.setLocation(190,260);
     add( B_9);

     B_8=new JButton("8");
     B_8.setSize(60,30);
     B_8.setLocation(115,260);
     add( B_8);

     B_7=new JButton("7");
     B_7.setSize(60,30);
     B_7.setLocation(40,260);
     add( B_7);

     B_6=new JButton("6");
     B_6.setSize(60,30);
     B_6.setLocation(190,300);

     add( B_6);

     B_5=new JButton("5");
     B_5.setSize(60,30);
     B_5.setLocation(115,300);
     add( B_5);

     B_4=new JButton("4");
     B_4.setSize(60,30);
     B_4.setLocation(40,300);
     add(  B_4);

     B_3=new JButton("3");
     B_3.setSize(60,30);
     B_3.setLocation(190,340);
     add( B_3);

     B_2=new JButton("2");
     B_2.setSize(60,30);
     B_2.setLocation(115,340);
     add( B_2);

     B_1=new JButton("1");
     B_1.setSize(60,30);
     B_1.setLocation(40,340);
     add( B_1);

     B_0=new JButton("0");
     B_0.setSize(60,30);
     B_0.setLocation(40,380);
     add( B_0);

     B_Multi=new JButton("*");
     B_Multi.setSize(60,30);
     B_Multi.setLocation(265,300);
     add(B_Multi);

     B_Div=new JButton("/");
     B_Div.setSize(60,30);
     B_Div.setLocation(340,300);
     add( B_Div);

     B_add=new JButton("+");
     B_add.setSize(60,30);
     B_add.setLocation(265,340);
     add(B_add);

     B_sub=new JButton("-");
     B_sub.setSize(60,30);
     B_sub.setLocation(340,340);
     add( B_sub);

     B_equal=new JButton("=");
     B_equal.setSize(60,30);
     B_equal.setLocation(340,380);
     add( B_equal);

     B_answ=new JButton("Ans");
     B_answ.setSize(60,30);
     B_answ.setLocation(265,380);
     add(B_answ);


     B_dot=new JButton(".");
     B_dot.setSize(60,30);
     B_dot.setLocation(115,380);
     add( B_dot);

     B_power=new JButton("(-)");
     B_power.setSize(60,30);
     B_power.setLocation(190,380);
     add(B_power);
     Inner Handler =new Inner ();
     Inner2 Handler2 =new Inner2();


        addKeyListener(Handler2);
        B_On.addActionListener(Handler);
        B_Shift.addActionListener(Handler);
        B_exp.addActionListener(Handler);
        B_Log.addActionListener(Handler);
        Btan_1.addActionListener(Handler);
        Bcos_1.addActionListener(Handler);
        Bsin_1.addActionListener(Handler);
        B_sqrt.addActionListener(Handler);
        B_tan.addActionListener(Handler);
        B_sin.addActionListener(Handler);
        B_cos.addActionListener(Handler);
        Bsinch.addActionListener(Handler);
        B_cosh.addActionListener(Handler);
        B_tansh.addActionListener(Handler);
        Bfactorial.addActionListener(Handler);
        BInverseOfX.addActionListener(Handler);
        BAc.addActionListener(Handler);
        BDel.addActionListener(Handler);

         B_9.addActionListener(Handler);
         B_8.addActionListener(Handler);
         B_7.addActionListener(Handler);
         B_6.addActionListener(Handler);
         B_5.addActionListener(Handler);
         B_4.addActionListener(Handler);

        B_3.addActionListener(Handler);
        B_2.addActionListener(Handler);
        B_1.addActionListener(Handler);
        B_0.addActionListener(Handler);
        B_Multi.addActionListener(Handler);
        B_Div.addActionListener(Handler);

        B_add.addActionListener(Handler);
        B_sub.addActionListener(Handler);
        B_equal.addActionListener(Handler);

        B_answ.addActionListener(Handler);
        B_dot.addActionListener(Handler);
        B_power.addActionListener(Handler);
        B_abs.addActionListener(Handler);
        B_Topower.addActionListener(Handler);

        setFocusable(true);
    }
 //KeyListener
    /*requestFocusInWindow();*/
    private class Inner2 extends KeyAdapter{
        public void keyTyped(KeyEvent E){}
          public void keyReleased(KeyEvent E)
          {
           int ikey = E.getKeyCode();
          // T.requestFocus();
           if (ikey == 127) T.setText("");
          }
        public void keyPressed(KeyEvent E)
          {
            //T.requestFocus();
             int ikey = E.getKeyChar();
            char ckey = (char) ikey;

             if (ckey == 'C') 
               {
               Del();
               }
             else if (ckey == 'd') 
           {
           T.setText("0");
           }
           else if (ckey == '=')
           {
            arg = "=";
            txt = T.getText();
            displayGeneral();// fun equal
           }
           else
           {
            txt = T.getText();
            arg = String.valueOf(ckey);
            if (arg.equals(".")) {
                String S=T.getText();
                if (!S.isEmpty()){
               if ( T.getText().contains(".")){
                     // there's already a . in the text, so end
                  T.setText("Error");
                     return;
                }
               else {
                    T.setText(Integer.toString(Integer.parseInt(S)) + ".");
               }

               }
                else {
                 T.setText("0.");
                }
             } // if dot

          if (E.isControlDown() && E.getKeyCode()== KeyEvent.VK_C){

              temp=T.getText();

          }
          else if ( E.isControlDown() && E.getKeyCode()== KeyEvent.VK_P)

          {
             if (T.getText().isEmpty()){
              T.setText(temp);

             }
             else {

                  T.setText(T.getText()+temp);

             }



          }
          else if ( E.isControlDown() && E.getKeyCode()== KeyEvent.VK_W){

              //setVisible(false);
              System.exit(0);

          }
            if(number.indexOf(arg) > -1)
            {
             if (change == "Y") txt = "";
             txt = txt + arg;
             T.setText(txt);
             change = "N";


            }
            else check_entry();

           }

         }//keytyped
        }//innerClass
    //ActionListener
    private class Inner implements ActionListener {

    public void actionPerformed(ActionEvent E) {
    String event = ((JButton) E.getSource()).getText();
    // Added this validation, just in case
    if (event == null || event.isEmpty()) {
    System.out.println("NO EVENT RECEIVED");
    return;
    }
    String out = T.getText();
    //String String_output;
    // String result_out=new String ();
    if (event.equals("tansh")) {
    T.setText(Double.toString(Math.tanh(Double.parseDouble(out))));
    } 
    else if (event.equals("Cosh")) {
    T.setText(Double.toString(Math.cosh(Double.parseDouble(out))));
    }
    else if (event.equals("sinch")) {
    T.setText(Double.toString(Math.sinh(Double.parseDouble(out))));
    }
    else if (event.equals("Tan")) {
    T.setText(Double.toString(Math.tan(Double.parseDouble(out))));
    }
    else if (event.equals("Sin")) {
    T.setText(Double.toString(Math.sin(Double.parseDouble(out))));
    }
    else if (event.equals("cos")) {
    T.setText(Double.toString(Math.cos(Double.parseDouble(out))));
    }
    else if (event.equals("Log")) {
    T.setText(Double.toString(Math.log(Double.parseDouble(out))));
    } 
    else if (event.equals("exp")) {
    T.setText(Double.toString(Math.exp(Double.parseDouble(out))));
    } 
    else if (event.equals("cos-1")) {
    T.setText(Double.toString(Math.acos(Double.parseDouble(out))));
    }
    else if (event.equals("Sin-1")) {
    T.setText(Double.toString(Math.asin(Double.parseDouble(out))));
    }
    else if (event.equals("tan-1")) {
    T.setText(Double.toString(Math.atan(Double.parseDouble(out))));
    }
    else if ("0123456789".contains(event)) {
    numberButton(event, out);
    }
    else if (event.equals(".")) {
       String S=T.getText();
       if (!S.isEmpty()){
      if ( T.getText().contains(".")){
            // there's already a . in the text, so end
          T.setText("Error");
            return;
       }
      else {
            T.setText(Integer.toString(Integer.parseInt(out)) + ".");
      }

      }
       else {
        T.setText("0.");
        //T.setText(Integer.toString(Integer.parseInt(out)) + ".");  
       }
    } //dot

    else if (event.equals("Ac")) {
    T.setText("0");
    } 
    else if (event.equals("Del")) {
    String S = T.getText();
    if (!S.isEmpty()) {
    StringBuilder Sb = new StringBuilder(S);
    int size = Sb.length();
    Sb.deleteCharAt(size - 1);
    String result_string = new String(Sb);
    T.setText(result_string);
    }
    else {
    T.setText("0");
    }
    } 
    else if (event.equals("sqrt")) {
    T.setText(Double.toString(Math.sqrt(Double.parseDouble(out))));
    } 

    else if (event.equals("+")) {
        String B_text=B_add.getText();
        Math_Operator(B_text);

    } 
    else if (event.equals("-")) {
        String B_text=B_sub.getText();
        Math_Operator(B_text);

    } 
    else if (event.equals("*")) {
        String B_text=B_Multi.getText();
        Math_Operator(B_text);

    } 
    else if (event.equals("/")) {
        String B_text=B_Div.getText();
        Math_Operator(B_text);

    } 
    else if (event.equals("=")) {
        switch(operator){
        case'+':
            tot2=tot1+Double.parseDouble(T.getText());
            break;
        case'-':
            tot2=tot1-Double.parseDouble(T.getText());

        break;
        case'*':
            tot2=tot1*Double.parseDouble(T.getText());

        break;
        case'/':tot2=tot1/Double.parseDouble(T.getText());

        break;
        case '^':tot2=Math.pow(tot1,Double.parseDouble(T.getText()));

        }
        T.setText(Double.toString(tot2));
        tot1=0.0;

    }
    else if (event.equals("| |")) {
    T.setText(Double.toString(Math.abs(Double.parseDouble(out))));
    } 
    else if (event.equals("Ans")) {
        T.setText(Double.toString(tot2));
        } 
    else if (event.equals("(-)")) {

        if (T.getText().isEmpty()){
            T.setText("-");
        }
        else if(T.getText().equals("0")){
            T.setText("");
            T.setText("-");
        }
        else{
        T.setText(Integer.toString(-1*(Integer.parseInt(out))));}
        } 
    else if (event.equals("1/x")) {
    T.setText(Double.toString(1 / Double.parseDouble(out)));
    } 
    else if (event.equals("X!")) {
    int f = 1;
    String s = T.getText();
    if (!s.isEmpty()) {
    char c[] = new char[10];
    c = s.toCharArray();
    boolean a = true;
    for (int m = 0; m < c.length; m++) {
    if (!Character.isDigit(c[m])) {
    a = false;
    }
    }
    if (!a) {
    T.setText("Error");
    }
    else {
    int num = Integer.parseInt(out);
    for (int v = 1; v <= num; v++) {
    f = f * v;
    }
    String result_string = Integer.toString(f);
    T.setText(result_string);
    }
    }
    else {
    T.setText("0");
    }
    }

    else {
        String B_text=B_Topower.getText();
        Math_Operator(B_text);

    }
    }
    }
    // all the fuctions
    private void Math_Operator(String button){
        operator =button.charAt(0);
        String t=T.getText();
        tot1=tot1+Double.parseDouble(T.getText());
        T.setText("");

    }


    private void numberButton(String number, String out) {
    String s = T.getText();
    if (!s.isEmpty()) {
    char c[] = new char[10];
    c = s.toCharArray();
    boolean a = true;

    for (int m = 0; m < c.length; m++) {
    if (!Character.isDigit(c[m])) {
    a = false;
    break;

    } 

    }
    if (!a) {
    T.setText(T.getText() + number);

    }
    else {
    int num = Integer.parseInt(out);
    String result_string = Integer.toString(num);
    T.setText(result_string + number);
    }
    }
    else {
    T.setText(number);
    }
    }

    public String computeOperations(String t, String c)//func compute
    {
     Double Outnum = Double.valueOf(t);

     if (c.equals("+")) result = result + Outnum;
     else if (c.equals("-"))
         {
         result = result - Outnum;
         }
     else if (c.equals("*"))
         {
         result = result * Outnum;
         }
     else if (c.equals("/")){
         result = result / Outnum;
     }

     return String.valueOf(result);
    }

    public void displayGeneral()
    {
     T.setText(computeOperations(txt,command));// func compute
     change = "Y";
     command = "+"; 
     result = 0.0;
    }
    public void Del(){
         String S = T.getText();
            if (!S.isEmpty()) {
            StringBuilder Sb = new StringBuilder(S);
            int size = Sb.length();
            Sb.deleteCharAt(size - 1);
            String result_string = new String(Sb);
            T.setText(result_string);
            }
            else {
            T.setText("0");
            }

    } 

    public void check_entry()
    {
     if(arg.equals("=")) {
         displayGeneral();
     }

     else if(Operator.indexOf(arg) > -1)
     {
      T.setText(computeOperations(txt,command));
      command = arg;
      change = "Y";
     }

    }

    }//end od class

-----------------------------------
Main Class

import javax.swing.JFrame;
public class Main {

    public static void main(String[] args) {
        calculator v=new calculator();
        v.setSize(450,500);
        v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

}
2
Contributors
1
Reply
8
Views
2 Years
Discussion Span
Last Post by JamesCherrill
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.