Created
October 1, 2012 15:06
-
-
Save Sadmansamee/3812335 to your computer and use it in GitHub Desktop.
simple calculator for begginer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
//import javax.swing.event.*; | |
//import javax.swing.border.EmptyBorder; | |
public class basiccalculator extends JFrame { | |
private static final long serialVersionUID = 1L; | |
//version 1.0.0 | |
//built by sadman samee | |
private final Font BIGGER_FONT=new Font("monspaced",Font.PLAIN,20); | |
private JTextField textfield; | |
private boolean number=true; | |
private String equalop="="; | |
//calls calculatorop class for operator | |
private CalculatorOp op = new CalculatorOp(); | |
public basiccalculator()//constructor start | |
{ | |
textfield=new JTextField("0",12); | |
textfield.setHorizontalAlignment(JTextField.RIGHT); | |
textfield.setFont(BIGGER_FONT); | |
//calls numberlistner class | |
ActionListener numberlistner=new NumberListner(); | |
String buttonorder="1234567890 "; | |
JPanel buttonpanel=new JPanel(); | |
buttonpanel.setLayout(new GridLayout(3,3,3,3)); | |
for (int i = 0; i < buttonorder.length(); i++) { | |
String key=buttonorder.substring(i, i+1); | |
if(key.equals(" ")){ | |
buttonpanel.add(new JLabel("")); | |
}//if | |
else | |
{ | |
JButton button=new JButton(key); | |
button.addActionListener(numberlistner); | |
button.setFont(BIGGER_FONT); | |
buttonpanel.add(button); | |
}//else | |
}//for finished | |
//calls operatorlistner class | |
ActionListener operatorlistner=new OperatorListner(); | |
JPanel panel=new JPanel(); | |
panel.setLayout(new GridLayout(4,4,4,4)); | |
String[] oporder={"C","+","-","*","/","="}; | |
for (int i = 0; i < oporder.length; i++) { | |
JButton button=new JButton(oporder[i]); | |
button.addActionListener(operatorlistner); | |
button.setFont(BIGGER_FONT); | |
panel.add(button); | |
}//for inished | |
JPanel pan=new JPanel(); | |
pan.setLayout(new BorderLayout(4,4)); | |
pan.add(textfield,BorderLayout.NORTH); | |
pan.add(buttonpanel,BorderLayout.CENTER); | |
pan.add(panel,BorderLayout.EAST); | |
this.setContentPane(pan); | |
this.pack(); | |
this.setTitle("Calculator (built by sadman samee)"); | |
this.setResizable(false); | |
}//constructor finished | |
private void action() | |
{ | |
number=true; | |
textfield.setText("0"); | |
equalop= "="; | |
op.setTotal("0"); | |
} | |
class OperatorListner implements ActionListener{ | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
if(number) | |
{ | |
action(); | |
textfield.setText("0"); | |
} | |
else | |
{ | |
number=true; | |
String displaytext=textfield.getText(); | |
if(equalop.equals("=")) | |
{ | |
op.setTotal(displaytext); | |
}else if(equalop.equals("+")){ | |
op.add(displaytext); | |
}else if(equalop.equals("-")) | |
{ | |
op.subtarct(displaytext); | |
}else if(equalop.equals("*")) | |
{ | |
op.multiply(displaytext); | |
}else if(equalop.equals("/")) | |
{ | |
op.divide(displaytext); | |
} | |
textfield.setText("" + op.GetTotalString()); | |
equalop = e.getActionCommand(); | |
} | |
}//method finished | |
} | |
//operatorlistner class finished | |
class NumberListner implements ActionListener{ | |
public void actionPerformed(ActionEvent e) { | |
String digit=e.getActionCommand(); | |
if(number) | |
{ | |
textfield.setText(digit); | |
number=false; | |
}else{ | |
textfield.setText(textfield.getText()+digit); | |
} | |
}//method | |
}//numberlistner finished | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
//import javax.swing.event.*; | |
public class calcaller { | |
public static void main(String[] args) { | |
JFrame frame=new basiccalculator(); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setVisible(true); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CalculatorOp { | |
private int total; | |
public CalculatorOp() | |
{ | |
total=0; | |
} | |
public String GetTotalString() | |
{return ""+total; | |
} | |
public void setTotal(String n) | |
{total = convertToNumber(n); | |
} | |
public void add(String n) | |
{ | |
total +=convertToNumber(n); | |
} | |
public void subtarct(String n) | |
{ | |
total -=convertToNumber(n); | |
} | |
public void multiply(String n) | |
{ | |
total *=convertToNumber(n); | |
} | |
public void divide(String n) | |
{ | |
total /=convertToNumber(n); | |
} | |
private int convertToNumber(String n) { | |
return Integer.parseInt(n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
simple calculator for begginer