// MyWindow.java // Class definition // Practice with anonymous inner classes, JFrame, windows, WindowAdapter import java.text.DecimalFormat; import java.awt.event.*; import java.awt.*; import javax.swing.*; public class MyWindow extends JFrame implements ActionListener { DecimalFormat precision2 = new DecimalFormat( "0.00" ); public double length, width; public JLabel lengthLabel, widthLabel, areaLabel; public JTextField lengthField, widthField, areaField; Container c; // constructor public MyWindow( ) { super ( "Anonymous Inner Class Practice" ); c = getContentPane( ); c.setLayout( new FlowLayout( ) ); lengthLabel = new JLabel( "Enter the length:" ); lengthField = new JTextField( 10 ); lengthField.addActionListener( this); c.add( lengthLabel ); c.add( lengthField ); widthLabel = new JLabel( "Enter the width:" ); widthField = new JTextField( 10 ); widthField.addActionListener( this ); c.add( widthLabel ); c.add( widthField ); areaLabel = new JLabel( "Area of the rectangle" ); areaField = new JTextField( 10 ); areaField.setEditable( false ); c.add( areaLabel ); c.add( areaField ); } public void actionPerformed( ActionEvent e ) { length = Double.parseDouble( lengthField.getText( ) ); width = Double.parseDouble( widthField.getText( ) ); areaField.setText( precision2.format( length*width ) ); } public void addALabel( JLabel l ) { c.add( l ); } public void addAField( JTextField tf ) { c.add( tf ); } }