An applet is a Java program that runs in a Web browser.It takes user input at the run time in applet window.Due to this it used some built in functions that is stored in import java.awt.* and import java.applet.*.The functions used are:
1.Integer.parseInt() :-Change the user input to integer because the value we have entered is in integer but it will be interpreted as a String.
2.Repaint():- when you want to re-draw your GUI because you have changed something inside.
3. Draw String():-It Returns the specified text at specified location.
WAP to implement add of two numbers from user input in applet
import java.awt.*; //creating user interfaces and for painting graphics and images.
import java.applet.*; //Provides the necessary classes to create an applet.
public class userInput extends Applet
{
TextField text1, text2; //TextField is keyword that creates the test fields
public void init() //To initialize the applet
{
text1 = new TextField(8); //It specifies the length of text field
text2 = new TextField(8);
add(text1);
add(text2);
}
public void paint(Graphics g)
{
int x=0,y=0,z=0;
String s1,s2,s;
// It Returns the specified text at specified location.
g.drawString("Input a numbers which you want to add ",10,50);
try
{
s1 = text1.getText();
x = Integer.parseInt(s1); // Change the user input to integer
s2 = text2.getText();
y = Integer.parseInt(s2);}
catch(Exception e){}
z = x + y;
s = String.valueOf(z); //Returns the string representation of integer value
g.drawString("The Sum is : ",10,75);
g.drawString(s,100,75);
}
public boolean action(Event event, Object obj)
{
repaint(); //when you want to re-draw your GUI
return true;
}
}
Now save this program with name userInput.java and then compile it.
Now open new notepad file and write the following code:
<html>
<body>
<applet code="userInput.class",width="200",height="200">
</applet>
</body>
</html>
Then save this file with name app.html in same folder where you have saved app.java file.
Output of this program:
1.Integer.parseInt() :-Change the user input to integer because the value we have entered is in integer but it will be interpreted as a String.
2.Repaint():- when you want to re-draw your GUI because you have changed something inside.
3. Draw String():-It Returns the specified text at specified location.
WAP to implement add of two numbers from user input in applet
import java.awt.*; //creating user interfaces and for painting graphics and images.
import java.applet.*; //Provides the necessary classes to create an applet.
public class userInput extends Applet
{
TextField text1, text2; //TextField is keyword that creates the test fields
public void init() //To initialize the applet
{
text1 = new TextField(8); //It specifies the length of text field
text2 = new TextField(8);
add(text1);
add(text2);
}
public void paint(Graphics g)
{
int x=0,y=0,z=0;
String s1,s2,s;
// It Returns the specified text at specified location.
g.drawString("Input a numbers which you want to add ",10,50);
try
{
s1 = text1.getText();
x = Integer.parseInt(s1); // Change the user input to integer
s2 = text2.getText();
y = Integer.parseInt(s2);}
catch(Exception e){}
z = x + y;
s = String.valueOf(z); //Returns the string representation of integer value
g.drawString("The Sum is : ",10,75);
g.drawString(s,100,75);
}
public boolean action(Event event, Object obj)
{
repaint(); //when you want to re-draw your GUI
return true;
}
}
Now save this program with name userInput.java and then compile it.
Now open new notepad file and write the following code:
<html>
<body>
<applet code="userInput.class",width="200",height="200">
</applet>
</body>
</html>
Output of this program:
No comments:
Post a Comment