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
©2024 WithoutBook