//Calc.java //Simple Calc game //User enters in two numbers //computer calculates results import java.awt.*; import java.applet.*; import java.awt.event.*; public class Calc extends Applet implements ActionListener{ //input elements TextField txtX = new TextField("0"); TextField txtY = new TextField("0"); //output elements Label lblPlus = new Label("0"); Label lblMinus = new Label("0"); Label lblTimes = new Label("0"); Label lblDiv = new Label("Undefined"); //button Button btnCalc = new Button("Calculate"); public void init(){ //set main layout setLayout(new BorderLayout()); Panel pnlCalc = new Panel(); pnlCalc.setLayout(new GridLayout(3,4)); pnlCalc.setFont(new Font("SansSerif", Font.BOLD, 20)); add(pnlCalc, BorderLayout.CENTER); Panel pnlButton = new Panel(); pnlButton.setLayout(new FlowLayout()); pnlButton.add(btnCalc); add(pnlButton, BorderLayout.SOUTH); btnCalc.addActionListener(this); //first row pnlCalc.add(new Label("X")); pnlCalc.add(txtX); pnlCalc.add(new Label("Y")); pnlCalc.add(txtY); //second row pnlCalc.add(new Label("X + Y")); pnlCalc.add(lblPlus); pnlCalc.add(new Label("X - Y")); pnlCalc.add(lblMinus); //third row pnlCalc.add(new Label("X * Y")); pnlCalc.add(lblTimes); pnlCalc.add(new Label("X / Y")); pnlCalc.add(lblDiv); //set colors lblPlus.setBackground(Color.gray); lblMinus.setBackground(Color.gray); lblTimes.setBackground(Color.gray); lblDiv.setBackground(Color.gray); } // end init public void actionPerformed(ActionEvent e){ //get x and y values Double xWrap = new Double(txtX.getText()); double x = xWrap.doubleValue(); Double yWrap = new Double(txtY.getText()); double y = yWrap.doubleValue(); String response = ""; //plus response = String.valueOf(x + y); lblPlus.setText(response); //minus response = String.valueOf(x - y); lblMinus.setText(response); //times response = String.valueOf(x * y); lblTimes.setText(response); //divide response = String.valueOf(x / y); lblDiv.setText(response); } // end actionPerformed } // end class def