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

Week1

public class Week1 extends JFrame implements ActionListener

Fields Summary
private JTextField
amount
private JTextField
rate
private JTextField
result
Constructors Summary
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);
		
	
Methods Summary
public voidactionPerformed(java.awt.event.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 voidmain(java.lang.String[] args)

		System.out.println("Starting Week1...");
		Week1 mainFrame = new Week1();
		mainFrame.setSize(400, 100);
		mainFrame.setTitle("Week1");
		mainFrame.setVisible(true);