nathan rivera nathan rivera - 2 months ago 7
Java Question

Receiving a number of errors for my Java script

Ive been trying to learn how to create a GUI Java program. I have been following a book to learn the process but despite me copying the book too the letter (to the best of my knowledge) I have been running into a number of issues. Here is my full code to start.

package healthprofilegui;

//packages to be imported for the GUI
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class HealthProfileGUI extends JFrame implements ActionListener {

// text field variables used to get user input.
JTextField txtName = new JTextField(15);
JTextField txtAge = new JTextField(15);
JTextField txtWeight = new JTextField(15);
JTextField txtHeight_feet = new JTextField(15);
JTextField txtHeight_inches = new JTextField(15);

//text field variables used to display calculations.
JTextField txtBMI = new JTextField(15);
JTextField txtCategory = new JTextField(15);
JTextField txtMaxHeartRate = new JTextField(15);

//buttons to be used for the program.
JButton btnDisplay = new JButton("Display");
JButton btnClear = new JButton("Clear");

Integer heightTotal = null;

private HealthProfile myProfile;

public profileGUI()
{
super("HealthProfile");
myProfile = new HealthProfile();
setLayout(new GridLayout(0,2));

//adds the text field labels to the program.
add(new JLabel("txtName"));
add(txtName);
add(new JLabel("txtAge"));
add(txtAge);
add(new JLabel("txtWeight"));
add(txtWeight);
add(new JLabel("txtHeight_feet"));
add(txtHeight_feet);
add(new JLabel("txtHeight_inches"));
add(txtHeight_inches);


//adds the buttons to the program.
add(btnDisplay);
add(btnClear);

//adds labels for the output fields
add(new JLabel("txtBMI"));
add(txtBMI);
add(new JLabel("txtCategory"));
add(txtCategory);
add(new JLabel("txtMaxHeartRate"));
add(txtMaxHeartRate);

setVisible(true);
btnDisplay.addActionListener(this);
btnClear.addActionListener(this);
}



@Override
public void action(ActionEvent e)
{
//clear text if the button is pressed.
if(e.getSource() == btnClear)
{
System.out.println("Clear Pressed");
txtName.setText("");
txtAge.setText("");
txtWeight.setText("");
txtHeight_feet.setText("");
txtHeight_inches.setText("");
txtBMI.setText("");
txtCategory.setText("");
txtMaxHeartRate.setText("");

}

//process data if pressed.
if(e.getSource() == btnDisplay)
{
//checks for missing input
if (txtName.getText().isEmpty() || txtAge.getText().isEmpty() || txtWeight.getText().isEmpty() || txtHeight_feet.getText().isEmpty() ||
txtHeight_inches.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, "Please provide all input.");
return;
}

myProfile.setName(txtName.getText());



try
{
myProfile.setAge(Integer.parseInt(txtAge.getText()));
myProfile.setWeight(Integer.parseInt(txtWeight.getText()));

}
catch (NumberFormatException ex)
{
JOptionPane.showMessageDialog(null, "Please enter a numeric value");
return;
}




}
}

public static void main(String[] args) {

}

}


The first error I am receiving for the class

public class HealthProfileGUI extends JFrame implements ActionListener


is HealthProfileGUI is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener.

I'm not entirely certain why I'm receiving this error.

The second/third is for a method i'm trying to use in order to set the layout for the GUI

public profileGUI()


is giving me the error

invalid method declaration; return type required


I have no idea why this appearing as the instructions never used a "return" in it.

I'm also getting this error "call to super must be first statement in constructor"

for

super("HealthProfile");


I have tried to find as much information to this error as possible but couldn't quite find anything that replicated my issue to a similar degree.

The very last thing I'm having issues with is

@Override
public void action(ActionEvent e)


method does not override or implement a method from a supertype

I'm sorry that there is so much, I kept following the instructions as best I could with assumptions that the errors would sort themselves out after I finished placing all the code. However that is not so much the case.

I greatly appreciate the assistance. Especially since this has been a huge handful for me.

Answer

A few notes to get you started:

//HealthProfile is missing  
private HealthProfile myProfile;

//the name of the constructor needs to be the same as the class name
public HealthProfileGUI() 

@Override
//you should override actionPerformed not action 
//public void action(ActionEvent e)
public void actionPerformed(ActionEvent e)

General advice:
When you have many errors or a "big problem" break it into smaller ones. For example reduce you code so you have just one, solve it, and move to the next. Better practice would be build it step by step, making sure that every step is error free.
For future questions please post an MCVE.

Comments