This Applet consists of 3 labels and 4 buttons named Add, Sub, Mul, Divide. Two labels are taken as input i.e 250 and 55. An event is created on button and result is displayed as a label.
For Command Prompt: Before compilation set path for your file is saved.
Java file:
Save it as TwoNumbers.java
Compile java file : javac TwoNumbers.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class TwoNumbers extends Applet implements ActionListener
{
String s="";
Label r;
Button addd,sub,mul,div; // Button reference variables
public void init()
{
add(new Label("250")); // add Java Label
add(new Label("55"));
r= new Label(" ");
addd = new Button("Add"); // convert reference variables into objects
sub = new Button("Sub");
mul = new Button("Mul");
div = new Button("Divide");
add(addd); // add each Java button
add(sub);
add(mul);
add(div);
add(r); // add each Java Label
addd.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == addd)
r.setText(" "+(250+55));
else if(e.getSource() == sub)
r.setText(""+(250-55));
else if(e.getSource() == mul)
r.setText(""+(250*55));
else if(e.getSource() == div)
r.setText(""+(250/55));
}
}
Applet File:
Save it as: Applet.html
Run: appletviewer Applet.html
<html>
<body>
<applet code="TwoNumbers" width=400 height=400>
</applet>
</body>
</html>
For Command Prompt: Before compilation set path for your file is saved.
Java file:
Save it as TwoNumbers.java
Compile java file : javac TwoNumbers.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class TwoNumbers extends Applet implements ActionListener
{
String s="";
Label r;
Button addd,sub,mul,div; // Button reference variables
public void init()
{
add(new Label("250")); // add Java Label
add(new Label("55"));
r= new Label(" ");
addd = new Button("Add"); // convert reference variables into objects
sub = new Button("Sub");
mul = new Button("Mul");
div = new Button("Divide");
add(addd); // add each Java button
add(sub);
add(mul);
add(div);
add(r); // add each Java Label
addd.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == addd)
r.setText(" "+(250+55));
else if(e.getSource() == sub)
r.setText(""+(250-55));
else if(e.getSource() == mul)
r.setText(""+(250*55));
else if(e.getSource() == div)
r.setText(""+(250/55));
}
}
Applet File:
Save it as: Applet.html
Run: appletviewer Applet.html
<html>
<body>
<applet code="TwoNumbers" width=400 height=400>
</applet>
</body>
</html>
No comments:
Post a Comment