Press "Enter" to skip to content

Calculator Using Java Gui

Procedure.

First You need to open a project in netbeans or any java editor.

Then Design the interface that i do.

Calculator

Then Action every button and pest code one by one.

First right click on button and go add event handler, then go action and then go action performed then click

Full code of calculator.

package Calculate;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.Window;

public class SNcalculator {

private JFrame frmCalculator;
private JTextField textField;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SNcalculator window = new SNcalculator();
window.frmCalculator.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

String EnterNum1 , EnterNum2 , stringTotal;
int clickAdd,clickSub,clickMul,clickDiv;
Double firstDouble, secondDouble, Total, RB;

public SNcalculator() {
initialize();
centreWindow(frmCalculator);
}



public static void centreWindow(Window frame) { //JFrame Middle a Run Korar Method
Dimension dimension = Toolkit.
getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() – frame.getWidth()) / 2);
int y = (int) ((dimension.getHeight() – frame.getHeight()) / 2);
frame.setLocation(x, y);

}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmCalculator = new JFrame();
frmCalculator.setForeground(SystemColor.inactiveCaption);
frmCalculator.setBackground(new Color(153, 51, 255));
frmCalculator.setTitle(“Calculator”);
frmCalculator.setBounds(100, 100, 283, 438);
frmCalculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JMenuBar menuBar = new JMenuBar();
frmCalculator.setJMenuBar(menuBar);

JMenu mnMenu = new JMenu(“Menu”);
menuBar.add(mnMenu);

JMenuItem mntmStanderd = new JMenuItem(“Standerd”);
mntmStanderd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

frmCalculator.setTitle(“Calculator”);
frmCalculator.setBounds(100, 100, 283, 438);
textField.setBounds(10, 11, 247, 40);
}
});
mnMenu.add(mntmStanderd);

JMenuItem mntmScientific = new JMenuItem(“Scientific”);
mntmScientific.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

frmCalculator.setTitle(“Scientific”);
frmCalculator.setBounds(100, 100, 355, 438);
textField.setBounds(10, 11, 318, 40);
}
});
mnMenu.add(mntmScientific);
frmCalculator.getContentPane().setLayout(null);

textField = new JTextField();
textField.setEditable(false);
textField.setBounds(10, 11, 247, 40);
frmCalculator.getContentPane().add(textField);
textField.setColumns(10);

JButton btnBack = new JButton(“<–“);
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

int length = textField.getText().length();
int number;
String store;

if(length > 0)
{
number = length – 1;
StringBuilder back = new StringBuilder(textField.getText());
back.deleteCharAt(number);
store = back.toString();
textField.setText(store);
}
}
});
btnBack.setBounds(73, 62, 55, 42);
frmCalculator.getContentPane().add(btnBack);

JButton btnClear = new JButton(“C”);
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

textField.setText(“”);

}
});
btnClear.setBounds(138, 62, 55, 42);
frmCalculator.getContentPane().add(btnClear);

JButton btnRound = new JButton(“Ru”);
btnRound.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

RB = Double.parseDouble(textField.getText());
RB = (double) Math.round(RB);
textField.setText(RB.toString());
}
});
btnRound.setBounds(201, 62, 57, 42);
frmCalculator.getContentPane().add(btnRound);

JButton btn7 = new JButton(“7”);
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EnterNum1 = textField.getText() + btn7.getText();
textField.setText(EnterNum1);
}
});
btn7.setBounds(10, 154, 55, 40);
frmCalculator.getContentPane().add(btn7);

JButton btn8 = new JButton(“8”);
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EnterNum1 = textField.getText() + btn8.getText();
textField.setText(EnterNum1);
}
});
btn8.setBounds(73, 154, 55, 40);
frmCalculator.getContentPane().add(btn8);

JButton btn9 = new JButton(“9”);
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EnterNum1 = textField.getText() + btn9.getText();
textField.setText(EnterNum1);
}
});
btn9.setBounds(138, 154, 55, 40);
frmCalculator.getContentPane().add(btn9);

