// AreaSimple2Applet.java
// Calculates the area of a rectangle given two integer sides
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AreaSimple2Applet extends JApplet implements ActionListener {
Container c;
JLabel lenPrompt, widPrompt, areaResult;
JTextField length, width, area;
int l, w, a;
String s;
public void init( ) {
l = 0;
w = 0;
a = 0;
s = "";
// create container c to hold the components
c = getContentPane( );
c.setLayout( new FlowLayout( ) );
// create GUI components and add to container
lenPrompt = new JLabel( "Enter the length:" );
c.add( lenPrompt );
length = new JTextField( 10 );
length.addActionListener( this );
c.add( length );
widPrompt = new JLabel( "Enter the width:" );
c.add( widPrompt );
width = new JTextField( 10 );
width.addActionListener( this );
c.add( width );
areaResult = new JLabel( "Area:" );
c.add( areaResult );
area = new JTextField( 40 );
c.add( area );
}
public void actionPerformed( ActionEvent e ) {
// convert inputs to integers
l = Integer.parseInt( length.getText( ) );
w = Integer.parseInt( width.getText( ) );
// calculate area and display in text field
a = l * w;
if ( a < 20 )
s = "The area is a teeny " + a + " square inches.";
else if ( a >= 20 && a <= 50 )
s = "The area is a medium " + a + " square inches.";
else if ( a > 50 )
s = "The area is a huge " + a + " square inches.";
area.setText( s );
}
}