package JFrameClaculator;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import layout.testing2;
public class Calculator extends JPanel implements ActionListener{
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
static String textop;
static String textop1;
static int x;
static int x1;
static int x2;
static int x3;
public static final char LF = '\n';
public static final char CR = '\r';
static JTextField textField;
protected static JButton b1;
protected static JButton b2;
protected static JButton b3;
protected static JButton b4;
protected static JButton b5;
protected static JButton b6;
protected static JButton b7;
protected static JButton b8;
protected static JButton b9;
protected static JButton b10;
protected static JButton b11;
protected static JButton b12;
protected static JButton b13;
protected static JButton b14;
protected static JButton b15;
protected static JButton b16;
private static void createAndShowGUI() {
JFrame frame = new JFrame("Calculator v2.1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
SpringLayout layout = new SpringLayout();
contentPane.setLayout(layout);
textField = new JTextField("", 17);
textField.setEditable(false);
b1 = new JButton("1");
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING);
b1.addActionListener(new Calculator());
b1.setActionCommand("1");
b2 = new JButton("2");
b2.setVerticalTextPosition(AbstractButton.CENTER);
b2.setHorizontalTextPosition(AbstractButton.LEADING);
b2.addActionListener(new Calculator());
b2.setActionCommand("2");
b3 = new JButton("3");
b3.setVerticalTextPosition(AbstractButton.CENTER);
b3.setHorizontalTextPosition(AbstractButton.LEADING);
b3.addActionListener(new Calculator());
b3.setActionCommand("3");
b4 = new JButton("4");
b4.setVerticalTextPosition(AbstractButton.CENTER);
b4.setHorizontalTextPosition(AbstractButton.LEADING);
b4.addActionListener(new Calculator());
b4.setActionCommand("4");
b5 = new JButton("5");
b5.setVerticalTextPosition(AbstractButton.CENTER);
b5.setHorizontalTextPosition(AbstractButton.LEADING);
b5.addActionListener(new Calculator());
b5.setActionCommand("5");
b6 = new JButton("6");
b6.setVerticalTextPosition(AbstractButton.CENTER);
b6.setHorizontalTextPosition(AbstractButton.LEADING);
b6.addActionListener(new Calculator());
b6.setActionCommand("6");
b7 = new JButton("7");
b7.setVerticalTextPosition(AbstractButton.CENTER);
b7.setHorizontalTextPosition(AbstractButton.LEADING);
b7.addActionListener(new Calculator());
b7.setActionCommand("7");
b8 = new JButton("8");
b8.setVerticalTextPosition(AbstractButton.CENTER);
b8.setHorizontalTextPosition(AbstractButton.LEADING);
b8.addActionListener(new Calculator());
b8.setActionCommand("8");
b9 = new JButton("9");
b9.setVerticalTextPosition(AbstractButton.CENTER);
b9.setHorizontalTextPosition(AbstractButton.LEADING);
b9.addActionListener(new Calculator());
b9.setActionCommand("9");
b10 = new JButton("0");
b10.setVerticalTextPosition(AbstractButton.CENTER);
b10.setHorizontalTextPosition(AbstractButton.LEADING);
b10.addActionListener(new Calculator());
b10.setActionCommand("0");
b11 = new JButton("+");
b11.setVerticalTextPosition(AbstractButton.CENTER);
b11.setHorizontalTextPosition(AbstractButton.LEADING);
b11.addActionListener(new Calculator());
b11.setActionCommand("+");
b12 = new JButton("-");
b12.setVerticalTextPosition(AbstractButton.CENTER);
b12.setHorizontalTextPosition(AbstractButton.LEADING);
b12.addActionListener(new Calculator());
b12.setActionCommand("-");
b13 = new JButton("x");
b13.setVerticalTextPosition(AbstractButton.CENTER);
b13.setHorizontalTextPosition(AbstractButton.LEADING);
b13.addActionListener(new Calculator());
b13.setActionCommand("x");
b14 = new JButton("/");
b14.setVerticalTextPosition(AbstractButton.CENTER);
b14.setHorizontalTextPosition(AbstractButton.LEADING);
b14.addActionListener(new Calculator());
b14.setActionCommand("/");
b15 = new JButton("C");
b15.setVerticalTextPosition(AbstractButton.CENTER);
b15.setHorizontalTextPosition(AbstractButton.LEADING);
b15.addActionListener(new Calculator());
b15.setActionCommand("C");
b16 = new JButton("=");
b16.setVerticalTextPosition(AbstractButton.CENTER);
b16.setHorizontalTextPosition(AbstractButton.LEADING);
b16.addActionListener(new Calculator());
b16.setActionCommand("=");
contentPane.add(textField);
contentPane.add(b1);
contentPane.add(b2);
contentPane.add(b3);
contentPane.add(b4);
contentPane.add(b5);
contentPane.add(b6);
contentPane.add(b7);
contentPane.add(b8);
contentPane.add(b9);
contentPane.add(b10);
contentPane.add(b11);
contentPane.add(b12);
contentPane.add(b13);
contentPane.add(b14);
contentPane.add(b15);
contentPane.add(b16);
b1.setPreferredSize(new Dimension(45, 30));
b2.setPreferredSize(new Dimension(45, 30));
b3.setPreferredSize(new Dimension(45, 30));
b4.setPreferredSize(new Dimension(45, 30));
b5.setPreferredSize(new Dimension(45, 30));
b6.setPreferredSize(new Dimension(45, 30));
b7.setPreferredSize(new Dimension(45, 30));
b8.setPreferredSize(new Dimension(45, 30));
b9.setPreferredSize(new Dimension(45, 30));
b10.setPreferredSize(new Dimension(45, 30));
b11.setPreferredSize(new Dimension(45, 30));
b12.setPreferredSize(new Dimension(45, 30));
b13.setPreferredSize(new Dimension(45, 30));
b14.setPreferredSize(new Dimension(45, 30));
b15.setPreferredSize(new Dimension(95, 30));
b16.setPreferredSize(new Dimension(195, 30));
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, textField,
30,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, textField,
10,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b1,
30,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b1,
110,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b2,
80,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b2,
110,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b3,
130,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b3,
110,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b4,
30,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b4,
75,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b5,
80,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b5,
75,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b6,
130,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b6,
75,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b7,
30,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b7,
40,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b8,
80,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b8,
40,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b9,
130,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b9,
40,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b10,
30,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b10,
145,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b11,
180,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b11,
40,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b12,
180,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b12,
75,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b13,
180,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b13,
110,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b14,
180,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b14,
145,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b15,
80,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b15,
145,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b16,
30,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b16,
180,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
frame.pack();
frame.setSize(263, 259);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==b1) {
String text = textField.getText();
textField.setText(text+"1");
}
if (e.getSource()==b2) {
String text = textField.getText();
textField.setText(text+"2");
}
if (e.getSource()==b3) {
String text = textField.getText();
textField.setText(text+"3");
}
if (e.getSource()==b4) {
String text = textField.getText();
textField.setText(text+"4");
}
if (e.getSource()==b5) {
String text = textField.getText();
textField.setText(text+"5");
}
if (e.getSource()==b6) {
String text = textField.getText();
textField.setText(text+"6");
}
if (e.getSource()==b7) {
String text = textField.getText();
textField.setText(text+"7");
}
if (e.getSource()==b8) {
String text = textField.getText();
textField.setText(text+"8");
}
if (e.getSource()==b9) {
String text = textField.getText();
textField.setText(text+"9");
}
if (e.getSource()==b10) {
String text = textField.getText();
textField.setText(text+"0");
}
if (e.getSource()==b11) {
textop = textField.getText();
textField.setText("");
x = x + 1;
}
if (e.getSource()==b12) {
textop = textField.getText();
textField.setText("");
x1 = x1 + 1;
}
if (e.getSource()==b13) {
textop = textField.getText();
textField.setText("");
x2 = x2 + 1;
}
if (e.getSource()==b14) {
textop = textField.getText();
textField.setText("");
x3 = x3 + 1;
}
if (e.getSource()==b15) {
String str = textField.getText();
if (str == null) {
}
int strLen = str.length();
if(strLen != 0){
if (strLen < 2) {
textField.setText("");
}
int lastIdx = strLen - 1;
String ret = str.substring(0, lastIdx);
char last = str.charAt(lastIdx);
if (last == LF) {
if (ret.charAt(lastIdx - 1) == CR) {
textField.setText(ret.substring(0, lastIdx - 1));
}
}
textField.setText(ret);
}
}
if (e.getSource()==b16) {
if(x % 2 != 0)
{
textop1 = textField.getText();
int op = Integer.parseInt(textop);
int op1 = Integer.parseInt(textop1);
int opFinal = op+op1;
String Final = Integer.toString(opFinal);
textField.setText(Final);
}
if(x1 % 2 != 0)
{
textop1 = textField.getText();
int op = Integer.parseInt(textop);
int op1 = Integer.parseInt(textop1);
int opFinal = op-op1;
String Final = Integer.toString(opFinal);
textField.setText(Final);
}
if(x2 % 2 != 0)
{
textop1 = textField.getText();
int op = Integer.parseInt(textop);
int op1 = Integer.parseInt(textop1);
int opFinal = op*op1;
String Final = Integer.toString(opFinal);
textField.setText(Final);
}
if(x3 % 2 != 0)
{
textop1 = textField.getText();
int op = Integer.parseInt(textop);
int op1 = Integer.parseInt(textop1);
int opFinal = op/op1;
String Final = Integer.toString(opFinal);
textField.setText(Final);
}
else
{
}
}
}
}