Abdullah
Posts: 1
Nickname: abdali
Registered: Dec, 2002
|
|
Re: Calculator
|
Posted: Dec 22, 2002 4:58 AM
|
|
> I've been working on this Calculator program for Java for > weeks now and it is a total mess!!! Does anyone have any > code on a simple calculator program with the numeric > buttons(0-9), (*, /, +, -) functions, and an AC button > with the label box on teh top?
// A calculator Program import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class Calculator extends Frame implements ActionListener { JTextField jt; JButton bt7; JButton bt8; JButton bt9;
JButton btPlus; JButton bt4; JButton bt5; JButton bt6;
JButton btMinus; JButton bt1; JButton bt2; JButton bt3;
JButton btMul; JButton btDot; JButton bt0; JButton btSign;
JButton btDiv; JButton btSin; JButton btCos; JButton btTan;
JButton btC; JButton btEqual; JButton btExit;
String str="0",str2="0"; int f,clear;
public Calculator (String title) { super(title); setSize(500,200); setBackground(Color.blue); Color functionColor =new Color (0,255,255); Color equalColor =new Color (255,193,224); Color exitColor =new Color (255,0,0); setLayout(new GridLayout(6,4,5,5)); Font f1 = new Font("TimesRoman",Font.BOLD,12); jt=new JTextField("0",5); jt.setFont(f1); add(jt); bt7=new JButton("7"); bt7.setFont(f1); bt7.addActionListener(this); bt8=new JButton("8"); bt8.setFont(f1); bt8.addActionListener(this); bt9=new JButton("9"); bt9.setFont(f1); bt9.addActionListener(this); btPlus=new JButton("+"); btPlus.setFont(f1); btPlus.setBackground (functionColor); btPlus.addActionListener(this); bt4=new JButton("4"); bt4.setFont(f1); bt4.addActionListener(this); bt5=new JButton("5"); bt5.setFont(f1); bt5.addActionListener(this); bt6=new JButton("6"); bt6.setFont(f1); bt6.addActionListener(this); btMinus=new JButton("-"); btMinus.setFont(f1); btMinus.setBackground (functionColor); btMinus.addActionListener(this); bt1=new JButton("1"); bt1.setFont(f1); bt1.addActionListener(this); bt2=new JButton("2"); bt2.setFont(f1); bt2.addActionListener(this); bt3=new JButton("3"); bt3.setFont(f1); bt3.addActionListener(this); btMul=new JButton("X"); btMul.setFont(f1); btMul.setBackground (functionColor); btMul.addActionListener(this); btDot=new JButton("."); btDot.setFont(f1); btDot.addActionListener(this); bt0=new JButton("0"); bt0.setFont(f1); bt0.addActionListener(this); btSign=new JButton("+/-"); btSign.setFont(f1); btSign.addActionListener(this); btDiv=new JButton("/"); btDiv.setFont(f1); btDiv.setBackground (functionColor); btDiv.addActionListener(this); btSin=new JButton("Sin"); btSin.setFont(f1); btSin.setBackground (functionColor); btSin.addActionListener(this); btCos=new JButton("Cos"); btCos.setFont(f1); btCos.setBackground (functionColor); btCos.addActionListener(this); btTan=new JButton("Tan"); btTan.setFont(f1); btTan.setBackground (functionColor); btTan.addActionListener(this); btC=new JButton("C"); btC.setFont(f1); btC.setBackground (functionColor); btC.addActionListener(this); btEqual=new JButton("="); btEqual.setFont(f1); btEqual.setBackground (equalColor); btEqual.addActionListener(this); btExit=new JButton("Exit"); btExit.setFont(f1); btExit.setBackground (exitColor); btExit.addActionListener(this); add(bt7); add(bt8); add(bt9); add(btPlus); add(bt4); add(bt5); add(bt6); add(btMinus); add(bt1); add(bt2); add(bt3); add(btMul); add(btDot); add(bt0); add(btSign); add(btDiv); add(btSin); add(btCos); add(btTan); add(btC); add(btEqual); add(btExit); setVisible(true); }
public static void main(String argv[]) { Calculator app=new Calculator("Calculator");
}
public void actionPerformed(ActionEvent evt) { if(evt.getSource()==bt7) { if (clear==1) { str=""; clear=0; } str = str+(bt7.getText()); jt.setText(str); } if(evt.getSource()==bt8) { if (clear==1) { str=""; clear=0; } str = str+(bt8.getText()); jt.setText(str); } if(evt.getSource()==bt9) { if (clear==1) { str=""; clear=0; } str = str+(bt9.getText()); jt.setText(str); } if(evt.getSource()==btPlus) { str2=str; str=""; f=1; } if(evt.getSource()==bt4) { if (clear==1) { str=""; clear=0; } str = str+(bt4.getText()); jt.setText(str); } if(evt.getSource()==bt5) { if (clear==1) { str=""; clear=0; } str = str+(bt5.getText()); jt.setText(str); } if(evt.getSource()==bt6) { if (clear==1) { str=""; clear=0; } str = str+(bt6.getText()); jt.setText(str); } if(evt.getSource()==btMinus) { str2=str; str=""; f=2; } if(evt.getSource()==bt1) { if (clear==1) { str=""; clear=0; } str = str+(bt1.getText()); jt.setText(str); } if(evt.getSource()==bt2) { if (clear==1) { str=""; clear=0; } str = str+(bt2.getText()); jt.setText(str); } if(evt.getSource()==bt3) { if (clear==1) { str=""; clear=0; } str = str+(bt3.getText()); jt.setText(str); } if(evt.getSource()==btMul) { str2=str; str=""; f=3; } if(evt.getSource()==btDot) { if (clear==1) { str=""; clear=0; }; str = str+(btDot.getText()); jt.setText(str); } if(evt.getSource()==bt0) { if (clear==1) { str=""; clear=0; } str = str+(bt0.getText()); jt.setText(str); } if(evt.getSource()==btSign) { str = "-"+str; jt.setText(str); } if(evt.getSource()==btDiv) { str2=str; str=""; f=4; }
if(evt.getSource()==btSin) { str2=str;
}
if(evt.getSource()==btCos) { str2=str; } if(evt.getSource()==btTan) { str2=str; }
if(evt.getSource()==btC) { str="0"; jt.setText(str); } if(evt.getSource()==btEqual) { switch(f) { case 1: float value1 = Float.parseFloat(str2); float value2 = Float.parseFloat(str); jt.setText("" + (value1 + value2)); str=(String)jt.getText(); break; case 2: float value3 = Float.parseFloat(str2); float value4 = Float.parseFloat(str); jt.setText("" + (value3 - value4)); str=(String)jt.getText(); break; case 3: float value5 = Float.parseFloat(str2); float value6 = Float.parseFloat(str); jt.setText("" + (value5 * value6)); str=(String)jt.getText(); break; case 4: float value7 = Float.parseFloat(str2); float value8 = Float.parseFloat(str); jt.setText("" + (float)(value7 / value8)); str=(String)jt.getText();
break;
}// switch f=0; clear=1; }//if btEqual
if(evt.getSource()==btSin) {
float value9 = Float.parseFloat(str); jt.setText("" + Math.sin(value9)); str=(String)jt.getText(); clear=1; } if(evt.getSource()==btCos) {
float value10 = Float.parseFloat(str); jt.setText("" + Math.cos(value10)); str=(String)jt.getText(); clear=1; } if(evt.getSource()==btTan) {
float value11 = Float.parseFloat(str); jt.setText("" + Math.tan(value11)); str=(String)jt.getText(); clear=1; }
if(evt.getSource()==btExit) { dispose(); System.exit(0); }
}//action Performed
}//class TextField1
|
|