JButton btn4 = new JButton(“4”);
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EnterNum1 = textField.getText() + btn4.getText();
textField.setText(EnterNum1);
}
});
btn4.setBounds(10, 197, 55, 40);
frmCalculator.getContentPane().add(btn4);

JButton btn5 = new JButton(“5”);
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EnterNum1 = textField.getText() + btn5.getText();
textField.setText(EnterNum1);
}
});
btn5.setBounds(73, 197, 55, 40);
frmCalculator.getContentPane().add(btn5);

JButton btn6 = new JButton(“6”);
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EnterNum1 = textField.getText() + btn6.getText();
textField.setText(EnterNum1);
}
});
btn6.setBounds(138, 197, 55, 40);
frmCalculator.getContentPane().add(btn6);

JButton btn1 = new JButton(“1”);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EnterNum1 = textField.getText() + btn1.getText();
textField.setText(EnterNum1);
}
});
btn1.setBounds(10, 239, 55, 40);
frmCalculator.getContentPane().add(btn1);

JButton btn2 = new JButton(“2”);
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EnterNum1 = textField.getText() + btn2.getText();
textField.setText(EnterNum1);
}
});
btn2.setBounds(73, 239, 55, 40);
frmCalculator.getContentPane().add(btn2);

JButton btn3 = new JButton(“3”);
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EnterNum1 = textField.getText() + btn3.getText();
textField.setText(EnterNum1);
}
});
btn3.setBounds(138, 239, 55, 40);
frmCalculator.getContentPane().add(btn3);

JButton btn0 = new JButton(“0”);
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EnterNum1 = textField.getText() + btn0.getText();
textField.setText(EnterNum1);
}
});
btn0.setBounds(10, 281, 55, 40);
frmCalculator.getContentPane().add(btn0);

JButton btnDot = new JButton(“.”);
btnDot.setFont(new Font(“Tahoma”, Font.BOLD, 15));
btnDot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

if(! textField.getText().contains(“.”))
{
textField.setText(textField.getText() + btnDot.getText());
}
}
});
btnDot.setBounds(73, 281, 55, 40);
frmCalculator.getContentPane().add(btnDot);

JButton btnPlus = new JButton(“+”);
btnPlus.setFont(new Font(“Tahoma”, Font.PLAIN, 15));
btnPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

EnterNum1 = textField.getText();
firstDouble = Double.parseDouble(EnterNum1.toString());
textField.setText(“”);
clickAdd = 1;
}
});
btnPlus.setBounds(138, 281, 55, 40);
frmCalculator.getContentPane().add(btnPlus);

JButton btnSub = new JButton(“-“);
btnSub.setFont(new Font(“Tahoma”, Font.PLAIN, 20));
btnSub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

EnterNum1 = textField.getText();
firstDouble = Double.parseDouble(EnterNum1.toString());
textField.setText(“”);
clickSub = 2;
}
});
btnSub.setBounds(201, 281, 57, 40);
frmCalculator.getContentPane().add(btnSub);

JButton btnEqual = new JButton(“=”);
btnEqual.setFont(new Font(“Tahoma”, Font.PLAIN, 16));
btnEqual.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {


EnterNum2 = textField.getText();
secondDouble = Double.parseDouble(EnterNum2.toString());

if(clickAdd == 1)
{
Total = firstDouble + secondDouble;
stringTotal = String.valueOf(Total);
textField.setText(stringTotal);
clickAdd = 0;
}
if(clickSub == 2)
{
Total = (firstDouble) – (secondDouble);
stringTotal = String.valueOf(Total);
textField.setText(stringTotal);
clickSub = 0;
}
if(clickMul == 3)
{
Total = firstDouble * secondDouble;
stringTotal = String.valueOf(Total);
textField.setText(stringTotal);
clickMul = 0;
}
if(clickDiv == 4)
{
Total = firstDouble / secondDouble;
stringTotal = String.valueOf(Total);
textField.setText(stringTotal);
clickDiv = 0;
}

}
});
btnEqual.setBounds(10, 327, 118, 40);
frmCalculator.getContentPane().add(btnEqual);

