i was doing a program using java applet. I want to make a simple calculator. So i added four labels,textboxes. And 6 buttons for add,multiply,divide,minus and a clear and exit button to clear the textboxes and to exit from the applet respectively.Now i want to display a message when the user clicks the add button after entering the nos in the textboxes. And the this will have to change everytime the user clicks in the various buttons. Suppose i hv entered four nos in the textboxes and i pressed the Add button then a message that 'the addition of four nos is' should be displayed. And i click the minus button then a message that Subtraction of four nos is' should be displayed. It means that a string should always change its content depending upon the action performed by the user. Please help me to solve this.
Thanks.
January 25, 2010 at 11:34 AM
Hi Friend,
Try the following code:
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class Calculator extends Applet implements ActionListener{
TextField text1,text2,text3,text4;
Label l1,l2,l3,l4;
Label lab;
Panel p;
String Add, Subtract,Multiply,Divide,Clear,Exit;
public void init(){
text1 = new TextField(12);
text2= new TextField(12);
text3 = new TextField(12);
text4 = new TextField(12);
l1=new Label("Enter Number 1:");
l2=new Label("Enter Number 2:");
l3=new Label("Enter Number 3:");
l4=new Label("Enter Number 4:");
lab=new Label();
p=new Panel(new GridLayout(1,3));
Panel p1=new Panel(new GridLayout(7,2));
Panel p2=new Panel(new GridLayout(1,1));
Button add = new Button("Add");
Button sub = new Button("Subtract");
Button mul = new Button("Multiply");
Button div = new Button("Divide");
Button clear = new Button("Clear");
Button exit = new Button("Exit");
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
clear.addActionListener(this);
exit.addActionListener(this);
p1.add(l1);
p1.add(text1);
p1.add(l2);
p1.add(text2);
p1.add(l3);
p1.add(text3);
p1.add(l4);
p1.add(text4);
p1.add(add);
p1.add(sub);
p1.add(mul);
p1.add(div);
p1.add(clear);
p1.add(exit);
p2.add(lab);
p.add(p1);
p.add(p2);
add(p);
}
public void actionPerformed(ActionEvent e){
String value1=text1.getText();
int num1=Integer.parseInt(value1);
String value2=text2.getText();
int num2=Integer.parseInt(value2);
String value3=text3.getText();
int num3=Integer.parseInt(value3);
String value4=text4.getText();
int num4=Integer.parseInt(value4);
Button source = (Button)e.getSource();
if(source.getLabel() == "Add"){
int cal=num1+num2+num3+num4;
lab.setText("Addition of four numbers is: "+cal);
}
if(source.getLabel() == "Subtract"){
int cal=num1-num2-num3-num4;
lab.setText("Subtraction of four numbers is: "+cal);
}
if(source.getLabel() == "Multiply"){
int cal=num1*num2*num3*num4;
lab.setText("Multiplication of four numbers is: "+cal);
}
if(source.getLabel() == "Divide"){
int cal=num1/num2;
lab.setText("Division of two numbers is: "+cal);
}
if(source.getLabel() == "Clear"){
text1.setText("");
text2.setText("");
text3.setText("");
text4.setText("");
}
if(source.getLabel() == "Exit"){
}
}
}
Thanks
Related Tutorials/Questions & Answers:
java applet problem - Appletjava applet problem i was doing a program using
java applet. I want... and to exit from the
applet respectively.Now i want to display a message when...*;
import java.awt.*;
public class Calculator extends
Applet implements
applet problem - Appletapplet problem How can I create a file in client side by a
java applet . Surely it will need a signed
applet .But how can a signed
applet create a file in the client side
Advertisements
problem with applet plugin - Appletproblem with
applet plugin hello friends,
iam using Eclipse IDE. i created one
applet and i want to plugin that
applet into webpage.. when i am...-->com.ezsoft.applets.Upload.java
please friends how can i resolve that
problem Problem in show card in applet.Problem in show card in
applet. The following link contained the card demo with
applet.
http://www.roseindia.net/
java/
java... On Run as
Java Applet then only show the
Applet, not show any one card,hence any
Problem with display of images in applets - Applet, this);
}
}
---------------------------------------
Display image in
Java Applet... information.
http://www.roseindia.net/
java/example/
java/
applet...
Problem with display of images in applets Hi all,
When I run
Moving Applet ProblemMoving
Applet Problem When image scroll in
applet then a blinking on image is continuously working.how to remove this blinking..
import... java.awt.Image;
public class PlaneAnimation extends
Applet implements Runnable
java applet - Appletjava applet wants to creat address bar on my
java applet. Hi
Applet don't provide a facility to create a address bar directly. You just create a text box. In this text box if you enter any http address
loading Java Applet - Appletloading
Java Applet Hi,
I use the archive tag to download my
applet into the Browser
It takes too long to load. Can I do it in several steps... control the loading within the
applet?
thanks
java applet - Appletjava applet I want to close
applet window which is open by another button of
applet program. plz tell me! Hi Friend,
Try...://www.roseindia.net/
java/example/
java/
applet/
Thanks
Java Applet Java Applet How to add Image in
Java Applet? what is getDocumentBase
java - Appletjava how to connect database table with scrollbar in
java applet java - Appletjava how to connect database table with scrollbar in
java applet java - Appletjava what is
applet? Hi Friend,
Please visit the following link:
http://www.roseindia.net/
java/example/
java/
applet/
Thanks
java appletjava applet why
java applet programs doesn't contain main method
java appletjava applet why
java applet programs doesn't contain main method
Java appletJava applet What tags are mandatory when creating HTML to display an
applet Java appletJava applet How do I go from my
applet to another JSP or HTML page
java appletjava applet If i insert in database from
applet this work, but from applet.html don't
Java AppletJava Applet Hi,
What is
Java Applet and how it can be useful in development of
Java programs?
Can anyone tell me the url to learn
Java Applet?
Thanks
Hi,
See the tutorial at: Applications and Applets
Thanks
java - Appletjava how to communicate to my servlet through
applet.
... this information to the servlet in the normal way.
The
applet pretends... is concerned, the
applet is just another HTTP client.
For more infomation
java applet java applet I want code to implement
(1) user will enter how many nodes
(2)it should allow that no. of clicks and
circle should be displayed at that
position
problem of writing to a local file ( JApplet ) - Appletproblem of writing to a local file ( JApplet ) Dear All,
I want to program a guestbook using
java applets but now I have
problem of writing to a file from JApplet.
it is working without any
problem if i run the program using
java appletjava applet Creation of
Applet for Personal Details Form
When a customer places his first order of garments, the Personal Details Form is
displayed.... The
applet must include the following validations:
ï?® The FirstName, Address1
java - Appletjava i have button browse button in my
applet form.when i click on browse button i have to go to file storage box(what ever files stored in my... in the mail).i have display that file path in my
applet form.am learner please send me
java applet progjava applet prog
applet to display scrolling text from right to left in an
applet window using thread. text should be accepted by html parameter
java - Appletjava 1.An
applet program to draw a line graph for y=2x+5.[for suitable values of x & y]
2. An
applet program to draw following shapes
(1)cone... java.applet.Applet;
public class CubeExample extends
Applet {
Stroke
The Java Applet Viewer The
Java Applet Viewer
Applet viewer is a command line program to run
Java applets...; the browser should be
Java enabled.To create an
applet, we need to define
disable keyboard in java appletdisable keyboard in
java applet How to disable key board of my client in an
java applet embedded in a website while the
applet is running.? Plz help
Java appletJava applet How do you set security in applets
Java appletJava applet How are the differences between Applets and Applications
Java appletJava applet What is the sequence for calling the methods by AWT for applets
Java appletJava applet What is the relationship between the Canvas class and the Graphics class
Java appletJava applet What is the relationship between the Canvas class and the Graphics class
java - Appletjava what is the use of java.utl Hi Friend,
The
java...://www.roseindia.net/
java/example/
java/util/
http://www.roseindia.net/
java/example/
java/util/
java-util-package.shtml
Thanks
Java appletJava applet How do I determine the width and height of my application
Java appletJava applet How can I arrange for different applets on a web page to communicate with each other
java appletjava applet create applets to accept the personal details and sales details in the database. create jdbc application to connect the applets with the database
java - Appletjava Hi,
I need very urgent code............
please help me.........
How to convert text to wave conversion?
I need
java code............
Regards,
Valarmathi
java - Applet://www.roseindia.net/
java/example/
java/swing/graphics2D/index.shtml
Thanks
Java appletJava applet I wanted as many clicks are there ,circles should be displayed there.
I tried this code but it erases previous circles.
plz help.
public void mouseClicked(MouseEvent m)
{
x=m.getX();
y=m.getY
Java appletJava applet I wanted as many clicks are there ,circles should be displayed there.
I tried this code but it erases previous circles.
plz help.
public void mouseClicked(MouseEvent m)
{
x=m.getX();
y=m.getY
Java AppletJava Applet Hi,
I have a query, on every mouse click an oval should be drawn. But in my program
I have used repaint() function, therefore previous oval gets erased.How to avoid this
so that I get more than one oval.
PFB
Java AppletJava Applet Hi,
I have a query, on every mouse click an oval should be drawn. But in my program
I have used repaint() function, therefore previous oval gets erased.How to avoid this
so that I get more than one oval.
PFB
java appletjava applet Web page that accepts a matrix as input and computes its transpose.
The web page should have two text boxes and a submit button labbeled as Input Elements.
After entering the numbers of rows of the input matrix
java appletjava applet Web page that accepts a matrix as input and computes its transpose.
The web page should have two text boxes and a submit button labbeled as Input Elements.
After entering the numbers of rows of the input matrix