Advertisement
boxglue

Java Calc (2of2)

Jul 17th, 2012
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.23 KB | None | 0 0
  1. //package calculator;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5. import javax.swing.BorderFactory;
  6. import javax.swing.border.*;
  7. import java.awt.event.*;
  8.  
  9. //Not public. Only accessible from within this folder.
  10. class CalcGUI extends JFrame {
  11.  
  12.     //class variables and constants
  13.     //use the same font for everything
  14.     static Font displayFont = new Font("Arial", Font.PLAIN, 18);
  15.    
  16.     //instance objects inside GUI
  17.     JButton bClr,bPlus,bMinus,bTimes,bDivide,bEquals,bSqr;
  18.     JButton[] bDigit = new MyButton[10];
  19.     JButton bp,bs;  //bp=button point, bs = button sign
  20.     private JTextField display;
  21.     MyActionListener myAL = new MyActionListener();
  22.     MyKeyListener myKL = new MyKeyListener();
  23.    
  24.     // for testing only
  25.     // public static void main(String args[]) {
  26.         // new CalcGUI();
  27.     // }
  28.  
  29.  
  30.     CalcGUI() {             //constructor
  31.         //set up everything to do with the JFrame here
  32.         this.setSize(310,300);
  33.         this.setTitle("Simple Calculator");
  34.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35.         this.setDefaultLookAndFeelDecorated(true);
  36.         this.setResizable(false);
  37.  
  38.         //use a contentPane in case I want to change the background
  39.         Container content = this.getContentPane( );
  40.         content.setLayout(new BorderLayout( ));
  41.        
  42.  
  43.         //Make panels and components
  44.         JPanel panelTop = new JPanel(); //defaults to flowlayout
  45.         panelTop.setLayout(new FlowLayout(FlowLayout.CENTER,12,0)); //add some horizontal spacing
  46.         panelTop.setBackground(new Color(249,249,179));
  47.         display = new JTextField("0.0",16);
  48.         display.setHorizontalAlignment(JTextField.RIGHT);
  49.         display.setFont(displayFont);
  50.         display.addKeyListener(myKL);
  51.         panelTop.add(display);
  52.         JButton bClr = new JButton("CE");   //this button is made separately. It needs smaller font.
  53.         bClr.addActionListener(myAL);
  54.         panelTop.add(bClr);
  55.         //use borders for spacing.
  56.         Border border0 = BorderFactory.createEmptyBorder(5,0,5,0);
  57.         panelTop.setBorder(border0);
  58.         content.add(panelTop, BorderLayout.NORTH);
  59.  
  60.         //set up number panel
  61.         JPanel panelNum = new JPanel();
  62.         panelNum.setLayout(new GridLayout(4,3,3,3));
  63.         panelNum.setBackground(new Color(62,140,227));
  64.         Border border = BorderFactory.createMatteBorder(5, 5, 2, 2, new Color(62,140,227));
  65.         panelNum.setBorder(border);
  66.        
  67.         //create the digit buttons using an array.
  68.         //The name of the buttons doesn't matter.
  69.         //I'm basing my calcMain on the fact that each button will have a different first letter.
  70.         for(int i=0;i<10;i++)
  71.             bDigit[i] = new MyButton("" + i);
  72.         panelNum.add(bDigit[7]);
  73.         panelNum.add(bDigit[8]);
  74.         panelNum.add(bDigit[9]);
  75.         panelNum.add(bDigit[4]);
  76.         panelNum.add(bDigit[5]);
  77.         panelNum.add(bDigit[6]);
  78.         panelNum.add(bDigit[1]);
  79.         panelNum.add(bDigit[2]);
  80.         panelNum.add(bDigit[3]);
  81.         panelNum.add(bDigit[0]);
  82.         JButton bp = new MyButton(".");
  83.         JButton bs = new MyButton("\u00B1");    //Unicode +- character.
  84.         panelNum.add(bp);
  85.         panelNum.add(bs);
  86.         content.add(panelNum, BorderLayout.CENTER);
  87.  
  88.         JPanel panelRight = new JPanel();
  89.         panelRight.setLayout(new GridLayout(6,1,3,3));
  90.         panelRight.setBackground(new Color(227,140,227));
  91.         Border border2 = BorderFactory.createMatteBorder(9, 9, 7, 9, new Color(235,160,235));
  92.         panelRight.setBorder(border2);
  93.        
  94.         JButton bSqr = new MyButton("\u221A");
  95.         JButton bPlus = new MyButton("+");
  96.         JButton bMinus = new MyButton("-");
  97.         JButton bTimes = new MyButton("x");
  98.         JButton bDivide = new MyButton("\u00F7");
  99.         JButton bEquals = new MyButton("=");
  100.         panelRight.add(bSqr);
  101.         panelRight.add(bPlus);
  102.         panelRight.add(bMinus);
  103.         panelRight.add(bTimes);
  104.         panelRight.add(bDivide);
  105.         panelRight.add(bEquals);
  106.                
  107.         content.add(panelRight, BorderLayout.EAST);
  108.         this.validate();
  109.         this.setVisible(true);
  110.     }
  111.    
  112.     //these two functions are called from CalcMain and are used to set the display.
  113.     void setDisplay(String s){
  114.         display.setText(s);
  115.     }
  116.    
  117.     void setDisplay(Double d){
  118.         display.setText(d.toString());
  119.     }
  120.    
  121.     private class MyButton extends JButton{
  122.         MyButton(String s) {
  123.             super(s);       //create the JButton just like normal
  124.             char c = (char)s.charAt(0);
  125.             if (c >= '0' && c <= '9') this.setBackground(new Color(230,240,255));
  126.             this.setFont(displayFont);
  127.             this.addActionListener(myAL);   //all buttons can use the same action listener.
  128.         }
  129.     }
  130.    
  131.     private class MyActionListener implements ActionListener {
  132.         public void actionPerformed( ActionEvent e ) {
  133.             String txt = e.getActionCommand();         
  134.             CalcMain.sendButton(txt);   //Here's the important part.
  135.         }
  136.     }
  137.    
  138.     private class MyKeyListener implements KeyListener {
  139.         public void keyTyped( KeyEvent e ) {
  140.             String txt ="";
  141.             // Keyboard code for the pressed key.
  142.             // int key = e.getKeyCode();
  143.             char key = e.getKeyChar();
  144.             switch (key) {         
  145.                 case '\n':  //Enter = '='
  146.                     txt="=";
  147.                     break;
  148.                 case '\b':  //backspace = CE
  149.                     txt="C";
  150.                     break;             
  151.                 case '/':
  152.                     txt="\u00F7";
  153.                     break;
  154.                 case '*':
  155.                     txt="x";
  156.                     break;
  157.                 case 'q':
  158.                     txt="\u221A";
  159.                     break;
  160.                 default:
  161.                     txt= Character.toString(key);
  162.             }
  163.             e.consume();    //after it updates the display, the keypressed also is displayed. Must consume it!!!
  164.  
  165.             CalcMain.sendButton(txt);  
  166.            
  167.         }
  168.         public void keyReleased( KeyEvent e ) {}
  169.         public void keyPressed( KeyEvent e ) {}
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement