import javax.swing.*; //for the graphics displays such as windows, buttons, etc import java.awt.*; import java.awt.event.*; //for the ActionListener class public class RectangleCalculator { // Objects should be class-level private JLabel lengthL, widthL, areaL, perimeterL; private JTextField lengthTF, widthTF, areaTF, perimeterTF; private JButton calculateB, exitB; CalculateButtonHandler cbHandler; ExitButtonHandler ebHandler; public void driver() { // Create the window JFrame rect = new JFrame("Rectangle Area & Perimeter"); rect.setSize(400, 300); rect.setDefaultCloseOperation(rect.EXIT_ON_CLOSE); // Get the content pane (an object of type Container) Container cp = rect.getContentPane(); cp.setLayout( new GridLayout(5, 2) ); lengthL = new JLabel("Enter the length: ", SwingConstants.RIGHT); widthL = new JLabel("Enter the width: ", SwingConstants.RIGHT); areaL = new JLabel("The area is: ", SwingConstants.RIGHT); perimeterL = new JLabel("Perimeter: ", SwingConstants.RIGHT); lengthTF = new JTextField(10); widthTF = new JTextField(10); areaTF = new JTextField(10); perimeterTF = new JTextField(10); areaTF.setEditable(false); perimeterTF.setEditable(false); calculateB = new JButton("Calculate"); exitB = new JButton("Exit"); // Add our components to the content pane container cp.add(lengthL); cp.add(lengthTF); cp.add(widthL); cp.add(widthTF); cp.add(areaL); cp.add(areaTF); cp.add(perimeterL); cp.add(perimeterTF); cp.add(calculateB); cp.add(exitB); rect.setVisible(true); // Instantiate listeners cbHandler = new CalculateButtonHandler(); ebHandler = new ExitButtonHandler(); // Register the listeners with their respective components calculateB.addActionListener(cbHandler); exitB.addActionListener(ebHandler); } // In order to run our program, we need to instantiate this class // and then invoke the driver() method public static void main(String[] args) { RectangleCalculator r = new RectangleCalculator(); r.driver(); } //This class implements the ActionListener interface //It is created to create a listener (aka "handler") for our calculateB button private class CalculateButtonHandler implements ActionListener { // Implementing an interface rquires writing a body for ALL of the methods in that interface. // In the case of ActionListener, there is only one method, so that is all we have to implement. // The actionPerformed() method will be automatically invoked whenever an event is generated // by the component. (Of course, we have to remember to first register the listener with the component). public void actionPerformed(ActionEvent e) { double width, length, area, perimeter; length = Double.parseDouble(lengthTF.getText() ); width = Double.parseDouble(widthTF.getText() ); area = length * width; perimeter = (length+width) * 2; areaTF.setText("" + area); // the setText() method changes the display of a text field perimeterTF.setText("" + perimeter); } //end method actionPerformed } //end class CalculateButtonHandler //This class implements the ActionListener interface //It is created to create a listener (aka "handler") for our exitB button private class ExitButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } //end method actionPerformed } //end class ExitButtonHandler } //end class RectangleCalculator