I\'ve made a calculator program in java and i need to rewrite the code to use as
ID: 3855649 • Letter: I
Question
I've made a calculator program in java and i need to rewrite the code to use as an applet. Can someone please rewrite this code and explain the changes made in order to make the below code run as an applet?
My Code:
package calculator;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class BasicCalculator1 implements ActionListener{//attributes
protected JFrame calWin;
protected JPanel keyPad;
protected JPanel panel;
protected JTextField show;
protected JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
protected JButton add, sub, mul, div, com, clr;
protected double o1, o2;
protected char op;
//constructors
public BasicCalculator1(){
clean();}
//methods
public void clean(){
o1 = 0;
o2 = 0;
op = ' ';}
public void createUserInterface()
{
calWin = new JFrame("Calculator");
keyPad = new JPanel();
keyPad.setLayout(new GridLayout(4,4,4,4));
show = new JTextField(15);
show.setEditable(false);
show.setBackground(Color.YELLOW);
show.setFont(new Font("SERIF", Font.PLAIN, 32));
b0 = new JButton("0");
b0.setBackground(Color.CYAN);
b0.setFont(new Font("SERIF", Font.PLAIN, 32));
b1 = new JButton("1");
b1.setBackground(Color.CYAN);
b1.setFont(new Font("SERIF", Font.PLAIN, 32));
b2 = new JButton("2");
b2.setBackground(Color.CYAN);
b2.setFont(new Font("SERIF", Font.PLAIN, 32));
b3 = new JButton("3");
b3.setBackground(Color.CYAN);
b3.setFont(new Font("SERIF", Font.PLAIN, 32));
b4 = new JButton("4");
b4.setBackground(Color.CYAN);
b4.setFont(new Font("SERIF", Font.PLAIN, 32));
b5 = new JButton("5");
b5.setBackground(Color.CYAN);
b5.setFont(new Font("SERIF", Font.PLAIN, 32));
b6 = new JButton("6");
b6.setBackground(Color.CYAN);
b6.setFont(new Font("SERIF", Font.PLAIN, 32));
b7 = new JButton("7");
b7.setBackground(Color.CYAN);
b7.setFont(new Font("SERIF", Font.PLAIN, 32));
b8 = new JButton("8");
b8.setBackground(Color.CYAN);
b8.setFont(new Font("SERIF", Font.PLAIN, 32));
b9 = new JButton("9");
b9.setBackground(Color.CYAN);
b9.setFont(new Font("SERIF", Font.PLAIN, 32));
add = new JButton("+");
add.setBackground(Color.RED);
add.setFont(new Font("SERIF", Font.PLAIN, 32));
sub = new JButton("-");
sub.setBackground(Color.RED);
sub.setFont(new Font("SERIF", Font.PLAIN, 32));
mul = new JButton("*");
mul.setBackground(Color.RED);
mul.setFont(new Font("SERIF", Font.PLAIN, 32));
div = new JButton("/");
div.setBackground(Color.RED);
div.setFont(new Font("SERIF", Font.PLAIN, 32));
com = new JButton("=");
com.setBackground(Color.RED);
com.setFont(new Font("SERIF", Font.PLAIN, 32));
clr = new JButton("Clear");
clr.setBackground(Color.RED);
clr.setFont(new Font("SERIF", Font.PLAIN, 20));
calWin.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);}});
panel = new JPanel();
panel.setSize(300,300);
BorderLayout layout = new BorderLayout();
layout.setHgap(10);
layout.setVgap(4);
panel.setLayout(layout);
keyPad.add(b0);
keyPad.add(b1);
keyPad.add(b2);
keyPad.add(b3);
keyPad.add(b4);
keyPad.add(b5);
keyPad.add(b6);
keyPad.add(b7);
keyPad.add(b8);
keyPad.add(b9);
keyPad.add(add);
keyPad.add(sub);
keyPad.add(mul);
keyPad.add(div);
keyPad.add(com);
keyPad.add(clr);
panel.add(show,BorderLayout.NORTH);
panel.add(keyPad,BorderLayout.CENTER);
calWin.getContentPane().add(panel);
calWin.setBackground(Color.lightGray);
calWin.setSize(400, 400);
calWin.setVisible(true);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
com.addActionListener(this);
clr.addActionListener(this);}
public void actionPerformed(ActionEvent e){
if(e.getSource() == b0){
show.setText(show.getText() + "0");}
if(e.getSource() == b1){
show.setText(show.getText() + "1");}
if(e.getSource() == b2){
show.setText(show.getText() + "2");}
if(e.getSource() == b3){
show.setText(show.getText() + "3");}
if(e.getSource() == b4){
show.setText(show.getText() + "4");}
if(e.getSource() == b5){
show.setText(show.getText() + "5");}
if(e.getSource() == b6){
show.setText(show.getText() + "6");}
if(e.getSource() == b7){
show.setText(show.getText() + "7");}
if(e.getSource() == b8){
show.setText(show.getText() + "8");}
if(e.getSource() == b9){
show.setText(show.getText() + "9");}
if(e.getSource() == add){
o1 = Double.parseDouble(show.getText());
op = '+';
show.setText("");}
if(e.getSource() == sub){
if(show.getText().trim().equals("")){
show.setText("-");}
else{
o1 = Double.parseDouble(show.getText());
op = '-';
show.setText("");
}
}
if(e.getSource() == mul){
o1 = Double.parseDouble(show.getText());
op = '*';
show.setText("");}
if(e.getSource() == div){
o1 = Double.parseDouble(show.getText());
op = '/';
show.setText("");}
if(e.getSource() == com){
o2 = Double.parseDouble(show.getText());
calculate();}
if(e.getSource() == clr){
show.setText("");
clean();
}
}
public void calculate(){
switch(op){
case '+': show.setText(new Double(o1 + o2).toString());break;
case '-': show.setText(new Double(o1 - o2).toString());break;
case '*': show.setText(new Double(o1 * o2).toString());break;
case '/': show.setText(new Double(o1 / o2).toString());break;
}
}
public static void main(String[] args){
try {UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());}
catch (Exception e) {
e.printStackTrace();
}
BasicCalculator1 c;
c = new BasicCalculator1();
c.createUserInterface();
}
}
Explanation / Answer
Hi please find below a calculator example with applet use its simple you can make few changes to get the applet
package yourPackage;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class calculator extends Applet implements ActionListener
{
int value1,value2,result;
TextField filed1;
Button b[]=new Button[10];
Button add,sub,mul,div,clear,mod,EQ;
char operation;
public void init()
{
Color k=new Color(120,90,90);
setBackground(k);
filed1=new TextField(10);
GridLayout gl=new GridLayout(4,5);
setLayout(gl);
for(int i=0;i<10;i++)
{
b[i]=new Button(""+i);
}
add=new Button("add");
sub=new Button("sub");
mul=new Button("mul");
div=new Button("div");
mod=new Button("mod");
clear=new Button("clear");
EQ=new Button("EQ");
filed1.addActionListener(this);
add(filed1);
for(int i=0;i<10;i++)
{
add(b[i]);
}
add(add);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
char ch=str.charAt(0);
if ( Character.isDigit(ch))
filed1.setText(filed1.getText()+str);
else
if(str.equals("add"))
{
value1=Integer.parseInt(filed1.getText());
operation='+';
filed1.setText("");
}
else if(str.equals("sub"))
{
value1=Integer.parseInt(filed1.getText());
operation='-';
filed1.setText("");
}
else if(str.equals("mul"))
{
value1=Integer.parseInt(filed1.getText());
operation='*';
filed1.setText("");
}
else if(str.equals("div"))
{
value1=Integer.parseInt(filed1.getText());
operation='/';
filed1.setText("");
}
else if(str.equals("mod"))
{
value1=Integer.parseInt(filed1.getText());
operation='%';
filed1.setText("");
}
if(str.equals("clear"))
{
filed1.setText("");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.