You are on page 1of 3

Changing the Calculator Program to a Calculator Applet

In class we developed the Calculator program to add and subtract two numbers. The code
for that program is listed below. To cut-and-paste this program into BlueJ, do the following:
tart BlueJ as usual
tart a new, empt! pro"ect. #ame the pro"ect Calculator
$reate a new class in !our pro"ect. #ame the class CalcProgram
%pen the CalcProgram class and delete ever!thing it contains
$op! the program listed below and paste it into the CalcProgram class
ave and compile
It should compile without problems. &un the program and test it.
import javax.swing.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
public class CalcProgram extends JFrame implements ctionListener
!
J"extField num# $ new J"extField%&'.''&(;
J"extField num) $ new J"extField%&'.''&(;

JLabel answer $ new JLabel%&'.''&(;
JLabel symbol $ new JLabel%&*&(;
JLabel e+uals $ new JLabel%&$&(;

JButton addButton $ new JButton%& * &(;
JButton subButton $ new JButton%& , &(;

public CalcProgram%(
!
JPanel -ields $ new JPanel%new FlowLayout%((;
-ields.add%num#(;
-ields.add%symbol(;
-ields.add%num)(;
-ields.add%e+uals(;
-ields.add%answer(;

JPanel buttons $ new JPanel%new FlowLayout%((;
buttons.add%addButton(;
buttons.add%subButton(;

getContentPane%(.setLayout%new BorderLayout%((;
getContentPane%(.add%&.ort/&0 -ields(;
getContentPane%(.add%&Center&0 buttons(;

addButton.addctionListener%t/is(;
subButton.addctionListener%t/is(;
pac1%(;
set2isible%true(;
3

public void actionPer-ormed%ction4vent e(
!
double x $ 5ouble.parse5ouble%num#.get"ext%((;
double y $ 5ouble.parse5ouble%num).get"ext%((;
i- %e.get6ource%( $$ addButton(
!
answer.set"ext%6tring.value7-% x * y ((;
symbol.set"ext%&*&(;
3
i- %e.get6ource%( $$ subButton(
!
answer.set"ext%6tring.value7-% x , y ((;
symbol.set"ext%&,&(;
3
3

public static void main%6tring args89(
!
CalcProgram c $ new CalcProgram%(;
3
3
To turn the program into an applet, we need to ma'e "ust a few changes. The changes are
outlined here, but the new, changed code is listed below for !our reference.
(e change the name of the class to Calcpplet instead of CalcProgram. (e need
to ma'e sure that we change all occurrences of CalcProgram to Calcpplet, and
that the new class is saved as Calcpplet.java
(e remove the main method at the end of the applet
(e change the superclass from JFrame to Jpplet
(e change the constructor name )the method with the same name as the class name* to
public void init
(e remove the last two lines from the init method that sa! +pac1%(, and
+set2isible%true(,.
-ou could do these changes manuall!, or simpl! cop!-and-paste the new applet from below as
follows:
tart BlueJ and open the Calculator pro"ect. $reate a new class in !our pro"ect. #ame
that class Calcpplet
%pen the Calcpplet class and delete ever!thing it currentl! contains
$op! the applet listed below and paste it into the Calcpplet class
ave and compile
import javax.swing.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
public class CalcApplet extends JApplet implements ctionListener
!
J"extField num# $ new J"extField%&'.''&(;
J"extField num) $ new J"extField%&'.''&(;

JLabel answer $ new JLabel%&'.''&(;
JLabel symbol $ new JLabel%&*&(;
JLabel e+uals $ new JLabel%&$&(;

JButton addButton $ new JButton%& * &(;
JButton subButton $ new JButton%& , &(;

public void init()
!
JPanel -ields $ new JPanel%new FlowLayout%((;
-ields.add%num#(;
-ields.add%symbol(;
-ields.add%num)(;
-ields.add%e+uals(;
-ields.add%answer(;

JPanel buttons $ new JPanel%new FlowLayout%((;
buttons.add%addButton(;
buttons.add%subButton(;

getContentPane%(.setLayout%new BorderLayout%((;
getContentPane%(.add%&.ort/&0 -ields(;
getContentPane%(.add%&Center&0 buttons(;

addButton.addctionListener%t/is(;
subButton.addctionListener%t/is(;
pac1%(;
set2isible%true(;
3

public void actionPer-ormed%ction4vent e(
!
double x $ 5ouble.parse5ouble%num#.get"ext%((;
double y $ 5ouble.parse5ouble%num).get"ext%((;
i- %e.get6ource%( $$ addButton(
!
answer.set"ext%6tring.value7-% x * y ((;
symbol.set"ext%&*&(;
3
i- %e.get6ource%( $$ subButton(
!
answer.set"ext%6tring.value7-% x , y ((;
symbol.set"ext%&,&(;
3
3
3
To run !our applet, right-clic' on the bloc' representing the Calcpplet class and select
+:un pplet,. .a'e sure that +:un applet in pplet2iewer, is chec'ed, then clic'
7;.

You might also like