JButton btnNewButton_10 = new JButton(“New button”);
btnNewButton_10.setBounds(138, 297, 35, -5);
frmCalculator.getContentPane().add(btnNewButton_10);

JButton btnMul = new JButton(“*”);
btnMul.setFont(new Font(“Tahoma”, Font.PLAIN, 15));
btnMul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

EnterNum1 = textField.getText();
firstDouble = Double.parseDouble(EnterNum1.toString());
textField.setText(“”);
clickMul = 3;
}
});
btnMul.setBounds(138, 327, 55, 40);
frmCalculator.getContentPane().add(btnMul);

JButton btnDiv = new JButton(“/”);
btnDiv.setFont(new Font(“Tahoma”, Font.PLAIN, 15));
btnDiv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

EnterNum1 = textField.getText();
firstDouble = Double.parseDouble(EnterNum1.toString());
textField.setText(“”);
clickDiv = 4;
}
});
btnDiv.setBounds(201, 327, 57, 40);
frmCalculator.getContentPane().add(btnDiv);

JButton btnSquarRoot = new JButton(“\u221A”);
btnSquarRoot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

RB = Double.parseDouble(String.valueOf(textField.getText()));
RB = Math.sqrt(RB);
textField.setText(String.valueOf(RB));

}
});
btnSquarRoot.setBounds(201, 109, 57, 42);
frmCalculator.getContentPane().add(btnSquarRoot);

JButton btnSquar = new JButton(“X^2”);
btnSquar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

RB = Double.parseDouble(textField.getText());
RB = Math.pow(RB, 2);
textField.setText(RB.toString());
}
});
btnSquar.setBounds(201, 154, 57, 40);
frmCalculator.getContentPane().add(btnSquar);

JButton btnCube = new JButton(“X^3”);
btnCube.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

RB = Double.parseDouble(textField.getText());
RB = Math.pow(RB , 3);
textField.setText(RB.toString());
}
});
btnCube.setBounds(201, 197, 57, 40);
frmCalculator.getContentPane().add(btnCube);

JButton btnPowaeN = new JButton(“X^n”);
btnPowaeN.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

RB = Double.parseDouble(textField.getText());
//RB = Math.pow(RB, b)
}
});
btnPowaeN.setBounds(201, 239, 57, 40);
frmCalculator.getContentPane().add(btnPowaeN);

JButton btnModlus = new JButton(“Mod”);
btnModlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

RB = Double.parseDouble(textField.getText());
RB = Math.abs(RB);
textField.setText(RB.toString());
}
});
btnModlus.setBounds(268, 327, 60, 40);
frmCalculator.getContentPane().add(btnModlus);

JButton btnSin = new JButton(“Sin”);
btnSin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

RB = Double.parseDouble(textField.getText());
RB = Math.sin(RB);
textField.setText(RB.toString());
}
});
btnSin.setBounds(268, 62, 60, 42);
frmCalculator.getContentPane().add(btnSin);

JButton btnCos = new JButton(“Cos”);
btnCos.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

RB = Double.parseDouble(textField.getText());
RB = Math.cos(RB);
textField.setText(RB.toString());
}
});
btnCos.setBounds(268, 110, 60, 40);
frmCalculator.getContentPane().add(btnCos);

JButton btnTan = new JButton(“Tan”);
btnTan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

RB = Double.parseDouble(textField.getText());
RB = Math.tan(RB);
textField.setText(RB.toString());
}
});
btnTan.setBounds(268, 154, 60, 40);
frmCalculator.getContentPane().add(btnTan);

JButton btnBinary = new JButton(“Bin”);
btnBinary.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

int R = Integer.parseInt(textField.getText());
textField.setText(Integer.toString(R,2));
}
});
btnBinary.setBounds(268, 197, 60, 40);
frmCalculator.getContentPane().add(btnBinary);

JButton btnHexa = new JButton(“Hex”);
btnHexa.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

