Program by Avinash Kumar Pandey
import java.awt.BorderLayout; import java.awt.Button; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class PopupCalculatorApplet extends JApplet implements ActionListener { public void init() { Button calcButton = new Button("Calculator"); calcButton.addActionListener(this); Container contentPane = getContentPane(); contentPane.add(calcButton); } public void actionPerformed(ActionEvent evt) { if (calc.isVisible()) calc.setVisible(false); else calc.show(); } private JFrame calc = new CalculatorFrame(); } 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); else display.setText(display.getText() + s); start = false; } else { if (start) { if (s.equals("-")) { display.setText(s); start = false; } else op = s; } else { calculate(Double.parseDouble(display.getText())); op = s; start = true; } } } public void calculate(double n) { if (op.equals("+")) arg += n; else if (op.equals("-")) arg -= n; else if (op.equals("*")) arg *= n; else if (op.equals("/")) arg /= n; else if (op.equals("=")) arg = n; display.setText("" + arg); } private JTextField display; private double arg = 0; private String op = "="; private boolean start = true; } class CalculatorFrame extends JFrame { public CalculatorFrame() { setTitle("Calculator"); setSize(200, 200); Container contentPane = getContentPane(); contentPane.add(new CalculatorPanel()); } } |
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Resources #Terms of Use
Copyright©2012 electrofriends.com All Rights Reserved
Contact:info@electrofriends.com
Not a lot of folks feel the identical way while you. That involves me.. sorry
not up to the mark answer
can u give me a code which include only awt code without the use of swing