Most asked top Interview Questions and Answers | Online Test | Mock Test
Education platform for interview prep, online tests, tutorials, and live practice

Build skills with focused learning paths, mock tests, and interview-ready content.

WithoutBook brings subject-wise interview questions, online practice tests, tutorials, and comparison guides into one responsive learning workspace.

Search the library
Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

WithoutBook Forum

Ques. Please give me a source code example that makes a calculator on java. awt or swing anything can be given.

- Please give me a source code example that makes a calculator on java. awt or swing anything can be given.

Posted on Sep 28, 2010 by Om
Ans.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class CalculatorPanel extends JPanel
implements ActionListener
{ public CalculatorPanel()
{ setLayout(new BorderLayout());

display = new JTextField("0");
display.setEditable(false);
add(display, "North");

JPanel p = new JPanel();
p.setLayout(new GridLayout(4, 4));
String buttons = "789/456*123-0.=+";
for (int i = 0; i < buttons.length(); i++)
addButton(p, buttons.substring(i, i + 1));
add(p, "Center");
}

private void addButton(Container c, String s)
{ JButton b = new JButton(s);
c.add(b);
b.addActionListener(this);
}

public void actionPerformed(ActionEvent evt)
{ String s = evt.getActionCommand();
if ('0' <= s.charAt(0) && s.charAt(0) <= '9'
'' s.equals("."))
{ if (start) display.setText(s);<
Posted on Sep 28, 2010 by Arindam Ghosh

Enter your Answer

Name
Email Address
Answer
Security Check
Solve this to continue: 1 + 2 = ?
©2026 WithoutBook