import java.awt.
EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
public class calculator {
private JFrame frame;
private JTextField textField;
double first;
double second;
double result;
String operation;
String answer;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
calculator window = new calculator();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public calculator() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 268, 378);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.BOLD, 18));
textField.setBounds(10, 11, 230, 48);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String number= textField.getText()+btn7.getText();
textField.setText(number);
}
});
btn7.setFont(new Font("Tahoma", Font.BOLD, 18));
btn7.setBounds(10, 122, 55, 41);
frame.getContentPane().add(btn7);
JButton btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String number= textField.getText()+btn8.getText();
textField.setText(number);
}
});
btn8.setFont(new Font("Tahoma", Font.BOLD, 18));
btn8.setBounds(69, 122, 55, 41);
frame.getContentPane().add(btn8);
JButton btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String number= textField.getText()+btn9.getText();
textField.setText(number);
}
});
btn9.setFont(new Font("Tahoma", Font.BOLD, 18));
btn9.setBounds(128, 122, 55, 41);
frame.getContentPane().add(btn9);
JButton btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String number= textField.getText()+btn4.getText();
textField.setText(number);
}
});
btn4.setFont(new Font("Tahoma", Font.BOLD, 18));
btn4.setBounds(10, 174, 55, 41);
frame.getContentPane().add(btn4);
JButton btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String number= textField.getText()+btn5.getText();
textField.setText(number);
}
});
btn5.setFont(new Font("Tahoma", Font.BOLD, 18));
btn5.setBounds(69, 174, 55, 41);
frame.getContentPane().add(btn5);
JButton btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String number= textField.getText()+btn6.getText();
textField.setText(number);
}
});
btn6.setFont(new Font("Tahoma", Font.BOLD, 18));
btn6.setBounds(128, 174, 55, 41);
frame.getContentPane().add(btn6);
JButton btnMinus = new JButton("-");
btnMinus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first=Double.parseDouble(textField.getText());
textField.setText("");
operation="-";
}
});
btnMinus.setFont(new Font("Tahoma", Font.BOLD, 18));
btnMinus.setBounds(187, 122, 55, 41);
frame.getContentPane().add(btnMinus);
JButton btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String number= textField.getText()+btn1.getText();
textField.setText(number);
}
});
btn1.setFont(new Font("Tahoma", Font.BOLD, 18));
btn1.setBounds(10, 222, 55, 41);
frame.getContentPane().add(btn1);
JButton btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String number= textField.getText()+btn2.getText();
textField.setText(number);
}
});
btn2.setFont(new Font("Tahoma", Font.BOLD, 18));
btn2.setBounds(69, 222, 55, 41);
frame.getContentPane().add(btn2);
JButton btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String number= textField.getText()+btn3.getText();
textField.setText(number);
}
});
btn3.setFont(new Font("Tahoma", Font.BOLD, 18));
btn3.setBounds(128, 222, 55, 41);
frame.getContentPane().add(btn3);
JButton btnDivide = new JButton("/");
btnDivide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first=Double.parseDouble(textField.getText());
textField.setText("");
operation="/";
}
});
btnDivide.setFont(new Font("Tahoma", Font.BOLD, 18));
btnDivide.setBounds(187, 222, 55, 41);
frame.getContentPane().add(btnDivide);
JButton btnPlus = new JButton("+");
btnPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first=Double.parseDouble(textField.getText());
textField.setText("");
operation="+";
}
});
btnPlus.setFont(new Font("Tahoma", Font.BOLD, 18));
btnPlus.setBounds(187, 70, 55, 41);
frame.getContentPane().add(btnPlus);
JButton btnBackspace = new JButton("\uF0E7");
btnBackspace.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String backSpace= null;
if(textField.getText().length()>0)
{StringBuilder str=new
StringBuilder(textField.getText());
str.deleteCharAt(textField.getText().length()-1);
backSpace=str.toString();
textField.setText(backSpace);
}
}
});
btnBackspace.setFont(new Font("Wingdings", Font.BOLD, 18));
btnBackspace.setBounds(10, 70, 55, 41);
frame.getContentPane().add(btnBackspace);
JButton btnClear = new JButton("C");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(null);
}
});
btnClear.setFont(new Font("Tahoma", Font.BOLD, 18));
btnClear.setBounds(69, 70, 55, 41);
frame.getContentPane().add(btnClear);
JButton btndoublezero = new JButton("00");
btndoublezero.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String number= textField.getText()+btndoublezero.getText();
textField.setText(number);
}
});
btndoublezero.setFont(new Font("Tahoma", Font.BOLD, 16));
btndoublezero.setBounds(128, 70, 55, 41);
frame.getContentPane().add(btndoublezero);
JButton btnMultiply = new JButton("*");
btnMultiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first=Double.parseDouble(textField.getText());
textField.setText("");
operation="*";
}
});
btnMultiply.setVerticalAlignment(SwingConstants.BOTTOM);
btnMultiply.setFont(new Font("Tahoma", Font.BOLD, 18));
btnMultiply.setBounds(187, 174, 55, 41);
frame.getContentPane().add(btnMultiply);
JButton btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String number= textField.getText()+btn0.getText();
textField.setText(number);
});
btn0.setFont(new Font("Tahoma", Font.BOLD, 18));
btn0.setBounds(10, 274, 55, 41);
frame.getContentPane().add(btn0);
JButton btnpoint = new JButton(".");
btnpoint.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String number= textField.getText()+btnpoint.getText();
textField.setText(number);
}
});
btnpoint.setFont(new Font("Tahoma", Font.BOLD, 18));
btnpoint.setBounds(69, 274, 55, 41);
frame.getContentPane().add(btnpoint);
JButton btnEquals = new JButton("=");
btnEquals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String answer;
second=Double.parseDouble(textField.getText());
if (operation=="+")
{
result=first+second;
answer=String.format("%.2f", result);
textField.setText(answer);
}
else if (operation=="-")
{
result=first-second;
answer=String.format("%.2f", result);
textField.setText(answer);
}
else if (operation=="*")
{
result=first*second;
answer=String.format("%.2f", result);
textField.setText(answer);
}
else if (operation=="/")
{
result=first/second;
answer=String.format("%.2f", result);
textField.setText(answer);
}
else if (operation=="%")
{
result=first%second;
answer=String.format("%.2f", result);
textField.setText(answer);
}
}
});
btnEquals.setFont(new Font("Tahoma", Font.BOLD, 18));
btnEquals.setBounds(128, 274, 55, 41);
frame.getContentPane().add(btnEquals);
JButton btnPercent = new JButton("%");
btnPercent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
first=Double.parseDouble(textField.getText());
textField.setText("");
operation="%";
}
});
btnPercent.setFont(new Font("Tahoma", Font.BOLD, 16));
btnPercent.setBounds(187, 276, 55, 41);
frame.getContentPane().add(btnPercent);
}
}