import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.*;
public class singletextfieldcalculator extends JFrame implements ActionListener{
JButton zero, one, two, three, four, five, six, seven, eight, nine, backspace;
JTextField field;
JButton plus, minus, multiply, divide, equal;
String operation=null;
int a, b, result;
public singletextfieldcalculator(){
super("Calculator");
Container c = getContentPane();
JPanel p = new JPanel();
field = new JTextField(15);
p.add(field);
backspace = new JButton("backspace");
backspace.addActionListener(this);
p.add(backspace);
one = new JButton("1");
one.addActionListener(this);
p.add(one);
two = new JButton("2");
two.addActionListener(this);
p.add(two);
three = new JButton("3");
three.addActionListener(this);
p.add(three);
four = new JButton("4");
four.addActionListener(this);
p.add(four);
five = new JButton("5");
five.addActionListener(this);
p.add(five);
six = new JButton("6");
six.addActionListener(this);
p.add(six);
seven = new JButton("7");
seven.addActionListener(this);
p.add(seven);
eight = new JButton("8");
eight.addActionListener(this);
p.add(eight);
nine = new JButton("9");
nine.addActionListener(this);
p.add(nine);
zero = new JButton("0");
zero.addActionListener(this);
p.add(zero);
plus = new JButton("+");
plus.addActionListener(this);
p.add(plus);
minus = new JButton("-");
minus.addActionListener(this);
p.add(minus);
multiply = new JButton("x");
multiply.addActionListener(this);
p.add(multiply);
divide = new JButton("/");
divide.addActionListener(this);
p.add(divide);
equal = new JButton("=");
equal.addActionListener(this);
p.add(equal);
c.add(p);
setSize(200,250);
setVisible(true);
}
public static void main(String args[]){
new singletextfieldcalculator();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==backspace){
String text = field.getText();
String text2 = text.substring(0, text.length()-1);
field.setText(""+text2);
}
else if(e.getSource() == zero)
field.setText(field.getText()+0);
else if(e.getSource() == one)
field.setText(field.getText()+1);
else if (e.getSource() == two)
field.setText(field.getText()+2);
else if (e.getSource() == three)
field.setText(field.getText()+3);
else if (e.getSource() == four)
field.setText(field.getText()+4);
else if (e.getSource() == five)
field.setText(field.getText()+5);
else if (e.getSource() == six)
field.setText(field.getText()+6);
else if (e.getSource() == seven)
field.setText(field.getText()+7);
else if (e.getSource() == eight)
field.setText(field.getText()+8);
else if (e.getSource() == nine)
field.setText(field.getText()+9);
if(e.getSource() == plus){
a = Integer.parseInt(field.getText());
field.setText("");
operation = "plus";
}
else if (e.getSource() == minus) {
a = Integer.parseInt(field.getText());
field.setText("");
operation = "minus";
}
else if (e.getSource() == multiply) {
a = Integer.parseInt(field.getText());
field.setText("");
operation = "multiply";
}
else if (e.getSource() == divide) {
a = Integer.parseInt(field.getText());
field.setText("");
operation = "divide";
}
else if (e.getSource() == equal) {
b = Integer.parseInt(field.getText());
field.setText("");
if(operation.equals("plus")) {
result = a+b;
field.setText(""+result);
}
else if(operation.equals("minus")) {
result = a-b;
field.setText(""+result);
}
else if(operation.equals("multiply")) {
result = a*b;
field.setText(""+result);
}
else if(operation.equals("divide")){
result = a/b;
field.setText(""+result);
}
operation=null;
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.*;
public class singletextfieldcalculator extends JFrame implements ActionListener{
JButton zero, one, two, three, four, five, six, seven, eight, nine, backspace;
JTextField field;
JButton plus, minus, multiply, divide, equal;
String operation=null;
int a, b, result;
public singletextfieldcalculator(){
super("Calculator");
Container c = getContentPane();
JPanel p = new JPanel();
field = new JTextField(15);
p.add(field);
backspace = new JButton("backspace");
backspace.addActionListener(this);
p.add(backspace);
one = new JButton("1");
one.addActionListener(this);
p.add(one);
two = new JButton("2");
two.addActionListener(this);
p.add(two);
three = new JButton("3");
three.addActionListener(this);
p.add(three);
four = new JButton("4");
four.addActionListener(this);
p.add(four);
five = new JButton("5");
five.addActionListener(this);
p.add(five);
six = new JButton("6");
six.addActionListener(this);
p.add(six);
seven = new JButton("7");
seven.addActionListener(this);
p.add(seven);
eight = new JButton("8");
eight.addActionListener(this);
p.add(eight);
nine = new JButton("9");
nine.addActionListener(this);
p.add(nine);
zero = new JButton("0");
zero.addActionListener(this);
p.add(zero);
plus = new JButton("+");
plus.addActionListener(this);
p.add(plus);
minus = new JButton("-");
minus.addActionListener(this);
p.add(minus);
multiply = new JButton("x");
multiply.addActionListener(this);
p.add(multiply);
divide = new JButton("/");
divide.addActionListener(this);
p.add(divide);
equal = new JButton("=");
equal.addActionListener(this);
p.add(equal);
c.add(p);
setSize(200,250);
setVisible(true);
}
public static void main(String args[]){
new singletextfieldcalculator();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==backspace){
String text = field.getText();
String text2 = text.substring(0, text.length()-1);
field.setText(""+text2);
}
else if(e.getSource() == zero)
field.setText(field.getText()+0);
else if(e.getSource() == one)
field.setText(field.getText()+1);
else if (e.getSource() == two)
field.setText(field.getText()+2);
else if (e.getSource() == three)
field.setText(field.getText()+3);
else if (e.getSource() == four)
field.setText(field.getText()+4);
else if (e.getSource() == five)
field.setText(field.getText()+5);
else if (e.getSource() == six)
field.setText(field.getText()+6);
else if (e.getSource() == seven)
field.setText(field.getText()+7);
else if (e.getSource() == eight)
field.setText(field.getText()+8);
else if (e.getSource() == nine)
field.setText(field.getText()+9);
if(e.getSource() == plus){
a = Integer.parseInt(field.getText());
field.setText("");
operation = "plus";
}
else if (e.getSource() == minus) {
a = Integer.parseInt(field.getText());
field.setText("");
operation = "minus";
}
else if (e.getSource() == multiply) {
a = Integer.parseInt(field.getText());
field.setText("");
operation = "multiply";
}
else if (e.getSource() == divide) {
a = Integer.parseInt(field.getText());
field.setText("");
operation = "divide";
}
else if (e.getSource() == equal) {
b = Integer.parseInt(field.getText());
field.setText("");
if(operation.equals("plus")) {
result = a+b;
field.setText(""+result);
}
else if(operation.equals("minus")) {
result = a-b;
field.setText(""+result);
}
else if(operation.equals("multiply")) {
result = a*b;
field.setText(""+result);
}
else if(operation.equals("divide")){
result = a/b;
field.setText(""+result);
}
operation=null;
}
}
}

0 comments:
Post a Comment