Calculator applet questions
I have a Calculator applet. The code works and I have some questions
about the code in public Calc(). Thank you in advance for help me.
1. In IE 6, it displays both the text box and keys. Since I did not
use the second argument in pCalc.add(), it should display the keys only
(it did in an applet viewer).
2. It does not have init() or start(), how does IE know where to
start?
3. The components did not set their size. Does IE sets the size for
the keys and the panels?
4. How can I display red color outside the keys and text box? It only
display a red bar beneath the text box.
Here is the html code
<HTML>
<HEAD>
<TITLE>A Simple Program</TITLE>
</HEAD>
<BODY>
<h2>Here is the applet:</h2><br>
<APPLET CODE="Calc.class" WIDTH=260 HEIGHT=220 alt="white">
Sorry, you aren't running a Java-capable browser.
</APPLET>
</BODY>
</HTML>
Below is the java code.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calc extends JApplet implements ActionListener
{
JTextField text;
String sText1, sText2;
double dReg1, dReg2, dMem;
String sOperator;
boolean isFixReg;
public Calc()
{
JPanel pFrame = new JPanel();
pFrame.setLayout(new FlowLayout());
text = new JTextField("");
text.setForeground(Color.red);
text.setEditable(false);
JPanel pCalc = new JPanel();
pCalc.setLayout(new BorderLayout(0, 10));
//pCalc.add("North", text);
//pFrame.add("Center", pCalc);
pCalc.add(text);
pFrame.add(pCalc);
Dimension dSize= pCalc.getSize();
dSize.width = dSize.width + 20;
dSize.height = dSize.height + 20;
pFrame.setSize(dSize);
JPanel pKey = new JPanel();
pKey.setLayout(new GridLayout(5, 4, 5, 5));
add(pKey);
JButton[] keys=new JButton[20];
for (int i=0; i<=9; i++)
keys[i] = new JButton(String.valueOf(i));
keys[10]=new JButton("C"); //C
keys[11]=new JButton("MR"); //MR
keys[12]=new JButton("M-"); //M-
keys[13]=new JButton("M+"); //M+
keys[14]=new JButton("/"); ///
keys[15]=new JButton("*"); //*
keys[16]=new JButton("-"); //-
keys[17]=new JButton("."); //.
keys[18]=new JButton("="); //=
keys[19]=new JButton("+"); //+
for (int i=10; i<=13; i++) //C,MR,M-,M+
pKey.add(keys[i]);
for (int i=7; i<=9; i++) // 7, 8, 9
pKey.add(keys[i]);
pKey.add(keys[14]); ///
for (int i=4; i<=6; i++) // 4, 5, 6
pKey.add(keys[i]);
pKey.add(keys[15]); //*
for (int i=1; i<=3; i++) // 1, 2, 3
pKey.add(keys[i]);
pKey.add(keys[16]); //-
pKey.add(keys[0]); //0
for (int i=17; i<=19; i++) // ., =, +
pKey.add(keys[i]);
//pCalc.add("South", pKey);
pCalc.add(pKey);
for (int i=0; i<keys.length; i++)
keys[i].addActionListener(this);
setLayout(new BorderLayout(10, 10));
//add("North", pFrame);
add(pFrame);
dReg1 = 0.0d;
dReg2 = 0.0d;
dMem = 0.0d;
sOperator = "";
text.setText("0");
isFixReg = true;
pCalc.setBackground(Color.red);
//setBackground(Color.blue);
//pKey.setBackground(Color.cyan);
//pFrame.setBackground(Color.cyan);
}
public void actionPerformed(ActionEvent e){
if (e.getActionCommand().equals("C"))
{
dReg1 = 0.0d;
dReg2 = 0.0d;
sOperator = "";
text.setText("0");
isFixReg = true;
}
else if ((e.getActionCommand().equals("0")) |
(e.getActionCommand().equals("1")) |
(e.getActionCommand().equals("2"))
| (e.getActionCommand().equals("3")) |
(e.getActionCommand().equals("4")) |
(e.getActionCommand().equals("5"))
| (e.getActionCommand().equals("6")) |
(e.getActionCommand().equals("7")) |
(e.getActionCommand().equals("8"))
| (e.getActionCommand().equals("9")) |
(e.getActionCommand().equals(".")))
{
if (isFixReg)
sText2 = (String) e.getActionCommand();
else
sText2 = text.getText() + e.getActionCommand();
text.setText(sText2);
isFixReg = false;
}
else if ((e.getActionCommand().equals("+")) |
(e.getActionCommand().equals("-"))
| (e.getActionCommand().equals("*")) |
(e.getActionCommand().equals("/")) |
(e.getActionCommand().equals("=")))
{
sText1 = text.getText();
dReg2 = (Double.valueOf(sText1)).doubleValue();
dReg1 = Calculation(sOperator, dReg1, dReg2);
Double dTemp = new Double(dReg1);
sText2 = dTemp.toString();
text.setText(sText2);
sOperator = (String) e.getActionCommand();
isFixReg = true;
}
else if (e.getActionCommand().equals("MR"))
{
Double dTemp = new Double(dMem);
sText2 = dTemp.toString();
text.setText(sText2);
sOperator = "";
isFixReg = true;
}
else if (e.getActionCommand().equals("M+"))
{
sText1 = text.getText();
dReg2 = (Double.valueOf(sText1)).doubleValue();
dReg1 = Calculation(sOperator, dReg1, dReg2);
Double dTemp = new Double(dReg1);
sText2 = dTemp.toString();
text.setText(sText2);
dMem = dMem + dReg1;
sOperator = "";
isFixReg = true;
}
else if (e.getActionCommand().equals("M-"))
{
sText1 = text.getText();
dReg2 = (Double.valueOf(sText1)).doubleValue();
dReg1 = Calculation(sOperator, dReg1, dReg2);
Double dTemp = new Double(dReg1);
sText2 = dTemp.toString();
text.setText(sText2);
dMem = dMem - dReg1;
sOperator = "";
isFixReg = true;
}
}
private double Calculation(String sOperator, double dReg1, double
dReg2)
{
if ("+".equals(sOperator)) dReg1 = dReg1 + dReg2;
else if ("-".equals(sOperator)) dReg1 = dReg1 - dReg2;
else if ("*".equals(sOperator)) dReg1 = dReg1 * dReg2;
else if ("/".equals(sOperator)) dReg1 = dReg1 / dReg2;
else dReg1 = dReg2;
return dReg1;
}
}