ADVANCE JAVA PROGRAMMING
22BECE30412
Practical-1
Aim: Create a simple calculator application using Swing
in java
Code:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
public class CalculatorDemo {
private JFrame frame;
int a, b, result, op;
String ans;
private JTextField txt_veiw;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalculatorDemo window = new CalculatorDemo();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public CalculatorDemo() {
intialize();
}
private void intialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
1
ADVANCE JAVA PROGRAMMING 22BECE30412
//Create Button "1"
JButton btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txt_veiw.setText(txt_veiw.getText() + "1");
}
});
btn1.setBounds(12, 94, 76, 25);
frame.getContentPane().add(btn1);
//Create Button "2"
JButton btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txt_veiw.setText(txt_veiw.getText() + "2");
}
});
btn2.setBounds(100, 94, 76, 25);
frame.getContentPane().add(btn2);
//Create Button "3"
JButton btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txt_veiw.setText(txt_veiw.getText() + "3");
}
});
btn3.setBounds(188, 94, 76, 25);
frame.getContentPane().add(btn3);
//Create Button "4"
JButton btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txt_veiw.setText(txt_veiw.getText() + "4");
}
});
btn4.setBounds(12, 131, 76, 25);
frame.getContentPane().add(btn4);
//Create Button "5"
JButton btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txt_veiw.setText(txt_veiw.getText() + "5");
}
2
ADVANCE JAVA PROGRAMMING 22BECE30412
});
btn5.setBounds(100, 131, 76, 25);
frame.getContentPane().add(btn5);
//Create Button "6"
JButton btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txt_veiw.setText(txt_veiw.getText() + "6");
}
});
btn6.setBounds(188, 131, 76, 25);
frame.getContentPane().add(btn6);
//Create Button "7"
JButton btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txt_veiw.setText(txt_veiw.getText() + "7");
}
});
btn7.setBounds(12, 168, 76, 25);
frame.getContentPane().add(btn7);
//Create Button "8"
JButton btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txt_veiw.setText(txt_veiw.getText() + "8");
}
});
btn8.setBounds(100, 168, 76, 25);
frame.getContentPane().add(btn8);
//Create Button "9"
JButton btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txt_veiw.setText(txt_veiw.getText() + "9");
}
});
btn9.setBounds(188, 168, 76, 25);
frame.getContentPane().add(btn9);
//Create Button "."
JButton btn_dot = new JButton(".");
btn_dot.addActionListener(new ActionListener() {
3
ADVANCE JAVA PROGRAMMING 22BECE30412
public void actionPerformed(ActionEvent e) {
txt_veiw.setText(txt_veiw.getText() + ".");
}
});
btn_dot.setBounds(12, 207, 76, 25);
frame.getContentPane().add(btn_dot);
//Create Button "0"
JButton btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txt_veiw.setText(txt_veiw.getText() + "0");
}
});
btn0.setBounds(100, 207, 76, 25);
frame.getContentPane().add(btn0);
//Create Button "Clear"
JButton btn_clear = new JButton("Clear");
btn_clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txt_veiw.setText("");
}
});
btn_clear.setBounds(188, 205, 76, 25);
frame.getContentPane().add(btn_clear);
//Create Button "+"
JButton btn_add = new JButton("+");
btn_add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Integer.parseInt(txt_veiw.getText());
txt_veiw.setText("");
op = 1;
}
});
btn_add.setBounds(299, 37, 117, 25);
frame.getContentPane().add(btn_add);
//Create Button "-"
JButton btn_sub = new JButton("-");
btn_sub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Integer.parseInt(txt_veiw.getText());
txt_veiw.setText("");
op = 2;
}
4
ADVANCE JAVA PROGRAMMING 22BECE30412
});
btn_sub.setBounds(299, 80, 117, 25);
frame.getContentPane().add(btn_sub);
//Create Button "*"
JButton btn_mul = new JButton("*");
btn_mul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Integer.parseInt(txt_veiw.getText());
txt_veiw.setText("");
op = 3;
}
});
btn_mul.setBounds(299, 117, 117, 25);
frame.getContentPane().add(btn_mul);
//Create Button "/"
JButton btn_div = new JButton("/");
btn_div.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Integer.parseInt(txt_veiw.getText());
txt_veiw.setText("");
op = 4;
}
});
btn_div.setBounds(299, 154, 117, 25);
frame.getContentPane().add(btn_div);
//Create Button "="
JButton btn_equal = new JButton("=");
btn_equal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
b = Integer.parseInt(txt_veiw.getText());
switch (op) {
case 1:result = a + b;
ans = String.format("%d", result);
txt_veiw.setText(ans);
break;
case 2:result = a - b;
ans = String.format("%d", result);
txt_veiw.setText(ans);
break;
case 3:result = a * b;
ans = String.format("%d", result);
txt_veiw.setText(ans);
5
ADVANCE JAVA PROGRAMMING 22BECE30412
break;
case 4:result = a / b;
ans = String.format("%d", result);
txt_veiw.setText(ans);
break;
default:
break;
}
}
});
btn_equal.setFont(new Font("Dialog", Font.BOLD, 18));
btn_equal.setForeground(Color.BLACK);
btn_equal.setBackground(Color.WHITE);
btn_equal.setBounds(299, 195, 117, 37);
frame.getContentPane().add(btn_equal);
txt_veiw = new JTextField();
txt_veiw.setBounds(38, 28, 205, 34);
frame.getContentPane().add(txt_veiw);
txt_veiw.setColumns(10);
}
}
Output: