import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; class rectangleCalculator extends JFrame{ private JTextField lengthField, widthField, areaField, perimeterField; private JButton calculateButton, exitButton; public static DecimalFormat decimal = new DecimalFormat("0.00"); //It's better to make use of a constructor as a way of "constructing" the necessary elements of the GUI //Get it? "Constructor?" make use of it to better simplify the process of creating GUIs, like the code below! public rectangleCalculator(){ //editing of the GUI; setting of necessary elements setTitle("Area and Perimeter of a Rectangle Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500, 300); setLocationRelativeTo(null); setLayout(new GridLayout(5,2)); //creation of the length label and length text field JLabel lengthLabel = new JLabel("Length: "); lengthField = new JTextField(); lengthField.setBorder(BorderFactory.createLineBorder(Color.BLACK)); //creates a line border for the field //creation of the width label and width text field JLabel widthLabel = new JLabel("Width: "); widthField = new JTextField(); widthField.setBorder(BorderFactory.createLineBorder(Color.BLACK)); //creation of the area label and of the area text field; non-editable JLabel areaLabel = new JLabel("Area: "); areaField = new JTextField(); areaField.setEditable(false); areaField.setBackground(Color.GRAY); //sets the background color of the field areaField.setBorder(BorderFactory.createLineBorder(Color.BLACK)); //creates a line border for the field //creation of the perimeter label and of the perimeter text field; non-editable JLabel perimeterLabel = new JLabel("Perimeter: "); perimeterField = new JTextField(); perimeterField.setEditable(false); perimeterField.setBackground(Color.YELLOW); //sets the background color of the field perimeterField.setBorder(BorderFactory.createLineBorder(Color.BLACK)); //creates a line border for the field //creation of the buttons for calculate and exit calculateButton = new JButton("Calculate"); calculateButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent calculate){ calculateRectangle(); } }); //remember the creation of the actual ActionListener are all inside the parameter itself; observe! exitButton = new JButton("Exit"); exitButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent exit){ System.exit(0); } }); //remember the creation of the actual ActionListener are all inside the parameter itself; observe! //The code below officially adds all the created labels and fields, as well as the buttons //REMEMBER, the order of adding would contribute to the overall placement of the elements add(lengthLabel); add(lengthField); add(widthLabel); add(widthField); add(areaLabel); add(areaField); add(perimeterLabel); add(perimeterField); add(calculateButton); add(exitButton); //the code below enables the GUI to be visible setVisible(true); } public void calculateRectangle(){ double length = Double.parseDouble(lengthField.getText()); double width = Double.parseDouble(widthField.getText()); double area = length * width; double perimeter = 2 * (length + width); areaField.setText(decimal.format(area)); perimeterField.setText(decimal.format(perimeter)); } } public class guiFrontliners{ public static void main(String[] args){ SwingUtilities.invokeLater(new Runnable(){ public void run(){ new rectangleCalculator(); } }); } }