Professional Documents
Culture Documents
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>
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
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();
//Get parameters
euro_dollar = handle_double(getParameter("euro_dollar"));
euro_drachmas = handle_double(getParameter("euro_drachmas"));
//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);
} //init ends
}//method ends
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