Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 4.93 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9. //Julius Medrano
  10. //Lab 08, Calculator
  11.  
  12. import javax.swing.*;
  13. import java.awt.*;
  14. import java.awt.event.*;
  15.  
  16.  
  17. public class Calculator
  18. {
  19.                         ButtonActionListener BAL = new ButtonActionListener();
  20.  
  21.         JTextField input = new JTextField("0", 27);
  22.         boolean newNumber = true;
  23.         JButton one = new JButton("1");
  24.         JButton cButton = new JButton("C");
  25.                 JButton CeButton = new JButton("CE");
  26.                 JButton plusMin = new JButton("+/-");
  27.                 JButton div = new JButton("/");
  28.                 JButton seven = new JButton("7");
  29.                 JButton eight = new JButton("8");
  30.                 JButton nine = new JButton("9");
  31.                 JButton mult = new JButton("*");
  32.                 JButton four = new JButton("4");
  33.                 JButton five = new JButton("5");
  34.                 JButton six = new JButton("6");
  35.                 JButton sub = new JButton("-");
  36.                 JButton two = new JButton("2");
  37.                 JButton three = new JButton("3");
  38.                 JButton plus = new JButton("+");
  39.                 JButton zero = new JButton("0");
  40.                 JButton dot = new JButton(".");
  41.                 JButton equal = new JButton("=");
  42.  
  43.  
  44.        
  45.        
  46.         void addDigit(int i)
  47.         {
  48.                 if(newNumber)
  49.                 {
  50.                         input.setText(""+i);
  51.                         newNumber = false;
  52.                 }
  53.                 else
  54.                 {
  55.                         input.setText(input.getText()+i);
  56.                 }
  57.                
  58.         }
  59.  
  60.  
  61.        
  62.         class ButtonActionListener implements ActionListener
  63.         {
  64.                 public void actionPerformed(ActionEvent e)
  65.                 {
  66.                                 if(e.getSource().equals(one))
  67.                 input.setText("1");
  68.             if(e.getSource().equals(two))
  69.                input.setText("2");
  70.             if(e.getSource().equals(three))
  71.                input.setText("3");
  72.             if(e.getSource().equals(four))
  73.                input.setText("4");
  74.             if(e.getSource().equals(five))
  75.                input.setText("5");
  76.             if(e.getSource().equals(six))
  77.                input.setText("6");
  78.             if(e.getSource().equals(seven))
  79.                input.setText("7");
  80.             if(e.getSource().equals(eight))
  81.                input.setText("8");
  82.             if(e.getSource().equals(nine))
  83.                input.setText("9");
  84.                 if(e.getSource().equals(zero))
  85.                input.setText("0");
  86.                         }
  87.                                
  88.  
  89.         public static void main(String[] args)
  90.         {
  91.                 ButtonActionListener BAL = new ButtonActionListener();
  92.  
  93.                 JFrame frm = new JFrame("Calculator Lab");
  94.                 JPanel buttons = new JPanel();
  95.                 JPanel textLabel = new JPanel();
  96.                 JPanel buttons0 = new JPanel();
  97.                 JPanel buttonsEtc = new JPanel();
  98.                                
  99.                 input.setHorizontalAlignment(JTextField.RIGHT);
  100.                 input.addActionListener(BAL);
  101.                 input.setEditable(false);
  102.                
  103.  
  104.                
  105.                
  106.                 JButton cButton = new JButton("C");
  107.                 JButton CeButton = new JButton("CE");
  108.                 JButton plusMin = new JButton("+/-");
  109.                 JButton div = new JButton("/");
  110.                 JButton seven = new JButton("7");
  111.                 JButton eight = new JButton("8");
  112.                 JButton nine = new JButton("9");
  113.                 JButton mult = new JButton("*");
  114.                 JButton four = new JButton("4");
  115.                 JButton five = new JButton("5");
  116.                 JButton six = new JButton("6");
  117.                 JButton sub = new JButton("-");
  118.                 JButton two = new JButton("2");
  119.                 JButton three = new JButton("3");
  120.                 JButton plus = new JButton("+");
  121.                 JButton zero = new JButton("0");
  122.                 JButton dot = new JButton(".");
  123.                 JButton equal = new JButton("=");
  124.                
  125.                
  126.                
  127.                 double numOne = 0;
  128.                   double numTwo = 0;
  129.        
  130.  
  131.                 textLabel.setLayout(new BorderLayout());
  132.                 textLabel.add(input, BorderLayout.NORTH);
  133.                
  134.  
  135.  
  136.  
  137.                 buttons.setLayout(new GridLayout(4,4));
  138.                 buttons.add(cButton);
  139.                 cButton.addActionListener(BAL);
  140.                 buttons.add(CeButton);
  141.                 CeButton.addActionListener(BAL);
  142.                 buttons.add(plusMin);
  143.                 plusMin.addActionListener(BAL);
  144.                 buttons.add(div);
  145.                 div.addActionListener(BAL);
  146.                 buttons.add(seven);
  147.                 seven.addActionListener(BAL);
  148.                 buttons.add(eight);
  149.                 eight.addActionListener(BAL);
  150.                 buttons.add(nine);
  151.                 nine.addActionListener(BAL);
  152.                 buttons.add(mult);
  153.                 mult.addActionListener(BAL);
  154.                 buttons.add(four);
  155.                 four.addActionListener(BAL);
  156.                 buttons.add(five);
  157.                 five.addActionListener(BAL);
  158.                 buttons.add(six);
  159.                 six.addActionListener(BAL);
  160.                 buttons.add(sub);
  161.                 sub.addActionListener(BAL);
  162.                 buttons.add(one);
  163.                 one.addActionListener(BAL);
  164.                 buttons.add(two);
  165.                 two.addActionListener(BAL);
  166.                 buttons.add(three);
  167.                 three.addActionListener(BAL);
  168.                 buttons.add(plus);
  169.                 plus.addActionListener(BAL);
  170.                
  171.                
  172.                
  173.                
  174.                 buttons0.setLayout(new GridLayout(1,2));
  175.                 buttons0.add(zero);
  176.                 zero.addActionListener(BAL);
  177.                
  178.                 buttonsEtc.setLayout(new GridLayout(1,2));
  179.                 buttonsEtc.add(dot);
  180.                 dot.addActionListener(BAL);
  181.                 buttonsEtc.add(equal);
  182.                 equal.addActionListener(BAL);
  183.                 buttons0.add(buttonsEtc);
  184.                
  185.  
  186.                
  187.  
  188.                
  189.        
  190.                 Container contentPane = frm.getContentPane();
  191.                 contentPane.setLayout(new BorderLayout());
  192.                 contentPane.add(textLabel, BorderLayout.NORTH);
  193.                 contentPane.add(buttons, BorderLayout.CENTER);
  194.                 contentPane.add(buttons0, BorderLayout.SOUTH);
  195.                
  196.                 frm.pack();
  197.                 frm.setSize(300,200);
  198.                 //frm.setResizable(false);
  199.                 frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  200.                 frm.setVisible(true);
  201.         }
  202.        
  203.        
  204.        
  205.        
  206.        
  207.                                        
  208.  
  209.        
  210. }
  211. }