As a beginner in java programming working in netbeans IDE, I made a currency converter; I have a textfield for 'franc' and another one for 'euro', a clear button for clearing both text fields, a convert button and a quit button to leave the window. I added code behind convert button so that the conversion should be performed when it is pressed. Then i'm having some issue: When I run the program and enter a value to convert, the conversion doesn't happen unless I first click the clear button and then input the value. Now my question is how can I prevent the program from doing that. Or if it is not possible to do so, is there a way I can automatically clear all the text fields at the program start? Thanks for helping
Here are the codes that handle events in the program
~ private void clear_buttonActionPerformed(java.awt.event.ActionEvent evt) {
euro_field.setText(" ");
franc_field.setText(" ");
}
private void quit_buttonActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void convert_buttonActionPerformed(java.awt.event.ActionEvent evt) {
double francVal, euroVal;
String a = franc_field.getText();
String b = euro_field.getText();
if(b.equals(" ") && !a.equals(" ")) {
francVal = Double.parseDouble(franc_field.getText());
euroVal = francVal/660.85;
euro_field.setText(String.valueOf(euroVal));
}
else if(a.equals(" ") && !b.equals(" ")) {
euroVal = Double.parseDouble(euro_field.getText());
francVal = euroVal*660.85;
franc_field.setText(String.valueOf(francVal));
}
}
// Variables declaration - do not modify
private java.awt.Button clear_button;
private java.awt.Button convert_button;
private javax.swing.JTextField euro_field;
private java.awt.Label euro_label;
private javax.swing.JTextField franc_field;
private java.awt.Label franc_label;
private java.awt.Button quit_button;
private java.awt.Label welcome_label;
// End of variables declaration ~
When a JTextField
is empty, its getText()
method returns an empty string and not a string that contains a single space. Hence the conditions in method convert_buttonActionPerformed()
are never true, but method clear_buttonActionPerformed()
actually inserts a single space into the JTextField
so when you call method convert_buttonActionPerformed()
after calling method clear_buttonActionPerformed()
then the conditions in method convert_buttonActionPerformed()
are true.
Change method clear_buttonActionPerformed()
to the following:
private void clear_buttonActionPerformed(java.awt.event.ActionEvent evt) {
euro_field.setText("");
franc_field.setText("");
}
And change method convert_buttonActionPerformed()
to the following:
private void convert_buttonActionPerformed(java.awt.event.ActionEvent evt) {
double francVal, euroVal;
String a = franc_field.getText();
String b = euro_field.getText();
if(b.equals("") && !a.equals("")) {
francVal = Double.parseDouble(franc_field.getText());
euroVal = francVal/660.85;
euro_field.setText(String.valueOf(euroVal));
}
else if(a.equals("") && !b.equals("")) {
euroVal = Double.parseDouble(euro_field.getText());
francVal = euroVal*660.85;
franc_field.setText(String.valueOf(francVal));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.