Advertisement
tsounakis

act4

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