Calculator
Calculator
EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.SwingUtilities;
import java.math.*;
import java.lang.*;
import java.awt.Window.Type;
import java.awt.event.*;
/**
* Launch the application.
*/
double first;
double second;
double result;
String operation;
String answer;
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("CALCULATOR");
frame.setType(Type.UTILITY); //Set the display on top of the panel
as utility
frame.setBounds(100, 100, 256, 262);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnDot.setFont(new Font("Tahoma", Font.BOLD, 18));
btnDot.setBounds(67, 176, 58, 29);
frame.getContentPane().add(btnDot);
btnSqrt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}});
btnSqrt.setFont(new Font("Tahoma", Font.BOLD, 18));
btnSqrt.setBounds(123, 73, 58, 29);
frame.getContentPane().add(btnSqrt);
btnEqual.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);
if (second == 0)
{
textField.setText(" Invalid Division");
}
else if (result == result / second)
{
textField.setText( answer);
}
}
else if(operation=="%")
{
result= first%second;
answer= String.format("%.2f",result);
textField.setText(answer);
}
else if (operation=="√")
{
result = Double.parseDouble(textField.getText());
Double sqrt = Math.sqrt(result);
answer= String.format("%.2f",result);
textField.setText(Double.toString(sqrt));
}
}
}
);
btnEqual.setFont(new Font("Tahoma", Font.BOLD, 18));
btnEqual.setBounds(123, 176, 58, 29);
frame.getContentPane().add(btnEqual);
//**This button is for the Addition
btnPlus = new JButton("+");
btnPlus.addActionListener(new ActionListener() { //On clicking this
button it gets the content of the textField and
public void actionPerformed(ActionEvent e) { //set it to the
first and then sets the operations variable with the
first=Double.parseDouble(textField.getText()); //addition
operation.and also sets the textField to just a space
textField.setText("");
operation="+";
}
});
btnPlus.setFont(new Font("Tahoma", Font.BOLD, 18));
btnPlus.setBounds(178, 73, 58, 29);
frame.getContentPane().add(btnPlus);
}
});
btnSub.setFont(new Font("Tahoma", Font.BOLD, 18));
btnSub.setBounds(178, 97, 58, 29);
frame.getContentPane().add(btnSub);
}
});
btnMul.setFont(new Font("Tahoma", Font.BOLD, 18));
btnMul.setBounds(178, 124, 58, 29);
frame.getContentPane().add(btnMul);
});
btnDivide.setFont(new Font("Tahoma", Font.BOLD, 18));
btnDivide.setBounds(178, 151, 58, 29);
frame.getContentPane().add(btnDivide);
//**This button is for finding the percentage
btnPercent = new JButton("%");
btnPercent.addActionListener(new ActionListener() { //On
clicking this button it gets the content of the textField and
public void actionPerformed(ActionEvent e) { //set
it to the firstNum and then sets the operations variable with the
first=Double.parseDouble(textField.getText());
//percentage operation.and also sets the textField to just a space
textField.setText("");
operation="%";
}
});
btnPercent.setFont(new Font("Tahoma", Font.BOLD, 18));
btnPercent.setBounds(178, 176, 58, 29);
frame.getContentPane().add(btnPercent);
}
}