Home Tutorialhelp Comment PLZ TRY THIS CODE

 
 

Comment

KHURAM SHAHZAD
PLZ TRY THIS CODE,
May 25, 2009 at 8:35 PM

public class CalculatorClass {
public static void main(String[] args) {
MainMethodClass FT = new MainMethodClass();
FT.necessary();
}
}
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.*;

class MainMethodClass extends JFrame implements ActionListener{

public MainMethodClass(){
}
JButton zeroButton, oneButton, twoButton, threeButton, fourButton, fiveButton, sixButton, sevenButton, eightButton, nineButton ;
JButton addButton, multiplyButton, subButton, divButton, equalsButton , clsButton;
JTextField digitField ;
int operator;
boolean a, b , c, d;
void necessary(){

Container Con = getContentPane();
setLayout(new FlowLayout());
setTitle("Simple Calculator");

// Initializing Buttons & TextBoxes.

digitField = new JTextField(10);
digitField.setSize(50, 20);
Con.add(digitField);

sevenButton = new JButton("7");
sevenButton.addActionListener(this);
Con.add(sevenButton);

eightButton = new JButton("8");
eightButton.addActionListener(this);
Con.add(eightButton);

nineButton = new JButton("9");
nineButton.addActionListener(this);
Con.add(nineButton);

fourButton = new JButton("4");
fourButton.addActionListener(this);
Con.add(fourButton);

fiveButton = new JButton("5");
fiveButton.addActionListener(this);
Con.add(fiveButton);

sixButton = new JButton("6");
sixButton.addActionListener(this);
Con.add(sixButton);

oneButton = new JButton("1");
oneButton.addActionListener(this);
Con.add(oneButton);

twoButton = new JButton("2");
twoButton.addActionListener(this);
Con.add(twoButton);

threeButton = new JButton("3");
threeButton.addActionListener(this);
Con.add(threeButton);

addButton = new JButton("+");
addButton.addActionListener(this);
Con.add(addButton);

multiplyButton = new JButton("x");
multiplyButton.addActionListener(this);
Con.add(multiplyButton);

subButton = new JButton("-");
subButton.addActionListener(this);
Con.add(subButton);

zeroButton = new JButton("0");
zeroButton.addActionListener(this);
Con.add(zeroButton);

equalsButton = new JButton("=");
equalsButton.addActionListener(this);
Con.add(equalsButton);

divButton = new JButton("/");
divButton.addActionListener(this);
Con.add(divButton);

clsButton = new JButton("C");
clsButton.addActionListener(this);
// clsButton.setSize(100, 100);
Con.add(clsButton);

setVisible(true);
setSize(172,247);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
System.out.println(""+e.getActionCommand());

if(e.getActionCommand()== "1" || e.getActionCommand()=="2" || e.getActionCommand()== "3" || e.getActionCommand()=="4" || e.getActionCommand()=="5" || e.getActionCommand()=="6" || e.getActionCommand()=="7" || e.getActionCommand()=="8" || e.getActionCommand()=="9" || e.getActionCommand()=="0"){
digitField.setText(digitField.getText()+e.getActionCommand());
}
else if(e.getActionCommand()=="+"){
operator = Integer.parseInt(digitField.getText());
a = true;
digitField.setText("");

}else if(e.getActionCommand()=="-"){
operator = Integer.parseInt(digitField.getText());
b = true;
digitField.setText("");
}else if(e.getActionCommand()=="x"){
operator = Integer.parseInt(digitField.getText());
c = true;
digitField.setText("");
}
else if(e.getActionCommand()=="/"){
operator = Integer.parseInt(digitField.getText());
d = true;
digitField.setText("");
}else if(e.getActionCommand()=="="){
if(a){
int a =operator + Integer.parseInt(digitField.getText());
digitField.setText(""+a);
}
else if(b){
int b =operator - Integer.parseInt(digitField.getText());
digitField.setText(""+b);
}
else if(c){
int c = operator * Integer.parseInt(digitField.getText());
digitField.setText(""+c);
}
else if(d){
int d = operator / Integer.parseInt(digitField.getText());
digitField.setText(""+d);
}

}
else if(e.getActionCommand()=="C"){
digitField.setText("");
}
}
}

View All Comments | View Tutorial
Advertisement
Related Tutorial and Articles
Advertisements Advertisements
 

 

 

DMCA.com