0% found this document useful (0 votes)
124 views

Euro Converter Java

This Java code defines an applet called Euro_Converter that allows the user to convert between the euro, US dollar, and Greek drachma currencies. It uses a grid bag layout to display labels and text fields for each currency. When the "Okay!" button is clicked, it determines which field last had focus and performs the appropriate currency conversion calculations and displays the results.

Uploaded by

D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views

Euro Converter Java

This Java code defines an applet called Euro_Converter that allows the user to convert between the euro, US dollar, and Greek drachma currencies. It uses a grid bag layout to display labels and text fields for each currency. When the "Okay!" button is clicked, it determines which field last had focus and performs the appropriate currency conversion calculations and displays the results.

Uploaded by

D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Euro_Converter.

java
/*<HTML>
*<APPLET code=Euro_Converter.class width=200 height=105>
*<PARAM NAME="euro_dollar" VALUE="1.1">
*<PARAM NAME="euro_drachmas" VALUE="341">
*</APPLET>
*</HTML>
*/

//Convert between dollar, drachmas, euro. It was part of an older exam


//at university ... I have learned from other's code, now I want (and hope
//it is of some help) to offer to others.

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

public class Euro_Converter extends javax.swing.JApplet implements

ActionListener, FocusListener{
public static final long serialVersionUID = 24362462L;
String focus_lost ="";
//take parameter from html code
double euro_dollar = 0.0;
double euro_drachmas = 0.0;//getParameter("euro_dollar")

//DECLARE JCOMPONENTS
JButton ok_bttn = new JButton("Okay!");
//LABELS
JLabel euro_lbl = new JLabel("Euro: ");
JLabel dollar_lbl = new JLabel("Dollars: ");
JLabel drachmas_lbl = new JLabel("Drachmas: ");
//TEXTFIELDS
JTextField euro_txf = new JTextField(10);
JTextField dollar_txf = new JTextField(10);
JTextField drachmas_txf = new JTextField(10);
//LAYOUT
//Create a gridBag layout object
GridBagLayout gb_layout = new GridBagLayout();
//Create an instance of GridBagConstraints
GridBagConstraints gb_cnstr = new GridBagConstraints();

public void init(){

//Get parameters
euro_dollar = handle_double(getParameter("euro_dollar"));
euro_drachmas = handle_double(getParameter("euro_drachmas"));

// GET THE CONTENT PANE


Container content_pane = getContentPane ();

//SET THE LAYOUT


content_pane.setLayout(gb_layout);

//BUILD AND ATACH LAYOUT CONSTRAINTS


//***constraints for labels
//euro
build_constraints(gb_cnstr, 0, 0, 1, 1, 100, 100);
gb_cnstr.anchor = GridBagConstraints.EAST;
gb_layout.setConstraints(euro_lbl, gb_cnstr);

//dollar
build_constraints(gb_cnstr, 0, 1, 1, 1, 100, 100);
gb_layout.setConstraints(dollar_lbl, gb_cnstr);

//drachmas
build_constraints(gb_cnstr, 0, 2, 1, 1, 100, 100);
gb_layout.setConstraints(drachmas_lbl, gb_cnstr);
//***constraints for textfields
//euro
build_constraints(gb_cnstr, 1, 0, 1, 1, 100, 100);
gb_cnstr.anchor = GridBagConstraints.WEST;
gb_layout.setConstraints(euro_txf, gb_cnstr);
//dollar
build_constraints(gb_cnstr, 1, 1, 1, 1, 100, 100);
gb_layout.setConstraints(dollar_txf, gb_cnstr);
//drachmas
build_constraints(gb_cnstr, 1, 2, 1, 1, 100, 100);
gb_layout.setConstraints(drachmas_txf, gb_cnstr);
//***constraints for button
build_constraints(gb_cnstr, 0, 3, 2, 1, 100, 100);
gb_cnstr.anchor = GridBagConstraints.NORTH;
gb_layout.setConstraints(ok_bttn, gb_cnstr);
//ADD THE COMPONENTS TO THE CONTAINER
//Labels
content_pane.add(euro_lbl);
content_pane.add(dollar_lbl);
content_pane.add(drachmas_lbl);
//TextFields
content_pane.add(euro_txf);
content_pane.add(dollar_txf);
content_pane.add(drachmas_txf);
//Button
content_pane.add(ok_bttn);

//ADD ACTION LISTENERS


//ActionListeners
ok_bttn.addActionListener(this);
//FocusListener
/*we need this to know which field was last used then
*we will suppose this is the one from which to convert to other two
*/
euro_txf.addFocusListener(this);
dollar_txf.addFocusListener(this);
drachmas_txf.addFocusListener(this);

} //init ends

//Handle the button click


public void actionPerformed(ActionEvent bttn_evt){
Object source = bttn_evt.getSource();

//Check which field lost focus last


if(focus_lost=="euro"){
convert_from_euro();
}
if(focus_lost=="dollar"){
convert_from_dollar();
}
if(focus_lost=="drachmas"){
convert_from_drachmas();
}

}//method ends

//Handle the TextField focus


public void focusGained(FocusEvent fcs_evt) {//
}//method ends, we don't care about it
public void focusLost(FocusEvent fcs_evt) {
Object source = fcs_evt.getSource();
//Keep track who lost last focus
if(source==euro_txf){
focus_lost="euro";
}if(source==dollar_txf){
focus_lost="dollar";
}if(source==drachmas_txf){
focus_lost="drachmas";
}
}//method ends, we care about this method
///////////////////////////////////////////////////

public void convert_from_euro() {


double amount=0.0;
//take the amount of euro
amount=handle_double(euro_txf.getText());
//Find dollars
dollar_txf.setText(Double.toString(euro_dollar*amount));
//Find drachmas
drachmas_txf.setText(Double.toString(euro_drachmas*amount));
}//method

public void convert_from_dollar(){


double amount=0.0;
//take the amount of euro
amount=handle_double(dollar_txf.getText());
//Find euro
euro_txf.setText(Double.toString(amount/euro_dollar));
//Find drachmas, using the amount of euro though
drachmas_txf.setText
(Double.toString(euro_drachmas*handle_double(euro_txf.getText())));
}//method

public void convert_from_drachmas(){


double amount=0.0;

amount=handle_double(drachmas_txf.getText());
//Find euro
euro_txf.setText(Double.toString(amount/euro_drachmas));
//Find dollar, using the amount of euro though
dollar_txf.setText
(Double.toString(euro_dollar*handle_double(euro_txf.getText())));
}//method
//This method set the constraints for the layout, we call it for every object
void build_constraints(GridBagConstraints gb_cnstr, int gx, int gy,
int gw, int gh, int wx, int wy){
gb_cnstr.gridx = gx;
gb_cnstr.gridy = gy;
gb_cnstr.gridwidth = gw;
gb_cnstr.gridheight =gh;
gb_cnstr.weightx = wx;
gb_cnstr.weighty = wy;
}//method ends

//handle double, this method converts string to double while checks errors
public double handle_double(String db_str)
{ double dbl_val=0.0;
try{
dbl_val=Double.parseDouble(db_str);
}
catch(java.lang.NumberFormatException e){
JOptionPane.showMessageDialog(
null, "Please enter a proper arithmetic value! ",
"Arithmetic Error ...", JOptionPane.ERROR_MESSAGE
);

}
return (dbl_val);
}//method ends

}//Class

You might also like