int R = Integer.parseInt(textField.getText());
textField.setText(Integer.toString(R,16));
}
});
btnHexa.setBounds(268, 239, 60, 40);
frmCalculator.getContentPane().add(btnHexa);

JButton btnOctal = new JButton(“Octl”);
btnOctal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

int R = Integer.parseInt(textField.getText());
textField.setText(Integer.toString(R,8));
}
});
btnOctal.setBounds(268, 281, 60, 40);
frmCalculator.getContentPane().add(btnOctal);

JButton btnPai = new JButton(“\u03C0”);
btnPai.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

RB = Math.PI;
textField.setText(String.valueOf(RB));
}
});
btnPai.setBounds(10, 109, 55, 40);
frmCalculator.getContentPane().add(btnPai);

JButton btnLn = new JButton(“ln”);
btnLn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

RB = Double.parseDouble(textField.getText());
RB = Math.log10(RB);
textField.setText(String.valueOf(RB));
}
});
btnLn.setBounds(73, 110, 55, 40);
frmCalculator.getContentPane().add(btnLn);

JButton btnLog = new JButton(“log”);
btnLog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

RB = Double.parseDouble(textField.getText());
RB = Math.log(RB);
textField.setText(String.valueOf(RB));
}
});
btnLog.setBounds(138, 110, 55, 40);
frmCalculator.getContentPane().add(btnLog);

JButton btnOn = new JButton(“ON”);
btnOn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setEnabled(true);
btn9.setEnabled(true);
btn8.setEnabled(true);
btn7.setEnabled(true);
btn6.setEnabled(true);
btn5.setEnabled(true);
btn4.setEnabled(true);
btn3.setEnabled(true);
btn2.setEnabled(true);
btn1.setEnabled(true);
btn0.setEnabled(true);
btnPlus.setEnabled(true);
btnSub.setEnabled(true);
btnMul.setEnabled(true);
btnDiv.setEnabled(true);
btnDot.setEnabled(true);
btnEqual.setEnabled(true);
btnClear.setEnabled(true);
btnBack.setEnabled(true);
btnPai.setEnabled(true);
btnLn.setEnabled(true);
btnLog.setEnabled(true);
btnRound.setEnabled(true);
btnSquarRoot.setEnabled(true);
btnSquar.setEnabled(true);
btnCube.setEnabled(true);
btnPowaeN.setEnabled(true);
btnSin.setEnabled(true);
btnCos.setEnabled(true);
btnTan.setEnabled(true);
btnBinary.setEnabled(true);
btnHexa.setEnabled(true);
btnOctal.setEnabled(true);
btnModlus.setEnabled(true);
}
});
btnOn.setBounds(10, 62, 55, 20);
frmCalculator.getContentPane().add(btnOn);

JButton btnOff = new JButton(“OFF”);
btnOff.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setEnabled(true);
btn9.setEnabled(false);
btn8.setEnabled(false);
btn7.setEnabled(false);
btn6.setEnabled(false);
btn5.setEnabled(false);
btn4.setEnabled(false);
btn3.setEnabled(false);
btn2.setEnabled(false);
btn1.setEnabled(false);
btn0.setEnabled(false);
btnPlus.setEnabled(false);
btnSub.setEnabled(false);
btnMul.setEnabled(false);
btnDiv.setEnabled(false);
btnDot.setEnabled(false);
btnEqual.setEnabled(false);
btnClear.setEnabled(false);
btnBack.setEnabled(false);
btnPai.setEnabled(false);
btnLn.setEnabled(false);
btnLog.setEnabled(false);
btnRound.setEnabled(false);
btnSquarRoot.setEnabled(false);
btnSquar.setEnabled(false);
btnCube.setEnabled(false);
btnPowaeN.setEnabled(false);
btnSin.setEnabled(false);
btnCos.setEnabled(false);
btnTan.setEnabled(false);
btnBinary.setEnabled(false);
btnHexa.setEnabled(false);
btnOctal.setEnabled(false);
btnModlus.setEnabled(false);
}
});
btnOff.setBounds(10, 84, 55, 20);
frmCalculator.getContentPane().add(btnOff);
}
}

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *