/**
* Function: Java Calculator
* Description: Use GridBagLayout to typeset the calculator button
*/
package com;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame{
private static final long serialVersionUID = -4046651753583665283L;
static JTextField LED;
private MyNumPanel mynum ;
static String str = new String();
public Calculator(){
LED = new JTextField("0");
LED.setEditable(false);
LED.setHorizontalAlignment(JTextField.RIGHT);
LED.setBackground(Color.WHITE);
LED.setFont(new Font("SansSerif", Font.BOLD, 20));
mynum = new MyNumPanel();
mynum.setFont(new Font("SansSerif", Font.BOLD, 14));
this.setLayout(new BorderLayout());
this.add(BorderLayout.NORTH,LED);
this.add(BorderLayout.WEST,mynum);
}
public static void main(String args[]){
Calculator myframe = new Calculator();
myframe.setSize(256,245);
myframe.setLocationRelativeTo(null);
myframe.setVisible(true);
myframe.setResizable(false);
myframe.setTitle("Calculator");
myframe.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class MyNumPanel extends JPanel implements ActionListener{
private static final long serialVersionUID = 7986569405988742690L;
private JButton b0 = new JButton(" 0 "),
b1 = new JButton(" 1 "),
b2 = new JButton(" 2 "),
b3 = new JButton(" 3 "),
b4 = new JButton(" 4 "),
b5 = new JButton(" 5 "),
b6 = new JButton(" 6 "),
b7 = new JButton(" 7 "),
b8 = new JButton(" 8 "),
b9 = new JButton(" 9 "),
bpoint = new JButton(" . ");
private JButton plus = new JButton(" + "),
minus = new JButton(" - "),
multiply = new JButton(" × "),
divide = new JButton(" ÷ "),
equals = new JButton(" = "),
symbol = new JButton(" ± "),
clear = new JButton("Clear"),
square = new JButton("√"),
recip = new JButton("1/x"),
exit = new JButton("Exit");
private JPanel p1;
private GridBagConstraints c;
boolean point = false;
boolean calc = false;
Double computer = 0.0;
Double temp = 0.0;
Double oldnum = 0.0;
Double newnum = 0.0;
static String comstatus = "FALSE";
public MyNumPanel(){
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
bpoint.addActionListener(this);
plus.addActionListener(this);
minus.addActionListener(this);
multiply.addActionListener(this);
divide.addActionListener(this);
equals.addActionListener(this);
symbol.addActionListener(this);
clear.addActionListener(this);
square.addActionListener(this);
recip.addActionListener(this);
exit.addActionListener(this);
p1 = new JPanel();
// GridBagLayout
p1.setLayout(new GridBagLayout());
p1.setOpaque(true);
c = new GridBagConstraints();
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(2,2,2,2);
/**
* SetGridLayout
*/
SetGridLayout(p1,symbol,0,0,1,1);
SetGridLayout(p1,divide,1,0,1,1);
SetGridLayout(p1,multiply,2,0,1,1);
SetGridLayout(p1,minus,3,0,1,1);
SetGridLayout(p1,b7,0,1,1,1);
SetGridLayout(p1,b8,1,1,1,1);
SetGridLayout(p1,b9,2,1,1,1);
SetGridLayout(p1,b4,0,2,1,1);
SetGridLayout(p1,b5,1,2,1,1);
SetGridLayout(p1,b6,2,2,1,1);
SetGridLayout(p1,plus,3,1,1,2);
SetGridLayout(p1,b1,0,3,1,1);
SetGridLayout(p1,b2,1,3,1,1);
SetGridLayout(p1,b3,2,3,1,1);
SetGridLayout(p1,b0,0,4,2,1);
SetGridLayout(p1,bpoint,2,4,1,1);
SetGridLayout(p1,equals,3,3,1,2);
SetGridLayout(p1,clear,0,5,1,1);
SetGridLayout(p1,square,1,5,1,1);
SetGridLayout(p1,recip,2,5,1,1);
SetGridLayout(p1,exit,3,5,1,1);
setLayout(new FlowLayout());
add(p1);
}
public void SetGridLayout(JPanel p,Component comp, int gridx, int gridy, int gridwidth, int gridheight){
c.gridx = gridx; c.gridy = gridy;
c.gridwidth = gridwidth; c.gridheight = gridheight;
p.add(comp,c);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object source = e.getSource();
int singlenum = 0;
if (source==b0){
singlenum = 0;
SetNum(singlenum);
}
else if (source==b1){
singlenum = 1;
SetNum(singlenum);
}
else if (source==b2){
singlenum = 2;
SetNum(singlenum);
}
else if (source==b3){
singlenum = 3;
SetNum(singlenum);
}
else if (source==b4){
singlenum = 4;
SetNum(singlenum);
}
else if (source==b5){
singlenum = 5;
SetNum(singlenum);
}
else if (source==b6){
singlenum = 6;
SetNum(singlenum);
}
else if (source==b7){
singlenum = 7;
SetNum(singlenum);
}
else if (source==b8){
singlenum = 8;
SetNum(singlenum);
}
else if (source==b9){
singlenum = 9;
SetNum(singlenum);
}
else if (source==bpoint){
if (point == false){
Calculator.str = Calculator.LED.getText();
Calculator.str = Calculator.str + ".";
Calculator.LED.setText(Calculator.str);
point = true;
}
}
else if (source==plus){
oldnum = Double.valueOf(Calculator.LED.getText());
comstatus = "PLUS";
calc = true;
}
else if (source==minus){
oldnum = Double.valueOf(Calculator.LED.getText());
comstatus = "MINUS";
calc = true;
}
else if (source==multiply){
oldnum = Double.valueOf(Calculator.LED.getText());
comstatus = "MULTIPLY";
calc = true;
}
else if (source==divide){
oldnum = Double.valueOf(Calculator.LED.getText());
comstatus = "DIVIDE";
calc = true;
}
else if (source==equals){
newnum = Double.valueOf(Calculator.LED.getText());
Calc(); // Operate the calculation process
}
else if (source==symbol){
temp = Double.valueOf(Calculator.LED.getText());
if (temp !=0){
Calculator.LED.setText(String.valueOf(-1 * temp));
}
}
else if (source==square){
newnum = Double.valueOf(Calculator.LED.getText());
Calculator.LED.setText(String.valueOf(Math.sqrt(newnum)));
calc = true;
point = false;
}
else if (source==recip){
newnum = Double.valueOf(Calculator.LED.getText());
Calculator.LED.setText(String.valueOf(1 / newnum));
calc = true;
point = false;
}
else if (source==clear){
Calculator.LED.setText("0");
point = false;
}
else if (source==exit){
System.exit(0);
}
}
public void SetNum(int num){
if (calc){
Calculator.str = String.valueOf(num);
calc = false;
}
else{
Calculator.str = Calculator.LED.getText();
computer = Double.valueOf(Calculator.str);
if (computer != 0 ){
Calculator.str = Calculator.str + num;
}
else if (computer == 0 ){
if (point == true){
Calculator.str = Calculator.str + num;
}
else{
Calculator.str = String.valueOf(num);
}
}
}
Calculator.LED.setText(Calculator.str);
}
public void Calc(){
if (comstatus == "PLUS"){
newnum = oldnum + newnum;
}
else if (comstatus == "MINUS"){
newnum = oldnum - newnum;
}
else if (comstatus == "MULTIPLY"){
newnum = oldnum * newnum;
}
else if (comstatus == "DIVIDE"){
if (newnum==0){
System.err.print("System Error: Divide number can not be zero!");
Calculator.LED.setText("ERROR");
calc = true;
point = false;
return;
}
else{
newnum = oldnum / newnum;
}
}
Calculator.LED.setText(String.valueOf(newnum));
calc = true;
point = false;
}
}