Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- public class CalculatorGuiAct4 extends Frame
- {
- public static Operand op;
- public static Adder add;
- public static Subtracter sub;
- public static Multiplier mul;
- public static Divider div;
- public static ResultPresenter rp;
- public Button button0, button1, button2, button3, button4;
- public Button button5, button6, button7, button8, button9;
- public Button buttonPlus, buttonMinus, buttonResultPresenter, buttonTimes, buttonDivide;
- public Button buttonEnter, buttonBackSpace, buttonClear,buttonClearAll;
- public static Frame window;
- public static TextField display;
- private static int width, height, x, y, i;
- public CalculatorGuiAct4(Operand op2, Adder add2, Subtracter sub2, Multiplier mult, Divider div2, ResultPresenter rp2)
- {
- int width = 35;
- int height = 28;
- int y = 265;
- int i = 0;
- CalculatorGuiAct4.op = op2;
- CalculatorGuiAct4.add = add2;
- CalculatorGuiAct4.sub = sub2;
- CalculatorGuiAct4.rp = rp2;
- Map <String, Button> buttons = new LinkedHashMap<String, Button>();
- buttons.put("7", button7);
- buttons.put("8", button8);
- buttons.put("9", button9);
- buttons.put("*", buttonTimes);
- buttons.put("4", button4);
- buttons.put("5", button5);
- buttons.put("6", button6);
- buttons.put("+", buttonPlus);
- buttons.put("1", button1);
- buttons.put("2", button2);
- buttons.put("3", button3);
- buttons.put("/", buttonDivide);
- buttons.put("0", button0);
- buttons.put("C", buttonClearAll);
- buttons.put("CE", buttonClear);
- buttons.put("-", buttonMinus);
- buttons.put("ENTER", buttonBackSpace);
- buttons.put("BackSpace", buttonBackSpace);
- buttons.put("=", buttonResultPresenter);
- window = new Frame("CALC Activity 4");
- window.setLayout(null);
- window.setFont(new Font("TimesRoman", Font.PLAIN, 14));
- window.setBackground(Color.red);
- for(Map.Entry<String, Button> entry: buttons.entrySet())
- {
- String key = entry.getKey();
- Button btn = entry.getValue();
- x = i == 3 ? 195 : i == 2 ? 144 : i == 1 ? 104 : 64;
- y = i == 0 ? y-35 : y;
- btn = new Button(key);
- btn.setBounds(x, y, width, height);
- btn.setFont(new Font("TimesRoman", Font.PLAIN, 14));
- btn.setForeground(Color.blue);
- btn.addActionListener(new ButtonHandler(key));
- CalculatorGuiAct4.window.add(btn);
- System.out.println(key + String.valueOf(i));
- i = i == 3 ? 0 : i + 1;
- }
- //D I S P L A Y S E T T I N G S
- display = new TextField("0");
- display.setEditable(false);
- display.setBounds(13, 55, 257, 30);
- //W I N D O W S E T T I N G S
- window.add(display);
- window.setSize(283,320);
- window.setLocation(40,80);
- //window.show();
- window.setVisible(true);
- window.setResizable(false);
- window.addWindowListener(new CloseWindowAndExit());
- }
- }
- class CloseWindowAndExit extends WindowAdapter
- {
- public void windowClosing(WindowEvent closeWindowAndExit)
- {
- System.exit(0);
- }
- }
- class ButtonHandler implements ActionListener
- {
- private String key;
- public ButtonHandler(String Key)
- {
- this.key = Key;
- }
- public void actionPerformed(ActionEvent pushingButton)
- {
- switch (this.key){
- case "ENTER":
- CalculatorGuiAct4.op.complete();
- break;
- case "BackSpace":
- CalculatorGuiAct4.op.deleteLastDigit();
- break;
- case "ClearAll":
- CalculatorGuiAct4.op.reset();
- Calc.st.removeAllElements();
- break;
- case "Clear":
- CalculatorGuiAct4.op.reset();
- break;
- case "+":
- CalculatorGuiAct4.add.operate();
- break;
- case "-":
- CalculatorGuiAct4.sub.operate();
- break;
- case "*":
- CalculatorGuiAct4.mul.operate();
- break;
- case "/":
- CalculatorGuiAct4.div.operate();
- break;
- default:
- CalculatorGuiAct4.op.addDigit(this.key.charAt(0));
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement