Popup Calculator : Calculator « Swing Components « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Swing Components » CalculatorScreenshots 
Popup Calculator


/**
 @version 1.20 27 Jun 1998
 @author Cay Horstmann
 */

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(44));
    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(200200);

    Container contentPane = getContentPane();
    contentPane.add(new CalculatorPanel());
  }
}

           
       
Related examples in the same category
w_w_w___.jav__a___2__s___.___c__o_m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.