Advertisement
tsounakis

hmm

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