Results 1 to 2 of 2
  1. #1
    nemo is offline Member
    Join Date
    May 2007
    Posts
    2
    Rep Power
    0

    Default Swing Calculator

    I want to code a Swing based calculator. Do you have any simple examples?

  2. #2
    levent Guest

    Default

    Here is a simple example. Good luck!

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.DecimalFormat;
    
    class BasicCalculator extends JFrame implements ActionListener, MouseListener
    {
    
      JTextField display = new JTextField();
      JButton[] number = new JButton[11];
      JButton addBtn = new JButton("+");
      double currentTotal = 0.0;
      DecimalFormat df = new DecimalFormat("0.00");
      boolean totalDisplayed = true;
    
      public BasicCalculator()
      {
    
        setTitle("Calculator");
        setLocation(300,100);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        display.setBackground(Color.WHITE);
        display.setFont(new Font("monospaced",Font.BOLD,20));
        display.setEditable(false);
        display.setHorizontalAlignment(JTextField.RIGHT);
        getContentPane().add(display,BorderLayout.NORTH);
        JPanel numpad = new JPanel(new GridLayout(4,3));
    
        for(int x = 7; x > 0; x -= 3)
        {
          for(int y = x; y < x+3; y++)
          {
            number[y] = new JButton(""+y);
            number[y].addActionListener(this);
            numpad.add(number[y]);
          }
        }
    
        number[0] = new JButton("0");
        number[0].addActionListener(this);
        numpad.add(number[0]);
        numpad.add(new JLabel(" "));
        number[10] = new JButton(".");
        number[10].addActionListener(this);
        numpad.add(number[10]);
        getContentPane().add(numpad,BorderLayout.CENTER);
        addBtn.addMouseListener(this);
        getContentPane().add(addBtn,BorderLayout.EAST);
        display.setText(df.format(currentTotal));
        pack();
    
      }
    
      public void actionPerformed(ActionEvent ae)
      {
        if(totalDisplayed) display.setText("");
        totalDisplayed = false;
        display.setText(display.getText()+ae.getActionCommand());
      }
    
      public void mouseClicked(MouseEvent me)
      {
        currentTotal += Double.parseDouble(display.getText());
        display.setText(df.format(currentTotal));
        totalDisplayed = true;
      }
    
      public void mousePressed(MouseEvent me){}
      public void mouseReleased(MouseEvent me){}
      public void mouseEntered(MouseEvent me){}
      public void mouseExited(MouseEvent me){}
      public static void main(String[] args){new BasicCalculator().setVisible(true);}
    }

Similar Threads

  1. Java Calculator
    By aapanju in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 05:33 AM
  2. Java calculator decimal
    By cart1443 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:19 PM
  3. calculator not working
    By Renegade85 in forum New To Java
    Replies: 5
    Last Post: 03-10-2008, 03:27 PM
  4. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:02 PM
  5. Create a Calculator in Java
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •