Hello, I added some things to my code and I’m getting an error that reads as follows
Hello, I added some things to my code and I’m getting an error that reads as follows:
Error:forms: C:\Users\Zackychaos\IdeaProjects\GUI’s and event handlers\src\waterflow\MGDtogpm.form: Cannot bind: field does not exist: waterflow.WaterCalGUI.WaterCalc
Not sure what is going on. In my form it is highlighting my JPanel in the component tree and saying Field “WaterCalGUI” does not exist in class “waterflow.WaterCalGUI”
My code is below. If it helps, I am using IntelliJ. Any help is appreciated:
package waterflow;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
public class WaterCalGUI extends JFrame implements ActionListener {
private JTextField mgdinput = new JTextField(10);
private JTextField gpmoutput = new JTextField(10);
private JButton conversionbutton = new JButton("Convert");
private JButton clearFormButton = new JButton("Clear Form");
private JTextArea outputTextArea = new JTextArea(8, 10);
private JLabel title = new JLabel("MGD to GPM");
private JLabel description = new JLabel("Convert millions of gallons per day to gallons per minute");
private double gpm;
private double mgd;
public WaterCalGUI(){
// Set output to not be editable and wrap lines
outputTextArea.setEditable(false);
outputTextArea.setWrapStyleWord(true);
outputTextArea.setLineWrap(true);
// Build buttons
conversionbutton.addActionListener(this);
conversionbutton.setPreferredSize(new Dimension(100, 15));
//Settings for JFrame
this.setSize(250, 150);
this.setLayout(new FlowLayout());
this.setTitle("Water Calculator");
this.add(mgdinput);
this.add(gpmoutput);
this.add(conversionbutton);
this.add(outputTextArea);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent e) {
// Decide which button the action event came from
if(e.getSource().equals(clearFormButton)) {
this.clearForm();
} else {
// Validate input before calculating
if (this.isValid(mgdinput.getText())) {
double gpd = Double.parseDouble(mgdinput.getText());
double gpm = WaterCalculator.convertMGDtoGPM(mgd);
// Format doubles before displaying them in the text area
DecimalFormat formatter = new DecimalFormat("###, ###.##");
String formattedGpd = formatter.format(gpd);
String formattedGpm = formatter.format(gpm);
// Display output
outputTextArea.setText(formattedGpd + " gallons per day is:\n\n" + formattedGpm + " gallons per minute");
}
}
}
public boolean isValid(String input) {
// First check if input field is blank or empty, then if it contains commas, then check if it is numeric
if(input.isBlank() || input.isEmpty()) {
JOptionPane.showMessageDialog(null, "Input is required");
this.clearForm();
return false;
} else if(input.contains(",")) {
JOptionPane.showMessageDialog(null, "Input must not contain commas");
return false;
} else {
try{
Double.parseDouble(input);
return true;
} catch(NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Input must be numeric");
this.clearForm();
return false;
}
}
}
public void clearForm() {
this.mgdinput.setText("");
this.gpmoutput.setText("");
mgdinput.requestFocus();
}
}
package waterflow;
public class WaterCalculator {
public static double convertMGDtoGPM(double mgd) {
double gpm = mgd * 694.44;
System.out.println(gpm);
return gpm;
}
}
package waterflow;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
try {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
}
new WaterCalGUI();
}
}