FileDocCategorySizeDatePackage
Week1.javaAPI DocExample2428Thu Feb 14 13:58:52 GMT 2002myprojects.week1

Week1.java

/*
 * @(#)Week1.java 1.0 02/02/03
 *
 * You can modify the template of this file in the
 * directory ..\JCreator\Templates\Template_1\Project_Name.java
 *
 * You can also create your own project template by making a new
 * folder in the directory ..\JCreator\Template\. Use the other
 * templates as examples.
 *
 */
package myprojects.week1;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


class Week1 extends JFrame implements ActionListener{
	private JTextField amount = new JTextField();
	private JTextField rate   = new JTextField();
	private JTextField result = new JTextField();
	
	public Week1() {
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
				System.exit(0);
			}
		});
		
		Container container = getContentPane();
		container.setLayout(new GridLayout(3,2));
		container.add(new JLabel("Enter amount to exchange"));
		container.add(amount);
		container.add(new JLabel("Enter exchange rate and hit enter"));
		container.add(rate);
		// Add an action listener to the rate text field
		rate.addActionListener(this);
		container.add(new JLabel("Result"));
		container.add(result);
		
	}
	
	public void actionPerformed(ActionEvent e) {
		//  Clear the output field
		result.setText("");
		// The try block, which will convert the Strings in the text fields into
		// double's.  If anything has been inputted incorrectly, then an exception
		// will be raised.
		try {
			double number1=Double.parseDouble(amount.getText());
			double number2=Double.parseDouble(rate.getText());
			double res = number1*number2;
			if(number1 == 0.0d || number2 == 0.0d) throw new ArithmeticException("Divide by zero");
			result.setText(String.valueOf(res));
		}
		catch (NumberFormatException n) {
			JOptionPane.showMessageDialog(this,
			"You must enter two numbers",
			"Invalid number format",JOptionPane.ERROR_MESSAGE);
		}
		catch (ArithmeticException d) {
			System.out.println("ArithmeticException "+d);
			JOptionPane.showMessageDialog(this,
			"You must enter two numbers",
			"One of the two numbers is zero",JOptionPane.ERROR_MESSAGE);
		}
			
		
	}

	public static void main(String args[]) {
		System.out.println("Starting Week1...");
		Week1 mainFrame = new Week1();
		mainFrame.setSize(400, 100);
		mainFrame.setTitle("Week1");
		mainFrame.setVisible(true);
	}
}