0% found this document useful (0 votes)
26 views5 pages

Euro Converter Java Applet Code

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)
26 views5 pages

Euro Converter Java Applet Code

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

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 [Link].*;
import [Link].*;
import [Link].*;

public class Euro_Converter extends [Link] 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 = [Link];
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 = [Link];
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 = [Link];
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([Link](euro_dollar*amount));
//Find drachmas
drachmas_txf.setText([Link](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([Link](amount/euro_dollar));
//Find drachmas, using the amount of euro though
drachmas_txf.setText
([Link](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([Link](amount/euro_drachmas));
//Find dollar, using the amount of euro though
dollar_txf.setText
([Link](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=[Link](db_str);
}
catch([Link] e){
[Link](
null, "Please enter a proper arithmetic value! ",
"Arithmetic Error ...", JOptionPane.ERROR_MESSAGE
);

}
return (dbl_val);
}//method ends

}//Class

Common questions

Powered by AI

In Euro_Converter, layout constraints are handled by the build_constraints method, which sets various GridBagConstraints properties for each GUI component, such as gridx, gridy, gridwidth, and others. This method provides a detailed and customizable arrangement of components, but it can be complex and difficult to manage for developers not familiar with GridBagLayout. Potential limitations include increased complexity for large interfaces and difficulty in quickly prototyping or making substantial layout changes compared to simpler layouts like FlowLayout or BorderLayout .

Focus handling in Euro_Converter is managed by tracking which JTextField component lost focus last, using this information to determine the source currency for conversions. This model responds to user interactions by predicting the user's intended action based on focus changes. However, potential improvements include implementing more explicit event handling, such as context-aware buttons or controls, to directly specify which currency to convert from, reducing reliance on indirect focus-tracking mechanisms and potentially increasing accuracy and responsiveness to user intentions .

Utilizing older coding techniques, such as those found in the Euro_Converter applet, can lead to significant security and compatibility issues. Java applets have largely been deprecated due to their vulnerabilities and the difficulty in securing them against modern threats like cross-site scripting and sandbox evasion. Furthermore, browser support for applets has diminished, leading to compatibility problems with newer systems. In contemporary contexts, it's advisable to migrate such applications to newer technologies like Java Web Start or rewrite them using web technologies like HTML5 and JavaScript to ensure security and compatibility .

In Euro_Converter, JLabel components serve as static text displays that indicate to the user what information should be entered or is being displayed in nearby JTextField components. JTextFields are interactive elements where the user inputs or views data values such as amounts in euros, dollars, or drachmas. Together, they streamline user interaction by providing a clear, organized way to input and visualize data within the applet, enhancing usability and user experience .

The Euro_Converter class manages conversion operations by using the 'focus_lost' string variable to track which text field was last used by the user. Depending on this value, the actionPerformed method calls the appropriate conversion method—convert_from_euro, convert_from_dollar, or convert_from_drachmas—to perform the necessary calculation from the last active currency input to the others. Each conversion method updates the text fields for the respective currencies using formulas based on predefined conversion rates (i.e., euro_dollar and euro_drachmas). The 'focus_lost' variable is crucial for determining the source currency for conversion operations .

The getParameter method is essential in Euro_Converter for retrieving initial conversion rates (euro_dollar and euro_drachmas) passed as HTML parameters at applet load time. These values are used in conversion calculations, allowing the applet to be dynamically configured with different rates without altering the code. This flexibility can adjust the applet's behavior for different economic scenarios or regions by changing the parameter values in the HTML embedding the applet .

JOptionPane in Euro_Converter provides a user-friendly method for displaying error messages, such as when invalid input is detected. It benefits applications by offering immediate, clear feedback through a graphical error dialog that helps guide user input correction. However, drawbacks include the interruption to user workflow, as modal dialogs can be obtrusive. Additionally, repeated alerts can cause frustration, suggesting that non-intrusive feedback mechanisms or inline error messages could be considered for a smoother user experience .

The method handle_double is designed to safely convert string input into a double. It enhances robustness by using a try-catch block to handle potential NumberFormatException errors, which can occur if the input cannot be parsed into a double. If an error is caught, the method displays an error dialog to the user, prompting them to enter a valid arithmetic value, thereby preventing the application from crashing due to invalid input .

The Euro_Converter applet utilizes Java's event handling by implementing ActionListener and FocusListener interfaces. When the 'Okay!' button is clicked, the actionPerformed method is invoked, checking which field last lost focus and then calls the appropriate conversion method based on that field—either convert_from_euro, convert_from_dollar, or convert_from_drachmas. The focusLost method keeps track of the most recently accessed text field. Additionally, it sets up the focus listeners on each text field and an action listener on the button to manage interactions .

GridBagLayout is used in Euro_Converter to create a flexible layout that can accommodate different user interface components such as labels, text fields, and buttons in a structured way. The build_constraints method defines constraints like gridx, gridy, gridwidth, gridheight, weightx, and weighty for each GUI component, allowing for precise control of their placement and resizing behavior in the user interface .

You might also like