从我的Java程序接收到大量错误
我一直在努力学习如何创建GUI Java程序。我一直在跟随一本书来学习这个过程,但尽管我尽我所知照本宣科,我还是遇到了一些问题。这是我的完整代码开始从我的Java程序接收到大量错误,java,netbeans,Java,Netbeans,我一直在努力学习如何创建GUI Java程序。我一直在跟随一本书来学习这个过程,但尽管我尽我所知照本宣科,我还是遇到了一些问题。这是我的完整代码开始 package healthprofilegui; //packages to be imported for the GUI import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import j
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) {
}
}
我在课堂上收到的第一个错误
public class HealthProfileGUI extends JFrame implements ActionListener
is HealthProfileGUI不是抽象的,并且不会覆盖ActionListener中的抽象方法actionPerformedActionEvent
我不完全确定我为什么会收到这个错误
第二个/第三个是我试图使用的一种方法,用于设置GUI的布局
public profileGUI()
是不是给了我错误
invalid method declaration; return type required
我不知道为什么这个出现在说明书中从来没有使用过退货
我还得到了这个对super的错误调用必须是构造函数中的第一个语句
为了
我试图找到尽可能多的关于这个错误的信息,但是找不到任何与我的问题相似的信息
我最不关心的是
@Override
public void action(ActionEvent e)
方法不会重写或实现超类型中的方法
我很抱歉,有这么多,我一直按照指示,我可以与假设,错误会自行解决后,我把所有的代码。然而,情况并非如此
我非常感谢你的帮助。尤其是因为这对我来说是一个巨大的难题。关于您的第一个和第四个错误,actionPerformed方法是您正在实现的ActionListener接口的一部分。您的方法称为action,它不在该接口中
在第二个和第三个错误中,构造函数的名称必须与类的名称相同。请注意,这是另外一回事。开始时需要注意以下几点:
//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)
一般忠告:
当你有很多错误或一个大问题时,把它分解成更小的问题。例如,减少您的代码,使您只有一个,解决它,并移动到下一个。更好的做法是一步一步地构建,确保每一步都没有错误。
对于将来的问题,请发布一个。Missing HealthProfile class.JavaScript不是Java我从来没有说过关于JavaScript的任何事情我说的是Java脚本。这是两个独立的词。但它的措辞可能令人困惑,这是可以理解的。我完全理解你的意图。你所做的没有错。只是我在找剧本。你的问题是对的。我重新编写了它以保存将来的搜索。
//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)