Re: Applet and Application Problem - Please Help!!!
From: Anil Sharma (anils_at_in.niksun.com)
Date: 12/10/03
- Previous message: Cobus: "Re: Making some money out of web services with referral schemes?"
- In reply to: Maria Laura Re: "Applet and Application Problem - Please Help!!!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 9 Dec 2003 22:49:21 -0800
Here is the modified code which works as "Application" and "Applet".
---calcg.java-----------------------------------------------------------
// calculator
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class calcg extends JApplet implements ActionListener{
//public class calcg extends JFrame implements ActionListener {
private JButton one, two, three, four, five, six, seven,
eight, nine, zero, dec, eq, plus, minus, mult, div, clear;
private JLabel output, spacer;
private Container container;
private String operation;
private double num1, num2, answer;
private boolean clear_flag = false;
//set up GUI
public void init() {
//Create Title
JPanel container = new JPanel();
output = new JLabel("");
output.setBorder(new MatteBorder(2,2,2,2,Color.gray));
output.setPreferredSize(new Dimension(1,26));
getContentPane().setBackground(Color.white);
getContentPane().add( "North",output );
getContentPane().add( "Center",container );
//set up spacer
spacer = new JLabel( " " );
container.add( spacer );
//set up clear
clear = new JButton( "CE" );
clear.addActionListener(this);
container.add( clear );
//set up seven and register its event handler
seven = new JButton( "7" );
seven.addActionListener(this);
container.add( seven );
//set up eight
eight = new JButton( "8" );
eight.addActionListener(this);
container.add( eight );
//set up nine
nine = new JButton( "9" );
nine.addActionListener(this);
container.add( nine );
//set up div
div = new JButton( "/" );
div.addActionListener(this);
container.add( div );
//set up four
four = new JButton( "4" );
four.addActionListener(this);
container.add( four );
//set up five
five = new JButton( "5" );
five.addActionListener(this);
container.add( five );
//set up six
six = new JButton( "6" );
six.addActionListener(this);
container.add( six );
//set up mult
mult = new JButton( "*" );
mult.addActionListener(this);
container.add( mult );
//set up one
one = new JButton( "1" );
one.addActionListener(this);
container.add( one );
//set up two
two = new JButton( "2" );
two.addActionListener(this);
container.add( two );
//set up three
three = new JButton( "3" );
three.addActionListener(this);
container.add( three );
//set up minus
minus = new JButton( "-" );
minus.addActionListener(this);
container.add( minus );
//set up zero
zero = new JButton( "0" );
zero.addActionListener(this);
container.add( zero );
//set up dec
dec = new JButton( "." );
dec.addActionListener(this);
container.add( dec );
//set up eq
eq = new JButton( "=" );
eq.addActionListener(this);
container.add( eq );
//set up plus
plus = new JButton( "+" );
plus.addActionListener(this);
container.add( plus );
//Set size and make visible
setSize( 190, 225 );
setVisible( true );
///AKS setResizable( false );
}
public void actionPerformed(ActionEvent ae) {
JButton but = ( JButton )ae.getSource();
//Handle what button was pushed
//Action for dec button
if( but.getText() == "." ) {
//if dec is pressed, first check to make shure there is
not already a decimal
String temp = output.getText();
if( temp.indexOf( '.' ) == -1 )
output.setText( output.getText() + but.getText() );
}
//Action for clear button
else if( but.getText() == "CE" ) {
output.setText( "" );
operation = "";
num1 = 0.0;
num2 = 0.0;
}
//Action for plus button
else if( but.getText() == "+" ) {
operation = "+";
num1 = Double.parseDouble( output.getText() );
clear_flag = true;
//output.setText( "" );
}
//Action for minus button
else if( but.getText() == "-" ) {
operation = "-";
num1 = Double.parseDouble( output.getText() );
clear_flag = true;
//output.setText( "" );
}
//Action for mult button
else if( but.getText() == "*" ) {
operation = "*";
num1 = Double.parseDouble( output.getText() );
clear_flag = true;
//output.setText( "" );
}
//Action for div button
else if( but.getText() == "/" ) {
operation = "/";
num1 = Double.parseDouble( output.getText() );
clear_flag = true;
//output.setText( "" );
}
//Action for eq button
else if( but.getText() == "=" ) {
num2 = Double.parseDouble( output.getText() );
//handle addition
if( operation == "+" )
answer = num1 + num2;
else if( operation == "-" )
answer = num1 - num2;
else if( operation == "*" )
answer = num1 * num2;
else if( operation == "/" )
answer = num1 / num2;
//display answer in output field
output.setText( String.valueOf( answer ) );
clear_flag = true;
operation = "";
}
//Default action should only be used for numbers
else {
if( clear_flag == true ) {
output.setText( "" );
clear_flag = false;
}
output.setText( output.getText() + but.getText() );
}
}
//execute application
public static void main( String args[] ) {
JFrame calFrame = new JFrame();
calcg application = new calcg();
application.init();
calFrame.getContentPane().add(application);
calFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calFrame.setVisible(true);
calFrame.setResizable(false);
}
}
------------------class file ends here-----------------------
laurare@rocketmail.com (Maria Laura Re) wrote in message news:<99b3d55c.0312070756.2c106c0@posting.google.com>...
> Hi everybody,
>
> I'm trying to make this calculator program work as an applet AND as an
> application. I tried using:
>
> public class calcg extends JApplet implements ActionListener{
>
> in many different ways, but it didn't work. Can sombody help me make
> it work?
>
> Below is the source code for my calculator program.
>
> Thanks in advance,
>
> -Maria
>
> ps-You can also e-mail me at laurare@rocketmail.com
>
>
> // calculator
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
> import javax.swing.border.*;
>
> public class calcg extends JFrame implements ActionListener
> {
> private JButton one, two, three, four, five, six, seven,
> eight, nine, zero, dec, eq, plus, minus, mult, div, clear;
> private JLabel output, spacer;
> private Container container;
> private String operation;
> private double num1, num2, answer;
> private boolean clear_flag = false;
>
> //set up GUI
> public calcg()
> {
> //Create Title
> super("Calculator");
> JPanel container = new JPanel();
> container.setLayout( new FlowLayout( FlowLayout.CENTER
> ) );
>
> output = new JLabel("");
> output.setBorder(new MatteBorder(2,2,2,2,Color.gray));
> output.setPreferredSize(new Dimension(1,26));
> getContentPane().setBackground(Color.white);
> getContentPane().add( "North",output );
> getContentPane().add( "Center",container );
>
> //set up spacer
> spacer = new JLabel( "
> " );
> container.add( spacer );
>
> //set up clear
> clear = new JButton( "CE" );
> clear.addActionListener(this);
> container.add( clear );
>
> //set up seven and register its event handler
> seven = new JButton( "7" );
> seven.addActionListener(this);
> container.add( seven );
>
>
> //set up eight
> eight = new JButton( "8" );
> eight.addActionListener(this);
> container.add( eight );
>
> //set up nine
> nine = new JButton( "9" );
> nine.addActionListener(this);
> container.add( nine );
>
> //set up div
> div = new JButton( "/" );
> div.addActionListener(this);
> container.add( div );
>
> //set up four
> four = new JButton( "4" );
> four.addActionListener(this);
> container.add( four );
>
> //set up five
> five = new JButton( "5" );
> five.addActionListener(this);
> container.add( five );
>
> //set up six
> six = new JButton( "6" );
> six.addActionListener(this);
> container.add( six );
>
> //set up mult
> mult = new JButton( "*" );
> mult.addActionListener(this);
> container.add( mult );
>
> //set up one
> one = new JButton( "1" );
> one.addActionListener(this);
> container.add( one );
>
> //set up two
> two = new JButton( "2" );
> two.addActionListener(this);
> container.add( two );
>
> //set up three
> three = new JButton( "3" );
> three.addActionListener(this);
> container.add( three );
>
> //set up minus
> minus = new JButton( "-" );
> minus.addActionListener(this);
> container.add( minus );
>
> //set up zero
> zero = new JButton( "0" );
> zero.addActionListener(this);
> container.add( zero );
>
> //set up dec
> dec = new JButton( "." );
> dec.addActionListener(this);
> container.add( dec );
>
> //set up eq
> eq = new JButton( "=" );
> eq.addActionListener(this);
> container.add( eq );
>
> //set up plus
> plus = new JButton( "+" );
> plus.addActionListener(this);
> container.add( plus );
>
> //Set size and make visible
> setSize( 190, 225 );
> setVisible( true );
> setResizable( false );
> }
>
> public void actionPerformed(ActionEvent ae)
> {
> JButton but = ( JButton )ae.getSource();
>
> //Handle what button was pushed
> //Action for dec button
> if( but.getText() == "." )
> {
> //if dec is pressed, first check to make shure there
> is not already a decimal
> String temp = output.getText();
> if( temp.indexOf( '.' ) == -1 )
> output.setText( output.getText() + but.getText() );
> }
>
> //Action for clear button
> else if( but.getText() == "CE" )
> {
> output.setText( "" );
> operation = "";
> num1 = 0.0;
> num2 = 0.0;
> }
>
> //Action for plus button
> else if( but.getText() == "+" )
> {
> operation = "+";
> num1 = Double.parseDouble( output.getText() );
> clear_flag = true;
> //output.setText( "" );
> }
>
> //Action for minus button
> else if( but.getText() == "-" )
> {
> operation = "-";
> num1 = Double.parseDouble( output.getText() );
> clear_flag = true;
> //output.setText( "" );
> }
>
> //Action for mult button
> else if( but.getText() == "*" )
> {
> operation = "*";
> num1 = Double.parseDouble( output.getText() );
> clear_flag = true;
> //output.setText( "" );
> }
>
> //Action for div button
> else if( but.getText() == "/" )
> {
> operation = "/";
> num1 = Double.parseDouble( output.getText() );
> clear_flag = true;
> //output.setText( "" );
> }
>
> //Action for eq button
> else if( but.getText() == "=" )
> {
> num2 = Double.parseDouble( output.getText() );
> //handle addition
> if( operation == "+" )
> answer = num1 + num2;
> else if( operation == "-" )
> answer = num1 - num2;
> else if( operation == "*" )
> answer = num1 * num2;
> else if( operation == "/" )
> answer = num1 / num2;
>
> //display answer in output field
> output.setText( String.valueOf( answer ) );
>
> clear_flag = true;
> operation = "";
> }
>
> //Default action should only be used for numbers
> else
> {
> if( clear_flag == true )
> {
> output.setText( "" );
> clear_flag = false;
> }
> output.setText( output.getText() + but.getText() );
> }
> }
>
> //execute application
> public static void main( String args[] )
> {
> calcg application = new calcg();
>
> application.setDefaultCloseOperation(
> JFrame.EXIT_ON_CLOSE );
> }
> }
- Previous message: Cobus: "Re: Making some money out of web services with referral schemes?"
- In reply to: Maria Laura Re: "Applet and Application Problem - Please Help!!!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]