/*
* @(#)Currencyconverter.java 1.0 02/02/15
*
* 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.currencyconverter;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Currencyconverter extends JFrame implements ActionListener {
private JTextField amount;
private JTextField rate;
private JTextField result;
private JLabel resultLabel, amountLabel, rateLabel;
private JButton dum;
private JButton cal;
Container c = getContentPane();
public Currencyconverter()
{
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
Container c = getContentPane();
c.setLayout(new GridLayout(4,2));
amountLabel = new JLabel("Enter Amount to Exchange: ");
c.add(amountLabel);
amount = new JTextField(10);
c.add(amount);
rateLabel = new JLabel("Enter the rate of Exchange: ");
c.add(rateLabel);
rate = new JTextField(10);
c.add(rate);
resultLabel = new JLabel("Result: ");
c.add(resultLabel);
result = new JTextField(10);
c.add(result);
dum = new JButton("Exit");
c.add(dum);
cal = new JButton("Calculate");
cal.addActionListener(this);
c.add(cal);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == cal)
{
result.setText("");
try{
//double number1 = Double.parseDouble(amount.getText());
double number1 = errorMessage(amount.getText());
//double number2 = Double.parseDouble(rate.getText());
double number2 = errorMessage(rate.getText());
double res = number1/number2;
result.setText(String.valueOf(res));
}
catch(NumberFormatException n)
{
JOptionPane.showMessageDialog(this, "Only numerics please", "Invalid Number Format",JOptionPane.ERROR_MESSAGE);
}
catch (ArithmeticException d)
{
JOptionPane.showMessageDialog(this, "Don't use zeroes: ", "One of the two numbers is Zero",JOptionPane.ERROR_MESSAGE);
}
catch (myException m)
{
JOptionPane.showMessageDialog(this, "Numbers under 5 only please ", "One of the two numbers is Zero",JOptionPane.ERROR_MESSAGE);
}
amount.setText("");
rate.setText("");
}
if(ae.getSource() == dum)
{
}
}
public static void main(String args[]) {
System.out.println("Starting Currencyconverter...");
Currencyconverter mainFrame = new Currencyconverter();
mainFrame.setSize(350, 140);
mainFrame.setTitle("Currencyconverter");
mainFrame.setVisible(true);
}
public double errorMessage(String s) throws myException
{
double n = Double.parseDouble(s);
if (n == 0.0d)throw new ArithmeticException("Divide by Zero");
if (n>5.0) throw new myException();
return n;
//double number1 = Double.parseDouble(amount.getText());
}
class myException extends Exception
{
public myException()
{}
}
}
|