import java.awt.*; import java.awt.Color.*; import java.awt.TextField.*; import java.awt.event.*; public class Calculator extends WindowAdapter { //declaraion of variable types Frame f; TextField tf; Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16; Panel west, center; public void makeCALC() { f = new Frame("Calculate it"); tf = new TextField("0.0"); //assigning values to buttons b1 = new Button("7"); b2 = new Button("8"); b3 = new Button("9"); b4 = new Button("/"); b5 = new Button("4"); b6 = new Button("5"); b7 = new Button("6"); b8 = new Button("*"); b9 = new Button("1"); b10 = new Button("2"); b11 = new Button("3"); b12 = new Button("-"); b13 = new Button("0"); b14 = new Button("="); b15 = new Button("."); b16 = new Button("+"); //setting up alignment and layout of grid west = new Panel(); center = new Panel(); center.setLayout(new GridLayout(4,4)); f.add(tf,BorderLayout.NORTH); f.add(west,BorderLayout.WEST); f.add(center,BorderLayout.CENTER); center.add(b1); center.add(b2); center.add(b3); center.add(b4); center.add(b5); center.add(b6); center.add(b7); center.add(b8); center.add(b9); center.add(b10); center.add(b11); center.add(b12); center.add(b13); center.add(b14); center.add(b15); center.add(b16); f.setSize(500,500); f.setVisible(true); //formating of font and color of numbers Font font = new Font("Verdana", Font.BOLD,40); f.setFont(font); b1.setForeground(Color.BLUE); b2.setForeground(Color.YELLOW); b3.setForeground(Color.DARK_GRAY); b4.setForeground(Color.RED); b5.setForeground(Color.GREEN); b6.setForeground(Color.CYAN); b7.setForeground(Color.LIGHT_GRAY); b8.setForeground(Color.ORANGE); b9.setForeground(Color.RED); b10.setForeground(Color.PINK); b11.setForeground(Color.GRAY); b12.setForeground(Color.MAGENTA); b13.setForeground(Color.WHITE); b14.setForeground(Color.YELLOW); b15.setForeground(Color.ORANGE); b16.setForeground(Color.PINK); //method caller for buttons and frame f.addWindowListener(this); b1.addActionListener(new MyButtonHandler()); b2.addActionListener(new MyButtonHandler()); b3.addActionListener(new MyButtonHandler()); b4.addActionListener(new MyButtonHandler()); b5.addActionListener(new MyButtonHandler()); b6.addActionListener(new MyButtonHandler()); b7.addActionListener(new MyButtonHandler()); b8.addActionListener(new MyButtonHandler()); b9.addActionListener(new MyButtonHandler()); b10.addActionListener(new MyButtonHandler()); b11.addActionListener(new MyButtonHandler()); b12.addActionListener(new MyButtonHandler()); b13.addActionListener(new MyButtonHandler()); b14.addActionListener(new MyButtonHandler()); b15.addActionListener(new MyButtonHandler()); b16.addActionListener(new MyButtonHandler()); } //calling method to allow window to close public void windowClosing(WindowEvent we) { System.exit(0); } public class MyButtonHandler implements ActionListener { public void actionPerformed(ActionEvent ae) { //method to test values of label stored in button String label = ae.getActionCommand(); if(label == "7") { tf.setText("7"); } else if(label == "8") { tf.setText("8"); } if(label == "9") { tf.setText("9"); } else if(label == "/") { tf.setText("/"); } if(label == "4") { tf.setText("4"); } else if(label == "5") { tf.setText("5"); } if(label == "6") { tf.setText("6"); } else if(label == "*") { tf.setText("*"); } else if(label == "1") { tf.setText("1"); } else if(label == "2") { tf.setText("2"); } else if(label == "3") { tf.setText("3"); } else if(label == "-") { tf.setText("-"); } else if(label == "0") { tf.setText("0"); } else if(label == "=") { tf.setText("="); } else if(label == ".") { tf.setText("."); } else if(label == "+") { tf.setText("+"); } else if(label == "Clear") { tf.setText(" "); } } }//end makeGUI() public static void main(String args[]) { Calculator theNew = new Calculator(); theNew.makeCALC(); }