import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator implements ActionListener
{
JButton bt_1, bt_2, bt_3, bt_4, bt_5, bt_6, bt_7, bt_8, bt_9, bt_0,
bt_Plus, bt_Minus, bt_Multiplication, bt_Division, bt_Dot, bt_Cal,
bt_BackSpace, bt_C;
JTextField tf_Display;
StringBuffer tf_getText;
String getText;
double cal_Buffer1, cal_Buffer2, cal_Buffer3;
final byte PLUS = 1, MINUS = 2, MULTIPLICATION = 3, DIVISION = 4;
byte cal_Check, numberIn;
public Calculator()
{
tf_getText = new StringBuffer();
frameSetting();
}
public void frameSetting()
{
tf_Display = new JTextField("0");
tf_Display.setHorizontalAlignment(JTextField.RIGHT);
tf_Display.setFont(new Font("font", Font.BOLD, 16));
tf_Display.setEditable(false);
bt_1 = new JButton("1");
bt_1.setFont(new Font("font", Font.BOLD, 16));
bt_2 = new JButton("2");
bt_2.setFont(new Font("font", Font.BOLD, 16));
bt_3 = new JButton("3");
bt_3.setFont(new Font("font", Font.BOLD, 16));
bt_4 = new JButton("4");
bt_4.setFont(new Font("font", Font.BOLD, 16));
bt_5 = new JButton("5");
bt_5.setFont(new Font("font", Font.BOLD, 16));
bt_6 = new JButton("6");
bt_6.setFont(new Font("font", Font.BOLD, 16));
bt_7 = new JButton("7");
bt_7.setFont(new Font("font", Font.BOLD, 16));
bt_8 = new JButton("8");
bt_8.setFont(new Font("font", Font.BOLD, 16));
bt_9 = new JButton("9");
bt_9.setFont(new Font("font", Font.BOLD, 16));
bt_0 = new JButton("0");
bt_0.setFont(new Font("font", Font.BOLD, 16));
bt_Plus = new JButton("+");
bt_Plus.setFont(new Font("font", Font.BOLD, 16));
bt_Minus = new JButton("-");
bt_Minus.setFont(new Font("font", Font.BOLD, 16));
bt_Multiplication = new JButton("×");
bt_Multiplication.setFont(new Font("font", Font.BOLD, 16));
bt_Division = new JButton("÷");
bt_Division.setFont(new Font("font", Font.BOLD, 16));
bt_Dot = new JButton(".");
bt_Dot.setFont(new Font("font", Font.BOLD, 16));
bt_Cal = new JButton("=");
bt_Cal.setFont(new Font("font", Font.BOLD, 16));
bt_BackSpace = new JButton("←");
bt_BackSpace.setFont(new Font("font", Font.BOLD, 14));
bt_C = new JButton(" C ");
bt_C.setFont(new Font("font", Font.BOLD, 14));
JPanel panel_North = new JPanel(new GridLayout(1,1,5,5));
JPanel panel_Center = new JPanel(new BorderLayout());
JPanel panel_C_North = new JPanel();
JPanel panel_C_Center = new JPanel(new GridLayout(4,4,5,5));
JLabel lb_Info = new JLabel("Copyright(c) 2007. Dscythe(Paik Seung Ik)", SwingConstants.CENTER);
try {
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) { }
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame(" Swing Calendar");
frame.add(panel_North, "North");
panel_North.add(tf_Display);
frame.add(panel_Center, "Center");
panel_Center.add(panel_C_North, "North");
panel_C_North.add(bt_BackSpace);
panel_C_North.add(bt_C);
panel_Center.add(panel_C_Center, "Center");
panel_C_Center.add(bt_7);
panel_C_Center.add(bt_8);
panel_C_Center.add(bt_9);
panel_C_Center.add(bt_Division);
panel_C_Center.add(bt_4);
panel_C_Center.add(bt_5);
panel_C_Center.add(bt_6);
panel_C_Center.add(bt_Multiplication);
panel_C_Center.add(bt_1);
panel_C_Center.add(bt_2);
panel_C_Center.add(bt_3);
panel_C_Center.add(bt_Minus);
panel_C_Center.add(bt_0);
panel_C_Center.add(bt_Dot);
panel_C_Center.add(bt_Cal);
panel_C_Center.add(bt_Plus);
panel_Center.add(lb_Info, "South");
eventListener();
frame.setSize(250,250);
frame.setLocation(300,300);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
}
public void eventListener()
{
bt_1.addActionListener(this);
bt_2.addActionListener(this);
bt_3.addActionListener(this);
bt_4.addActionListener(this);
bt_5.addActionListener(this);
bt_6.addActionListener(this);
bt_7.addActionListener(this);
bt_8.addActionListener(this);
bt_9.addActionListener(this);
bt_0.addActionListener(this);
bt_Plus.addActionListener(this);
bt_Minus.addActionListener(this);
bt_Multiplication.addActionListener(this);
bt_Division.addActionListener(this);
bt_Dot.addActionListener(this);
bt_Cal.addActionListener(this);
bt_BackSpace.addActionListener(this);
bt_C.addActionListener(this);
tf_Display.addKeyListener(new keyAdapter()
{
public void keyTyped (KeyEvent ke)
{
char key_Cr = ke.getKeyChar();
//System.out.println(key_Cr);
if ('1' == key_Cr){
numberIn = 1;
numberInput();
} else if ('2' == key_Cr){
numberIn = 2;
numberInput();
} else if ('3' == key_Cr){
numberIn = 3;
numberInput();
} else if ('4' == key_Cr){
numberIn = 4;
numberInput();
} else if ('5' == key_Cr){
numberIn = 5;
numberInput();
} else if ('6' == key_Cr){
numberIn = 6;
numberInput();
} else if ('7' == key_Cr){
numberIn = 7;
numberInput();
} else if ('8' == key_Cr){
numberIn = 8;
numberInput();
} else if ('9' == key_Cr){
numberIn = 9;
numberInput();
} else if ('0' == key_Cr){
zero();
} else if ('+' == key_Cr){
calculate(PLUS);
} else if ('-' == key_Cr){
calculate(MINUS);
} else if ('*' == key_Cr){
calculate(MULTIPLICATION);
} else if ('/' == key_Cr){
calculate(DIVISION);
} else if ('.' == key_Cr){
dotCheck();
} else if ('' == key_Cr){
backSpace();
}
}
});
}
public void actionPerformed(ActionEvent e)
{
Object obj = e.getSource();
if (bt_1 == obj){
numberIn = 1;
numberInput();
} else if (bt_2 == obj){
numberIn = 2;
numberInput();
} else if (bt_3 == obj){
numberIn = 3;
numberInput();
} else if (bt_4 == obj){
numberIn = 4;
numberInput();
} else if (bt_5 == obj){
numberIn = 5;
numberInput();
} else if (bt_6 == obj){
numberIn = 6;
numberInput();
} else if (bt_7 == obj){
numberIn = 7;
numberInput();
} else if (bt_8 == obj){
numberIn = 8;
numberInput();
} else if (bt_9 == obj){
numberIn = 9;
numberInput();
} else if (bt_0 == obj){
zero();
} else if (bt_Plus == obj){
calculate(PLUS);
} else if (bt_Minus == obj){
calculate(MINUS);
} else if (bt_Multiplication == obj){
calculate(MULTIPLICATION);
} else if (bt_Division == obj){
calculate(DIVISION);
} else if (bt_Dot == obj){
dotCheck();
} else if (bt_C == obj){
tf_Display.setText("0");
tf_getText.delete(0,tf_getText.length());
cal_Buffer1 = 0;
cal_Buffer2 = 0;
cal_Check = 0;
numberIn = 0;
} else if (bt_BackSpace == obj){
backSpace();
} else if (bt_Cal == obj)
{
result();
}
}
public void numberInput()
{
getText = tf_getText.toString();
if (getText.length() == 1 && getText.indexOf("0") == 0)
{
tf_getText.delete(0,tf_getText.length());
}
tf_getText.append("" + numberIn);
tf_Display.setText(tf_getText.toString());
}
public void zero()
{
getText = tf_getText.toString();
if (0 <= getText.indexOf("."))
{
tf_getText.append("0");
tf_Display.setText(tf_getText.toString());
} else {
if (getText.length() == 1 && getText.indexOf("0") == 0)
{
tf_Display.setText("0");
}else
{
tf_getText.append("0");
tf_Display.setText(tf_getText.toString());
}
}
}
public void dotCheck()
{
getText = tf_getText.toString();
if (0 <= getText.indexOf("."))
{
tf_getText.append("");
tf_Display.setText(tf_getText.toString());
} else {
tf_getText.append(".");
tf_Display.setText(tf_getText.toString());
}
}
public void backSpace()
{
try
{
tf_getText.delete(0,tf_getText.length());
tf_getText.append(tf_Display.getText());
getText = tf_getText.delete(tf_getText.length()-1, tf_getText.length()).toString();
if (getText.length() == 0)
{
getText = "0";
}
tf_getText.delete(0,tf_getText.length());
tf_getText.append(getText);
tf_Display.setText(tf_getText.toString());
}
catch (StringIndexOutOfBoundsException se){}
}
public void calculate(byte cal_Check)
{
try
{
switch (cal_Check)
{
case PLUS:
cal_Buffer1 = Double.parseDouble(tf_Display.getText());
cal_Buffer2 = cal_Buffer1 + cal_Buffer2;
tf_getText.delete(0,tf_getText.length());
tf_Display.setText("" + cal_Buffer2);
break;
case MINUS:
cal_Buffer1 = Double.parseDouble(tf_Display.getText());
if (cal_Buffer2 == 0)
{
cal_Buffer2 = cal_Buffer1;
} else
{
cal_Buffer2 = cal_Buffer2 - cal_Buffer1;
}
tf_getText.delete(0,tf_getText.length());
tf_Display.setText("" + cal_Buffer2);
break;
case MULTIPLICATION:
cal_Buffer1 = Double.parseDouble(tf_Display.getText());
if (cal_Buffer2 == 0)
{
cal_Buffer2 = cal_Buffer1;
} else
{
cal_Buffer2 = cal_Buffer2 * cal_Buffer1;
}
tf_getText.delete(0,tf_getText.length());
tf_Display.setText("" + cal_Buffer2);
break;
case DIVISION:
cal_Buffer1 = Double.parseDouble(tf_Display.getText());
if (cal_Buffer2 == 0)
{
cal_Buffer2 = cal_Buffer1;
} else
{
cal_Buffer2 = cal_Buffer2 / cal_Buffer1;
}
tf_getText.delete(0,tf_getText.length());
tf_Display.setText("" + cal_Buffer2);
break;
default :
}
} catch(NumberFormatException ne) {}
this.cal_Check = cal_Check;
}
public void result()
{
try
{
if (cal_Check == PLUS)
{
String result = cal_Buffer2 + Double.parseDouble(tf_Display.getText()) + "";
tf_Display.setText(result);
} else if (cal_Check == MINUS)
{
String result = cal_Buffer2 - Double.parseDouble(tf_Display.getText()) + "";
tf_Display.setText(result);
} else if (cal_Check == MULTIPLICATION)
{
String result = cal_Buffer2 * Double.parseDouble(tf_Display.getText()) + "";
tf_Display.setText(result);
} else if (cal_Check == DIVISION)
{
String result = cal_Buffer2 / Double.parseDouble(tf_Display.getText()) + "";
tf_Display.setText(result);
}
}
catch (NumberFormatException ne){}
tf_getText.delete(0,tf_getText.length());
cal_Buffer1 = 0;
cal_Buffer2 = 0;
}
public static void main(String[] args)
{
new Calculator();
}
}
class keyAdapter implements KeyListener
{
public void keyPressed (KeyEvent e){}
public void keyReleased (KeyEvent e){}
public void keyTyped (KeyEvent e){}
}