I'm trying to write a calculator java applet, so far i've declared the buttons and terxtfield and i've intialized it also, what I don't know how to do is add the numbers, subtract, multiply, divide, square root it, add to memory and so forth when the user presses the buttons...
Can any1 help in doing this?? I'm using the GridLayout technique...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Cal extends JFrame implements ActionListener
{
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
Button one, two, three, four, five, six, seven, eight, nine, zero;
Button C,CE,MC,MR,MS,Mplus,plumin,dot,sqrt,perc,recip;
Button add,mult,div,sub,equals;
public static void main(String[] args)
{
Cal c = new Cal();
c.setVisible(true);
}
public Cal()
{
setSize(WIDTH, HEIGHT);
addWindowListener(new WindowDestroyer());
setTitle("Parul's Cal."
Container content = getContentPane();
contentPane.setLayout(new FlowLayout());
text = new TextField(""
text.setForeground(Color.black);
text.setEditable(false);
Container contentPane = getContentPane();
contentPane.setlayout(new BorderLayout(0, 10));
contentPane.add(text, "North"
content.add(contentPane, "Center"
Container contentPane = getContentPane();
contentPane.setBackground(Color.cyan);
contentPane.setLayout(new GridLayout(5, 6));
JButton zeroButton = new JButton("0"
zeroButton.addActionListener(this);
contentPane.add(zeroButton);
JButton oneButton = new JButton("1"
oneButton.addActionListener(this);
contentPane.add(oneButton);
JButton twoButton = new JButton("2"
twoButton.addActionListener(this);
contentPane.add(twoButton);
JButton threeButton = new JButton("3"
threeButton.addActionListener(this);
contentPane.add(threeButton);
JButton fourButton = new JButton("4"
fourButton.addListener(this);
contentPane.add(fourButton);
JButton fiveButton = new JButton("5"
fiveButton.addListener(this);
contentPane.add(fiveButton);
JButton sixButton = new JButton("6"
sixButton.addListener(this);
contentPane.add(sixButton);
JButton sevenButton = new JButton("7"
sevenButton.addListener(this);
contentPane.add(sevenButton);
JButton eightButton = new JButton("8"
eightButton.addListener(this);
contentPane.add(eightButton);
JButton nineButton = new JButton("9"
nineButton.addListener(this);
contentPane.add(nineButton);
JButton MCButton = new JButton("MC"
MCButton.addListener(this);
contentPane.add(MCButton);
JButton MRButton = new JButton("MR"
MRButton.addListener(this);
contentPane.add(MRButton);
JButton MSButton = new JButton("MS"
MSButton.addListener(this);
contentPane.add(MSButton);
JButton mPlusButton = new JButton("M+"
mPlusButton.addListener(this);
contentPane.add(mPlusButton);
JButton CEButton = new JButton("CE"
CEButton.addListener(this);
contentPane.add(CEButton);
JButton CButton = new JButton("C"
CButton.addListener(this);
contentPane.add(CButton);
JButton divButton = new JButton("/"
divButton.addListener(this);
contentPane.add(divButton);
JButton multButton = new JButton("*"
multButton.addListener(this);
contentPane.add(multButton);
JButton addButton = new JButton("+"
addButton.addListener(this);
contentPane.add(addButton);
JButton subButton = new JButton("-"
subButton.addListener(this);
contentPane.add(subButton);
JButton dotButton = new JButton("."
dotButton.addListener(this);
contentPane.add(dotButton);
JButton equalsButton = new JButton("="
equalsButton.addListener(this);
contentPane.add(equalsButton);
JButton recipButton = new JButton("1/x"
recipButton.addListener(this);
contentPane.add(recipButton);
JButton percButton = new JButton("%"
percButton.addListener(this);
contentPane.add(percButton);
JButton sqrtButton = new JButton("sqrt"
sqrtButton.addListener(this);
contentPane.add(sqrtButton);
JButton plusminButton = new JButton("+/-"
plusminButton.addListener(this);
contentPane.add(plusminButton);
}
public void actionPerformed(ActionEvent e)
{
Container contentPane = getContentPane();
if(e.getActionCommand().equals("0")
Can any1 help in doing this?? I'm using the GridLayout technique...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Cal extends JFrame implements ActionListener
{
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
Button one, two, three, four, five, six, seven, eight, nine, zero;
Button C,CE,MC,MR,MS,Mplus,plumin,dot,sqrt,perc,recip;
Button add,mult,div,sub,equals;
public static void main(String[] args)
{
Cal c = new Cal();
c.setVisible(true);
}
public Cal()
{
setSize(WIDTH, HEIGHT);
addWindowListener(new WindowDestroyer());
setTitle("Parul's Cal."
Container content = getContentPane();
contentPane.setLayout(new FlowLayout());
text = new TextField(""
text.setForeground(Color.black);
text.setEditable(false);
Container contentPane = getContentPane();
contentPane.setlayout(new BorderLayout(0, 10));
contentPane.add(text, "North"
content.add(contentPane, "Center"
Container contentPane = getContentPane();
contentPane.setBackground(Color.cyan);
contentPane.setLayout(new GridLayout(5, 6));
JButton zeroButton = new JButton("0"
zeroButton.addActionListener(this);
contentPane.add(zeroButton);
JButton oneButton = new JButton("1"
oneButton.addActionListener(this);
contentPane.add(oneButton);
JButton twoButton = new JButton("2"
twoButton.addActionListener(this);
contentPane.add(twoButton);
JButton threeButton = new JButton("3"
threeButton.addActionListener(this);
contentPane.add(threeButton);
JButton fourButton = new JButton("4"
fourButton.addListener(this);
contentPane.add(fourButton);
JButton fiveButton = new JButton("5"
fiveButton.addListener(this);
contentPane.add(fiveButton);
JButton sixButton = new JButton("6"
sixButton.addListener(this);
contentPane.add(sixButton);
JButton sevenButton = new JButton("7"
sevenButton.addListener(this);
contentPane.add(sevenButton);
JButton eightButton = new JButton("8"
eightButton.addListener(this);
contentPane.add(eightButton);
JButton nineButton = new JButton("9"
nineButton.addListener(this);
contentPane.add(nineButton);
JButton MCButton = new JButton("MC"
MCButton.addListener(this);
contentPane.add(MCButton);
JButton MRButton = new JButton("MR"
MRButton.addListener(this);
contentPane.add(MRButton);
JButton MSButton = new JButton("MS"
MSButton.addListener(this);
contentPane.add(MSButton);
JButton mPlusButton = new JButton("M+"
mPlusButton.addListener(this);
contentPane.add(mPlusButton);
JButton CEButton = new JButton("CE"
CEButton.addListener(this);
contentPane.add(CEButton);
JButton CButton = new JButton("C"
CButton.addListener(this);
contentPane.add(CButton);
JButton divButton = new JButton("/"
divButton.addListener(this);
contentPane.add(divButton);
JButton multButton = new JButton("*"
multButton.addListener(this);
contentPane.add(multButton);
JButton addButton = new JButton("+"
addButton.addListener(this);
contentPane.add(addButton);
JButton subButton = new JButton("-"
subButton.addListener(this);
contentPane.add(subButton);
JButton dotButton = new JButton("."
dotButton.addListener(this);
contentPane.add(dotButton);
JButton equalsButton = new JButton("="
equalsButton.addListener(this);
contentPane.add(equalsButton);
JButton recipButton = new JButton("1/x"
recipButton.addListener(this);
contentPane.add(recipButton);
JButton percButton = new JButton("%"
percButton.addListener(this);
contentPane.add(percButton);
JButton sqrtButton = new JButton("sqrt"
sqrtButton.addListener(this);
contentPane.add(sqrtButton);
JButton plusminButton = new JButton("+/-"
plusminButton.addListener(this);
contentPane.add(plusminButton);
}
public void actionPerformed(ActionEvent e)
{
Container contentPane = getContentPane();
if(e.getActionCommand().equals("0")