// AreaSimpleApplication.java
// Calculates the area of a rectangle given two integer sides
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AreaSimpleApplication extends JFrame implements ActionListener {
// instance variables
JLabel lenPrompt, widPrompt, areaResult;
JTextField length, width, area;
int l, w, a;
public static void main( String args[ ] ) {
AreaSimpleApplication app = new AreaSimpleApplication( );
app.addWindowListener(
new WindowAdapter( ) {
public void windowClosing( WindowEvent e ) {
System.exit( 0 );
}
}
);
}
public AreaSimpleApplication( ) {
super( "Area Simple as an application" );
Container c = getContentPane( );
c.setLayout( new FlowLayout( ) );
// create GUI components
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( 10 );
c.add( area );
setSize( 135, 200 );
show( );
}
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;
area.setText( Integer.toString( a ) );
}
}