0

t1.setText(String.valueOf(v)); is not interactive than t2.setText(String.valueOf(v)) Here if i press one integer(two it will show both previous and current integer) valueu t1 doesn't show,but t2 will give the immidiate reflection on data change

import java.applet.*;
import java.awt.*;

import java.awt.event.*;

public class aacal extends Applet implements ActionListener{
    static int cv,pv,v,res;
    String cap; 
    Button b1=new Button("1");
    Button b2=new Button("2");
    Button b3=new Button("+");
    Button b4=new Button("=");
    TextField t1=new TextField(10);
    TextField t2=new TextField(10);
    public void init(){     
        // This textfield is to display the result and integer on buttons
        add(t1);
        t1.addActionListener(this);
        t1.setText("0");

        b1.addActionListener(this);
        add(b1);
        b2.addActionListener(this);
        add(b2);    
        b3.addActionListener(this);
        add(b3);        
        b4.addActionListener(this);
        add(b4);        
        //This textfield is to display the ActionCommand on buttons
        add(t2);
        t2.addActionListener(this);
        t2.setText("0");            
    }
    public void actionPerformed(ActionEvent ae){
        cap=ae.getActionCommand();  
        try{
            cv=Integer.parseInt(t2.getText());
            v=cv*10+Integer.parseInt(cap);          
            t2.setText(String.valueOf(v));
            t1.setText(String.valueOf(v));      
        }catch(NumberFormatException ne){
            try{
                t2.setText(cap);
            }catch(NumberFormatException e){
                t2.setText("BYE");
            }
        }
        switch(cap){
            case "+":
                pv=cv;                  
                break;
            case"=":                
                res=cv+pv;
                t1.setText(String.valueOf(res));
                break;      
        }           
    }
}   
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
radha
  • 1
  • 3
  • 1
    1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson May 07 '14 at 08:04

1 Answers1

0

You have an exception in v=cv*10+Integer.parseInt(cap); which causes t2.setText(cap); to be executed.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40