Advertisement
tsounakis

hmmm

Nov 27th, 2020
971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.86 KB | None | 0 0
  1. package gui;
  2.  
  3. /**
  4.  * CalculatorGui set of classes developed for the Activity 4*
  5.  * @author: Kleanthis Thramboulidis
  6.  * @revison: Thanasis Tsounakis
  7.  * date: 6/11/19 new date: 27/11/2020
  8.  */
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import java.util.*;
  12.  
  13. import engine.*;
  14.  
  15.  
  16. public class CalculatorGuiAct4 extends Frame
  17. {
  18.     public static  Operand op;
  19.     public static Adder add;
  20.     public static Subtractor sub;
  21.     public static Multiplier mul;
  22.     public static Divider div;
  23.     public static ResultPresenter rp;    
  24.     public Button button0, button1, button2, button3, button4;
  25.     public Button button5, button6, button7, button8, button9;
  26.     public Button buttonPlus, buttonMinus, buttonResultPresenter;
  27.     public Button buttonEnter, buttonBackSpace, buttonClear,buttonClearAll;
  28.     public static Frame window;
  29.     public static TextField display;
  30.  
  31.  
  32.     public CalculatorGuiAct4(Operand op2, Adder add2, Subtractor sub2, Multiplier mult, Divider div2, ResultPresenter rp2)
  33.         {
  34.             CalculatorGuiAct4.op = op2;
  35.             CalculatorGuiAct4.add = add2;
  36.             CalculatorGuiAct4.sub = sub2;
  37.             CalculatorGuiAct4.rp = rp2;
  38.  
  39.             Map <String, Button> buttons = new HashMap<String, Button>;
  40.  
  41.             buttons.put("0", button0);
  42.             buttons.put("1", button1);
  43.             buttons.put("2", button2);
  44.             buttons.put("3", button3);
  45.             buttons.put("4", button4);
  46.             buttons.put("5", button5);
  47.             buttons.put("6", button6);
  48.             buttons.put("7", button7);
  49.             buttons.put("8", button8);
  50.             buttons.put("9", button9);
  51.             buttons.put("C", buttonClearAll);
  52.             buttons.put("CE", buttonClear);
  53.             buttons.put("BackSpace", buttonBackSpace);
  54.             buttons.put("ENTER", buttonBackSpace);
  55.             buttons.put("+", buttonPlus);
  56.             buttons.put("-", buttonMinus);
  57.             buttons.put("*", buttonTimes);
  58.             buttons.put("/", buttonDivide);
  59.             buttons.put("=", buttonResultPresenter);
  60.  
  61.             window = new Frame("CALC Activity 4");
  62.  
  63.             window.setLayout(null);
  64.             window.setFont(new Font("TimesRoman", Font.PLAIN, 14));
  65.             window.setBackground(Color.blue);
  66.  
  67.             for(Map.Entry<String, Button> entry: objects.entrySet())
  68.             {
  69.                 String key = entry.getKey();
  70.                 Button btn = entry.getValue();
  71.                 btn = new Button(key);      
  72.                 btn.setBounds(195, 265, 35, 28);
  73.                 btn.setFont(new Font("TimesRoman", Font.PLAIN, 14));
  74.                 btn.setForeground(Color.blue);    
  75.                 btn.addActionListener(new ButtonHandler(key));
  76.                 CalculatorGuiAct4.window.add(btn);
  77.             }
  78.  
  79.             //D  I  S  P  L  A  Y     S  E  T  T  I  N  G  S
  80.             display = new TextField("0");
  81.             display.setEditable(false);  
  82.             display.setBounds(13, 55, 257, 30);
  83.  
  84.             //W  I  N  D  O  W     S  E  T  T  I  N  G  S
  85.             window.add(display);        
  86.             window.setSize(283,320);
  87.             window.setLocation(40,80);      
  88.             //window.show();      
  89.             window.setVisible(true);
  90.             window.setResizable(false);  
  91.             window.addWindowListener(new CloseWindowAndExit());  
  92.         }
  93.  
  94.     }              
  95.  
  96.     class CloseWindowAndExit extends WindowAdapter
  97.         {
  98.             public void windowClosing(WindowEvent closeWindowAndExit)
  99.         {
  100.             System.exit(0);
  101.         }
  102. }
  103.  
  104. class ButtonHandler implements ActionListener
  105.   {    
  106.      public ButtonHandler(string Key)
  107.     {
  108.         this.key = Key;
  109.     }
  110.  
  111.      public void actionPerformed(ActionEvent pushingButton)
  112.         {
  113.             switch this.key{
  114.                 case "ENTER":
  115.                     CalculatorGuiAct4.op.complete();
  116.                     break;
  117.                 case "BackSpace":
  118.                     CalculatorGuiAct4.op.deleteLastDigit();
  119.                     break;
  120.                 case "ClearAll":
  121.                     CalculatorGuiAct4.op.reset();
  122.                     Calc.st.removeAllElements();
  123.                     break;
  124.                 case "Clear":
  125.                     CalculatorGuiAct4.op.reset();
  126.                     break;
  127.                 case "+":
  128.                     CalculatorGuiAct4.add.operate();
  129.                     break;
  130.                 case "-":
  131.                     CalculatorGuiAct4.sub.operate();
  132.                     break;
  133.                 case "*":
  134.                     CalculatorGuiAct4.mul.operate();
  135.                     break;
  136.                 case "/":
  137.                     CalculatorGuiAct4.div.operate();
  138.                     break;
  139.                 default:
  140.                     CalculatorGuiAct4.op.addDigit(this.key);
  141.                     break;
  142.             }
  143.         }
  144.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement