import java.awt.*; import java.awt.event.*; class Calculator2 { public static void main(String[] args) { SimpleCalculator2 calculator = new SimpleCalculator2(); } // main } // Calculator2 // --- cut here into separate files --- class SimpleCalculator2 extends Frame { private Panel[] p = new Panel[2]; private Button[] buttons = new Button[5]; private TextField opAField = new TextField(10); private TextField opBField = new TextField(10); private TextField resultField = new TextField(10); public SimpleCalculator2() { setLayout(new GridLayout(2,1)); // override standard layout manager (-> BorderLayout) p[0] = new Panel(new FlowLayout(FlowLayout.CENTER, 10,15)); // operands/result fields p[1] = new Panel(new BorderLayout()); // operation buttons p[0].add(new Label("operand A")); // preparing upper row p[0].add(opAField); p[0].add(new Label("operand b")); p[0].add(opBField); p[0].add(new Label("result")); p[0].add(resultField); buttons[0] = new Button("add"); // preparing lower row buttons[1] = new Button("sub"); // operation buttons, arranged as star buttons[2] = new Button("mul"); buttons[3] = new Button("int div"); buttons[4] = new Button("real div"); buttons[0].addActionListener(new AddHoerer()); buttons[1].addActionListener(new SubHoerer()); buttons[2].addActionListener(new MulHoerer()); buttons[3].addActionListener(new IntDivHoerer()); buttons[4].addActionListener(new RealDivHoerer()); p[1].add("North", buttons[0]); p[1].add("East", buttons[1]); p[1].add("South", buttons[2]); p[1].add("West", buttons[3]); p[1].add("Center", buttons[4]); for(int i=0;i<2;i++) { // assigning the panels to the frame add(p[i]); } addWindowListener(new WindowAdapter() { // register for window closing public void windowClosing(WindowEvent e) { System.exit(0); } }); setTitle(" My Simple Calculator, V2"); setSize(300,200); setVisible(true); // show(); deprecated } // SimpleCalculator() class AddHoerer implements ActionListener { public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(opAField.getText()); int b = Integer.parseInt(opBField.getText()); resultField.setText((a+b)+" "); } } // AddHoerer class SubHoerer implements ActionListener { public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(opAField.getText()); int b = Integer.parseInt(opBField.getText()); resultField.setText((a-b)+" "); } } // SubHoerer class MulHoerer implements ActionListener { public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(opAField.getText()); int b = Integer.parseInt(opBField.getText()); resultField.setText((a*b)+" "); } } // MulHoerer class IntDivHoerer implements ActionListener { public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(opAField.getText()); int b = Integer.parseInt(opBField.getText()); resultField.setText((a/b)+" "); } } // IntDivHoerer class RealDivHoerer implements ActionListener { public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(opAField.getText()); double b = Integer.parseInt(opBField.getText()); resultField.setText((a/b)+" "); } } // RealDivHoerer } // class SimpleCalculator2