You are on page 1of 7

CALCULATOR

1. Install window builder (must containing Swing Designer)


2. Create a project named Calculator and package name app.
3. Now click the app package then go to file>new>other>windowbuilder>swing
designer>application window.

a. Now go to design tab (shown below your source code).


b. Take the absolute layout.
c. Create your calculator using jtextfield, jbutton.
d. Give the name to the text which is to be displayed on the JTextField and JButton in the Text
and its variable name in the variable section in Properties
e. Now add the events for the buttons by double clicking on each button.
package app;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;

public class calci {

private JFrame frame;


private JTextField textField;

private JButton btn0;


private JButton btn1;
private JButton btn2;
private JButton btn3;
private JButton btn4;
private JButton btn5;
private JButton btn6;
private JButton btn7;
private JButton btn8;
private JButton btn9;

private JButton btnback;


private JButton btnclear;
private JButton btndoublezero;

private JButton btnsum;


private JButton btnsub;
private JButton btnmul;
private JButton btndiv;
private JButton btnmod;

private JButton btndot;


private JButton btnequal;

double n1; //it takes first number from the user.


double n2; //it takes second number from the user.
double result; //which stores the result of first and second number.
String operation; //it holds the which operation to be done.

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

});
}

/**
* Create the application.
*/
public calci() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 343, 433);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

textField = new JTextField();


textField.setBounds(10, 11, 302, 58);
frame.getContentPane().add(textField);
textField.setColumns(10);

//button 0
btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn0.getText();
textField.setText(number);
}
});
btn0.setBounds(10, 325, 68, 58);
frame.getContentPane().add(btn0);

//button 1
btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn1.getText();
textField.setText(number);
}
});
btn1.setBounds(10, 263, 68, 58);
frame.getContentPane().add(btn1);

//button 2
btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn2.getText();
textField.setText(number);
}
});
btn2.setBounds(88, 263, 68, 58);
frame.getContentPane().add(btn2);

//button 3
btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn3.getText();
textField.setText(number);
}
});
btn3.setBounds(166, 263, 68, 58);
frame.getContentPane().add(btn3);

//button 4
btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn4.getText();
textField.setText(number);
}
});
btn4.setBounds(10, 202, 68, 58);
frame.getContentPane().add(btn4);

//button 5
btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn5.getText();
textField.setText(number);
}
});
btn5.setBounds(88, 202, 68, 58);
frame.getContentPane().add(btn5);

//button 6
btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn6.getText();
textField.setText(number);
}
});
btn6.setBounds(166, 202, 68, 58);
frame.getContentPane().add(btn6);

//button 7
btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn7.getText();
textField.setText(number);
}
});
btn7.setBounds(10, 141, 68, 58);
frame.getContentPane().add(btn7);

//button 8
btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn8.getText();
textField.setText(number);
}
});
btn8.setBounds(88, 141, 68, 58);
frame.getContentPane().add(btn8);
//button 9
btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn9.getText();
textField.setText(number);
}
});
btn9.setBounds(166, 141, 68, 58);
frame.getContentPane().add(btn9);

//button backspace
btnback = new JButton("\uf0e7");
btnback.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String s = textField.getText();
if(s.length()>0)
{
s=s.substring(0,s.length()-1);
textField.setText(s);
}
}
});
btnback.setFont(new Font("Wingdings", Font.PLAIN, 11));
btnback.setBounds(10, 80, 68, 58);
frame.getContentPane().add(btnback);

//button clear
btnclear = new JButton("C");
btnclear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(null);
}
});
btnclear.setBounds(88, 80, 68, 58);
frame.getContentPane().add(btnclear);

//button 00
btndoublezero = new JButton("00");
btndoublezero.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number =
textField.getText()+btndoublezero.getText();
textField.setText(number);
}
});
btndoublezero.setBounds(166, 80, 68, 58);
frame.getContentPane().add(btndoublezero);

//button sum(+)
btnsum = new JButton("+");
btnsum.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
n1 = Double.parseDouble(textField.getText());
textField.setText(null);
operation="+"; //to indicate that + button is
pressed.
}
});
btnsum.setBounds(244, 80, 68, 58);
frame.getContentPane().add(btnsum);

//button difference(-)
btnsub = new JButton("-");
btnsub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
n1 = Double.parseDouble(textField.getText());
textField.setText(null);
operation="-";
}
});
btnsub.setBounds(244, 141, 68, 58);
frame.getContentPane().add(btnsub);

//button product(*)
btnmul = new JButton("*");
btnmul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
n1 = Double.parseDouble(textField.getText());
textField.setText(null);
operation="*";
}
});
btnmul.setBounds(244, 202, 68, 58);
frame.getContentPane().add(btnmul);

//button division(/)
btndiv = new JButton("/");
btndiv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
n1 = Double.parseDouble(textField.getText());
textField.setText(null);
operation="/";
}
});
btndiv.setBounds(244, 263, 68, 58);
frame.getContentPane().add(btndiv);

//button percent(%)
btnmod = new JButton("%");
btnmod.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
n1 = Double.parseDouble(textField.getText());
textField.setText(null);
operation="%";
}
});
btnmod.setBounds(244, 325, 68, 58);
frame.getContentPane().add(btnmod);

//button dot
btndot = new JButton(".");
btndot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number =
textField.getText()+btndot.getText();
textField.setText(number);
}
});
btndot.setBounds(88, 325, 68, 58);
frame.getContentPane().add(btndot);
//button equal(=)
btnequal = new JButton("=");
btnequal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
n2 = Double.parseDouble(textField.getText());
switch(operation)
{
case "+":
result = n1+n2;
textField.setText(Double.toString(result));
break;

case "-":
result = n1-n2;
textField.setText(Double.toString(result));
break;
case "*":
result = n1*n2;
textField.setText(Double.toString(result));
break;
case "/":
result = n1/n2;
textField.setText(Double.toString(result));
break;
case "%":
result = n1%n2;
textField.setText(Double.toString(result));
break;

}
});
btnequal.setBounds(166, 325, 68, 58);
frame.getContentPane().add(btnequal);
}
}

To make a JAR executable file


1. Go to File > export > java > runnable JAR file > export destination – select desktop > file
name - calci – Calculator > finish.
2. Now, run your application by double clicking it.

You might also like