Total members 11801 |It is currently Wed Oct 23, 2019 5:38 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Making calculator in JAVA
Code:

import java
.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator extends JPanel implements ActionListener
{
    private JTextField display = new JTextField("0");
    private String button = "789/456*123.=+";
    private double result = 0;
    private String operator = "=";
    private boolean calculating = true;
    private boolean decimal = false;
public Calculator()
{
    setLayout(new BorderLayout());
    
    display
.setEditable(false);
    add(display, "North");
    
    JPanel p 
= new JPanel();
    p.setLayout (new GridLayout (4,4));
    
        for 
(int i = 0; i < button.length(); i++)
        {
            JButton b = new JButton(button.substring(i, i + 1));
            p.add(b);
            b.addActionListener(this);
        }
        add(p, "Center");
    }

public void actionPerformed(ActionEvent evt)
{
    String cmd = evt.getActionCommand();
    if ('0' <= cmd.charAt(0) && cmd.charAt(0) <= '9' || cmd.equals("."))
    {
        System.out.println("Entered Here");
        if (calculating){
            System.out.println("Entered calculating "+ cmd.charAt(0));
            display.setText(cmd);
 }
        else
        display
.setText(display.getText() + cmd);
        calculating = false;
 }
    
    else
    
{
        if(calculating)
        {
            if (cmd.equals("-"))
            calculating = false;
        }
        else
        
{
            try{
                double x = Double.parseDouble(display.getText());
                calculate(x);
                operator = cmd;
                calculating = true;
            }catch(NumberFormatException e){
                display.setText("Illegal Number Format");
                calculating = false;
            }
 calculating = true;
        }
    }
}
private void calculate(double n)
{
 
if(operator.equals("+"))
    result += n;
    else if(operator.equals("-"))
    result -= n;
    else if(operator.equals("/"))
    result /= n;
    else if(operator.equals("*"))
    result *= n;
    else if(operator.equals("="))
    result = n;
    
    display
.setText("" + result);
}

public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setTitle("Calculator");
frame.setBounds(250,250,200,200);
frame.addWindowListener(new WindowAdapter()
{
 public void windowClosing(WindowEvent e)
    {
        System.exit(0);
    }
});

Container contentPane = frame.getContentPane();
contentPane.add(new Calculator());
frame.show();
}
}
 





Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Making calculator in JAVA
 help for making web pages using servlets     -  
 Making Colors Transparent with imagecolortransparent()     -  
 Calculator     -  
 C++ Calculator     -  
 Calculator     -  
 Scientific Calculator     -  
 Simple Calculator     -  
 C++ calculator with modification.     -  
 Build Calculator in OpenGL     -  
 Complex numbers calculator (C++)     -  



Topic Tags

Java Swing
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com