// JFrameDemo // Demos JFrame // Calculates area of a rectangle import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JFrameDemo extends JFrame { private JLabel lengthLabel, widthLabel, areaLabel; private JTextField lengthField, widthField, areaField; private JButton calcButton, exitButton; private int l, w; public JFrameDemo( ) { super( "JFrame Demo Application" ); Container c = getContentPane( ); c.setLayout( new FlowLayout( ) ); lengthLabel = new JLabel( "Enter length as an integer" ); c.add( lengthLabel ); lengthField = new JTextField( 10 ); c.add( lengthField ); widthLabel = new JLabel( "Enter width as an integer" ); c.add( widthLabel ); widthField = new JTextField( 10 ); c.add( widthField ); areaLabel = new JLabel( "AREA" ); c.add( areaLabel ); areaField = new JTextField( 10 ); c.add( areaField ); calcButton = new JButton( "Calculate" ); calcButton.addActionListener( new ActionListener( ) { public void actionPerformed( ActionEvent e ) { l = Integer.parseInt( lengthField.getText( ) ); w = Integer.parseInt( widthField.getText( ) ); areaField.setText( l * w + " square units" ); } } ); c.add( calcButton ); exitButton = new JButton( "Quit" ); exitButton.addActionListener( new ActionListener( ) { public void actionPerformed( ActionEvent e ) { System.exit( 0 ); } } ); c.add( exitButton ); } public static void main( String args[ ] ) { JFrameDemo areaWindow = new JFrameDemo( ); areaWindow.setSize( 155, 250 ); areaWindow.show( ); areaWindow.addWindowListener( new WindowAdapter( ) { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } }