Untitled
unknown
plain_text
2 years ago
10 kB
20
Indexable
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main
{
public static void main(String[] args)
{
Calculator c=new Calculator();
}
}
public class Calculator implements ActionListener {
boolean isButtonClicked = false;
int mark;
String oldValue,newValue,oldResult;
JFrame jf;
JLabel display_lable;
JButton sevenButton, eightButton, nineButton, fourButton, fiveButton, sixButton, oneButton, twoButton, threeButton;
JButton zeroButton, addButton, minusButton, mulButton, divButton, clearButton, equalButton, dotButton,ansButton;
Calculator() {
jf = new JFrame("Calculator");
jf.setLayout(null);
jf.getContentPane().setBackground(Color.white);
jf.setSize(700, 630);
jf.setLocation(50, 50);
display_lable = new JLabel();
display_lable.setBounds(20, 30, 620, 60);
display_lable.setBackground(Color.lightGray);
display_lable.setFont(new Font("Arial", Font.PLAIN, 40));
display_lable.setOpaque(true);
display_lable.setHorizontalAlignment(SwingConstants.RIGHT);
jf.add(display_lable);
sevenButton = new JButton("7");
sevenButton.setBounds(50,130,80,80);
sevenButton.setFont(new Font("Arial", Font.PLAIN, 40));
sevenButton.addActionListener(this);
jf.add(sevenButton);
eightButton = new JButton("8");
eightButton.setBounds(160, 130, 80, 80);
eightButton.setFont(new Font("Arial", Font.PLAIN, 40));
eightButton.addActionListener(this);
jf.add(eightButton);
nineButton = new JButton("9");
nineButton.setBounds(270, 130, 80, 80);
nineButton.setFont(new Font("Arial", Font.PLAIN, 40));
nineButton.addActionListener(this);
jf.add(nineButton);
divButton = new JButton("/");
divButton.setBounds(380, 130, 80, 80);
divButton.setFont(new Font("Arial", Font.PLAIN, 40));
divButton.addActionListener(this);
jf.add(divButton);
fourButton = new JButton("4");
fourButton.setBounds(50, 240, 80, 80);
fourButton.setFont(new Font("Arial", Font.PLAIN, 40));
fourButton.addActionListener(this);
jf.add(fourButton);
fiveButton = new JButton("5");
fiveButton.setBounds(160, 240, 80, 80);
fiveButton.setFont(new Font("Arial", Font.PLAIN, 40));
fiveButton.addActionListener(this);
jf.add(fiveButton);
sixButton = new JButton("6");
sixButton.setBounds(270, 240, 80, 80);
sixButton.setFont(new Font("Arial", Font.PLAIN, 40));
sixButton.addActionListener(this);
jf.add(sixButton);
mulButton = new JButton("x");
mulButton.setBounds(380, 240, 80, 80);
mulButton.setFont(new Font("Arial", Font.PLAIN, 40));
mulButton.addActionListener(this);
jf.add(mulButton);
oneButton = new JButton("1");
oneButton.setBounds(50, 350, 80, 80);
oneButton.setFont(new Font("Arial", Font.PLAIN, 40));
oneButton.addActionListener(this);
jf.add(oneButton);
twoButton = new JButton("2");
twoButton.setBounds(160, 350, 80, 80);
twoButton.setFont(new Font("Arial", Font.PLAIN, 40));
twoButton.addActionListener(this);
jf.add(twoButton);
threeButton = new JButton("3");
threeButton.setBounds(270, 350, 80, 80);
threeButton.setFont(new Font("Arial", Font.PLAIN, 40));
threeButton.addActionListener(this);
jf.add(threeButton);
minusButton = new JButton("-");
minusButton.setBounds(380, 350, 80, 80);
minusButton.setFont(new Font("Arial", Font.PLAIN, 40));
minusButton.addActionListener(this);
jf.add(minusButton);
dotButton = new JButton(".");
dotButton.setBounds(50, 460, 80, 80);
dotButton.setFont(new Font("Arial", Font.PLAIN, 40));
dotButton.addActionListener(this);
jf.add(dotButton);
zeroButton = new JButton("0");
zeroButton.setBounds(160, 460, 80, 80);
zeroButton.setFont(new Font("Arial", Font.PLAIN, 40));
zeroButton.addActionListener(this);
jf.add(zeroButton);
equalButton = new JButton("=");
equalButton.setBounds(270, 460, 80, 80);
equalButton.setFont(new Font("Arial", Font.PLAIN, 40));
equalButton.setBackground(Color.green);
equalButton.addActionListener(this);
jf.add(equalButton);
addButton = new JButton("+");
addButton.setBounds(380, 460, 80, 80);
addButton.setFont(new Font("Arial", Font.PLAIN, 40));
addButton.addActionListener(this);
jf.add(addButton);
clearButton = new JButton("clear");
clearButton.setBounds(490, 130, 80, 80);
clearButton.setFont(new Font("Arial", Font.PLAIN, 20));
clearButton.setBackground(Color.red);
clearButton.addActionListener(this);
jf.add(clearButton);
ansButton = new JButton("Ans");
ansButton.setBounds(490, 240, 80, 80);
ansButton.setFont(new Font("Arial", Font.PLAIN, 25));
ansButton.setBackground(Color.ORANGE);
ansButton.addActionListener(this);
jf.add(ansButton);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==eightButton){
if(isButtonClicked){
display_lable.setText("8");
isButtonClicked=false;
}else{
display_lable.setText(display_lable.getText()+"8");}
}else if(e.getSource()==sevenButton){
if(isButtonClicked){
display_lable.setText("7");
isButtonClicked=false;
}else{
display_lable.setText(display_lable.getText()+"7");}
}else if(e.getSource()==nineButton){
if(isButtonClicked){
display_lable.setText("9");
isButtonClicked=false;
}else{
display_lable.setText(display_lable.getText()+"9");}
}else if(e.getSource()==fourButton){
if(isButtonClicked){
display_lable.setText("4");
isButtonClicked=false;
}else{
display_lable.setText(display_lable.getText()+"4");}
}else if(e.getSource()==fiveButton){
if(isButtonClicked){
display_lable.setText("5");
isButtonClicked=false;
}else{
display_lable.setText(display_lable.getText()+"5");}
}else if(e.getSource()==sixButton){
if(isButtonClicked){
display_lable.setText("6");
isButtonClicked=false;
}else{
display_lable.setText(display_lable.getText()+"6");}
}else if(e.getSource()==oneButton){
if(isButtonClicked){
display_lable.setText("1");
isButtonClicked=false;
}else{
display_lable.setText(display_lable.getText()+"1");}
}else if(e.getSource()==twoButton){
if(isButtonClicked){
display_lable.setText("2");
isButtonClicked=false;
}else{
display_lable.setText(display_lable.getText()+"2");}
}else if(e.getSource()==threeButton){
if(isButtonClicked){
display_lable.setText("3");
isButtonClicked=false;
}else{
display_lable.setText(display_lable.getText()+"3");}
}else if(e.getSource()==zeroButton){
if(isButtonClicked){
display_lable.setText("0");
isButtonClicked=false;
}else{
display_lable.setText(display_lable.getText()+"0");}
}else if(e.getSource()==dotButton){
if(isButtonClicked){
display_lable.setText(".");
isButtonClicked=false;
}else{
display_lable.setText(display_lable.getText()+".");}
} else if(e.getSource()==addButton) {
isButtonClicked=true;
oldValue=display_lable.getText();
mark=1;
}else if(e.getSource()==minusButton) {
isButtonClicked=true;
oldValue=display_lable.getText();
mark=2;
}else if(e.getSource()==mulButton) {
isButtonClicked=true;
oldValue=display_lable.getText();
mark=3;
}else if(e.getSource()==divButton) {
isButtonClicked=true;
oldValue=display_lable.getText();
mark=4;
} else if(e.getSource()==clearButton){
display_lable.setText("");
}else if(e.getSource()==ansButton){
display_lable.setText(oldResult);
} else if(e.getSource()==equalButton){
newValue=display_lable.getText();
float newValueF=Float.parseFloat(newValue);
float oldValueF=Float.parseFloat(oldValue);
//going to perform operations
if(mark==1){
float result=oldValueF+newValueF;
display_lable.setText(result+"");
} else if(mark==2){
float result=oldValueF-newValueF;
display_lable.setText(result+"");
}else if(mark==3){
float result=oldValueF*newValueF;
display_lable.setText(result+"");
}else if(mark==4){
float result=oldValueF/newValueF;
display_lable.setText(result+"");
}
oldResult=display_lable.getText();
}
}
}
Editor is loading...
Leave a Comment