0

Cannot understand the use of :public boolean action(Event event, Object object) { repaint(); return true ; } }. Am making an that would return the sum of two numbers. If I don't use.

Public boolean action (Event event, Object object) {

repaint();

return true ; 

I can enter the number in the text field but it won't generate the sum. Why?

}}

import java.awt.*;

import java.applet .*;

public class user extends Applet{

TextField text1,text2;

public void init(){

text1=new TextField(8);

text2=new TextField(8);

add(text1);

add(text2);

text1.setText("0");

text2.setText("0");}

public void paint(Graphics g){

int x =0,y=0,z=0;

String s1,s2,s ;

g.Drawstring("input a no in.each box",10,50);

try{

s1=text1.getText();

x=Integer.parseInt(s1);

s2=text1.getText();

y=Integer.parseInt(s2); 

}

catch(Exception e){} 

z=x +y ;

s=String.valueOf(z);

g.drawString("The sum is:",10,75);

g.drawString(s,100,75);

}

public Boolean action (Event event, Object object ) 

{

repaint();

return true ;

}}
user187744
  • 71
  • 1
  • 1
  • 7
  • 1) Why code an applet? If it is due to the teacher specifying it, 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 use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. 3) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! ... – Andrew Thompson Aug 17 '15 at 14:37
  • ... 4) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson Aug 17 '15 at 14:37

2 Answers2

2

You need to change this line:

public Boolean action (Event event, Object object ) 

to this:

public boolean action (Event event, Object object ) 

Note the lowercase b in boolean. Boolean and boolean are not the same thing.

tddmonkey
  • 20,798
  • 10
  • 58
  • 67
1

You're returning a Boolean wrapper object instead of a primitive Boolean. Change your return type to 'boolean' (Lowercase)

andrewdleach
  • 2,458
  • 2
  • 17
  • 25