Java Applet Program For Calculator – Do Just In 10 Minutes

Applets are small Java applications that can be embedded with web browsers to display dynamic content and can run on the client-side directly.

In this post, we are given a simple example of a java applet program for Calculator.

In Java, we can create a Calculator program using the applet. Applets are not stand-alone programs, it can be viewed using direct JVM. Applet programs can be run with the help of web browsers directly and applets also don’t contain any main( ) method.

Applet programs are extended from java.applet.Applet class.

Java directly creates an instance of the applet class and invokes the init( ) method to initialize Applet Programs.

Also Read: How to reverse a String in Java

Program: Java applet program for Calculator

In this program, we will see an applet program for calculator application using JFrame. We will design the exact functionality of a real-world calculator application.

Let’s see a simple calculator program in Java using Applet.

package cf.java.applet; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JTextField; import java.awt.BorderLayout; import javax.swing.JButton; import java.awt.Font; import java.awt.Color; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class JavaAppletProgramForCalculator1 { private JFrame frame; private JTextField textField; double first, second, result; String operation, answer; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { JavaAppletProgramForCalculator1 window = new JavaAppletProgramForCalculator1(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public JavaAppletProgramForCalculator1() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 426, 458); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); textField = new JTextField(); textField.setFont(new Font("Tahoma", Font.BOLD, 16)); textField.setBackground(Color.WHITE); textField.setBounds(12, 13, 384, 66); frame.getContentPane().add(textField); textField.setColumns(10); JButton btnBack = new JButton("\uF0E7"); btnBack.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); } } }); btnBack.setBounds(12, 92, 86, 51); btnBack.setFont(new Font("Wingdings", Font.BOLD, 15)); frame.getContentPane().add(btnBack); JButton btn7 = new JButton("7"); btn7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String num = textField.getText() + btn7.getText(); textField.setText(num); } }); btn7.setBounds(12, 156, 86, 51); btn7.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btn7); JButton btn4 = new JButton("4"); btn4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String num = textField.getText() + btn4.getText(); textField.setText(num); } }); btn4.setBounds(12, 220, 86, 51); btn4.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btn4); JButton btn1 = new JButton("1"); btn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String num = textField.getText() + btn1.getText(); textField.setText(num); } }); btn1.setBounds(12, 284, 86, 51); btn1.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btn1); JButton btn0 = new JButton("0"); btn0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String num = textField.getText() + btn0.getText(); textField.setText(num); } }); btn0.setBounds(12, 347, 86, 51); btn0.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btn0); JButton btnClear = new JButton("C"); btnClear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(null); } }); btnClear.setBounds(111, 92, 86, 51); btnClear.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btnClear); JButton btn8 = new JButton("8"); btn8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String num = textField.getText() + btn8.getText(); textField.setText(num); } }); btn8.setBounds(111, 156, 86, 51); btn8.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btn8); JButton btn5 = new JButton("5"); btn5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String num = textField.getText() + btn5.getText(); textField.setText(num); } }); btn5.setBounds(111, 220, 86, 51); btn5.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btn5); JButton btn2 = new JButton("2"); btn2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String num = textField.getText() + btn2.getText(); textField.setText(num); } }); btn2.setBounds(111, 284, 86, 51); btn2.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btn2); JButton btnDot = new JButton("."); btnDot.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String num = textField.getText() + btnDot.getText(); textField.setText(num); } }); btnDot.setBounds(111, 347, 86, 51); btnDot.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btnDot); JButton btn00 = new JButton("00"); btn00.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String num = textField.getText() + btn00.getText(); textField.setText(num); } }); btn00.setBounds(211, 92, 86, 51); btn00.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btn00); JButton btn6 = new JButton("6"); btn6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String num = textField.getText() + btn6.getText(); textField.setText(num); } }); btn6.setBounds(211, 220, 86, 51); btn6.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btn6); JButton btn9 = new JButton("9"); btn9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String num = textField.getText() + btn9.getText(); textField.setText(num); } }); btn9.setBounds(211, 156, 86, 51); btn9.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btn9); JButton btn3 = new JButton("3"); btn3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String num = textField.getText() + btn3.getText(); textField.setText(num); } }); btn3.setBounds(211, 284, 86, 51); btn3.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btn3); JButton btnEqual = new JButton("="); btnEqual.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { 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 == "X") { 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); } } }); btnEqual.setBounds(211, 347, 86, 51); btnEqual.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btnEqual); JButton btnMinus = new JButton("-"); btnMinus.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { first = Double.parseDouble(textField.getText()); textField.setText(""); operation = "-"; } }); btnMinus.setBounds(310, 156, 86, 51); btnMinus.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btnMinus); JButton btnMultiply = new JButton("X"); btnMultiply.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { first = Double.parseDouble(textField.getText()); textField.setText(""); operation = "X"; } }); btnMultiply.setBounds(310, 220, 86, 51); btnMultiply.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btnMultiply); JButton btnDivision = new JButton("/"); btnDivision.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { first = Double.parseDouble(textField.getText()); textField.setText(""); operation = "/"; } }); btnDivision.setBounds(310, 284, 86, 51); btnDivision.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btnDivision); JButton btnPercent = new JButton("%"); btnPercent.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { first = Double.parseDouble(textField.getText()); textField.setText(""); operation = "%"; } }); btnPercent.setBounds(310, 347, 86, 51); btnPercent.setFont(new Font("Bookman Old Style", Font.BOLD, 15)); frame.getContentPane().add(btnPercent); 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("Bookman Old Style", Font.BOLD, 15)); btnPlus.setBounds(310, 91, 86, 51); frame.getContentPane().add(btnPlus); } }
Code language: JavaScript (javascript)

Explanation:

In above program, We have given an example of Java applet program for calculator.

We have designed interface for calculator program using JFrame

We have created all buttons related to Calculator number buttons like 0, 1, 2, …., 8, 9 and then we have created all other operation buttons like Addition(+), Subtraction(-), Multiplication(X), Division(/) and Modulus(%). Some other functionalities like backspace(<-), Clear, Dot(.) and Equal sign(=) for result are given.

You can go through above program to get clear knowledge of Java applet program for calculator.

Also Read: Palindrome Program In Java

Other Queries:

  • Explain Java applet program for calculator application.
  • Example of a simple calculator program in Java using applet.
  • How to do calculator program in java.

Leave a Comment