// Converter.java
// Experimenting with JFrame - Morelli text
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class Converter extends JFrame implements ActionListener {
	DecimalFormat precision2 = new DecimalFormat( "0.00" );

	// instance variables - GUI Components
	private JLabel prompt = new JLabel( "Distance in miles: " );
	private JTextField input = new JTextField( 6 );
	private JTextArea display = new JTextArea( 10, 20 );
	private JButton convert = new JButton( "Convert" );

	// constructor
	public Converter( ) {
		super( "Converting miles to kilometers" );
		Container c = getContentPane( );
		c.setLayout( new FlowLayout( ) );
		c.add( prompt );
		c.add( input );
		c.add( convert );

		c.add( display );

		display.setLineWrap( true );
		display.setEditable( false );

		convert.addActionListener( this );
	}  // end Converter( )

	public void actionPerformed( ActionEvent e ) {
		double miles = Double.parseDouble( input.getText( ) );
		double km = milesToKm( miles );
		display.append( miles + " miles equals " + precision2.format( km ) + " kilometers\n" );
	}

	public static void main( String args[ ] ) {
		Converter w = new Converter( );
		w.setSize( 400, 300 );
		w.setVisible( true );
		w.addWindowListener( new WindowAdapter( ) {
			public void windowClosing( WindowEvent e ) {
				System.exit( 0 );
			}
		} );
	}  // end of main( )

	double milesToKm( double m ) {
		return m / 0.62;
	}
}  // end of Converter