求教java缺少返回语句的问题 10

importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.event.*... import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class bmiCalculator extends JFrame
{
//Panel
JPanel mainPanel = new JPanel();
JPanel textPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JPanel messagePanel = new JPanel();
//Label
JLabel heightLebal = new JLabel("Height(m)");
JLabel weightLebal = new JLabel("Weight(kg)");
JLabel bmiLebal = new JLabel("Your BMI is");
//Textfield
JTextField heightField = new JTextField(10);
JTextField weightField = new JTextField(10);
JTextField bmiField = new JTextField(4);
JTextField messageField = new JTextField(10);
//Button
JButton bmiButton = new JButton("Calculate BMI");
JButton tranButton = new JButton("Metric/Imperial");
public BMI Calculator()
{
bmiField.setEditable(false);
messageField.setEditable(false);
bmiButton.addActionListener(new BMIListener());
textPanel.add(heightLebal);
textPanel.add(heightField);
textPanel.add(weightLebal);
textPanel.add(weightField);
buttonPanel.add(bmiButton);
buttonPanel.add(tranButton);
messagePanel.add(bmiLebal);
messagePanel.add(bmiField);
messagePanel.add(messageField);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(textPanel, BorderLayout.WEST);
mainPanel.add(buttonPanel, BorderLayout.EAST);
mainPanel.add(messagePanel, BorderLayout.SOUTH);
add(mainPanel);
}
private class BMIListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double meters = Double.parseDouble(heightField.getText());
double kilograms = Double.parseDouble(weightField.getText());
int bmi = (int)computeBMI(meters, kilograms);
bmiField.setText("" + bmi);
if(bmi<18)
messageField.setText("You are underweight");
else if(18<=bmi && bmi<25)
{
messageField.setText("You are normal");
}
else if(bmi>=25 && bmi<=30)
{
messageField.setText("You are considered overweight");
}
else if(bmi>30)
{
messageField.setText("You are considered obese");
}
}
}
public static double computeBMI(double weight, double height)
{
return weight / (height * height);
}
public static void main(String[] args)
{
JFrame myFrame = new bmiCalculator();
myFrame.setTitle("BMI Calculator");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(400,300);
myFrame.setVisible(true);
}
}

bmiCalculator.java:49: 错误: 缺少返回语句
另外 JButton tranButton = new JButton("Metric/Imperial")不会启用,目标是达到公制和英制的转换。1千克等于2.205磅,1米等于3.28英尺。求帮忙
展开
 我来答
songyx1982
2012-10-16 · TA获得超过197个赞
知道小有建树答主
回答量:98
采纳率:100%
帮助的人:69.6万
展开全部
首先,bmiCalculator类名小写,违反java规约。
缺少返回语句的错误应该是
public BMI Calculator()这个方法。
此方法要求返回一个BMI类型的数据,但方法最厚并没有return语句。

另外 JButton tranButton = new JButton("Metric/Imperial")不会启用的原因可能是没有

给该按钮添加监听? tranButton.addActionListener(new BMIListener());

自己没写过Applet程序,所以以上均为推测。呵呵
二二七英芳I1
2012-10-16
知道答主
回答量:7
采纳率:0%
帮助的人:1万
展开全部
package network;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class bmiCalculator extends JFrame
{
//Panel
JPanel mainPanel = new JPanel();
JPanel textPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JPanel messagePanel = new JPanel();
//Label
JLabel heightLebal = new JLabel("Height(m)");
JLabel weightLebal = new JLabel("Weight(kg)");
JLabel bmiLebal = new JLabel("Your BMI is");
//Textfield
JTextField heightField = new JTextField(10);
JTextField weightField = new JTextField(10);
JTextField bmiField = new JTextField(4);
JTextField messageField = new JTextField(20);
//Button
JButton bmiButton = new JButton("Calculate BMI");
JButton tranButton = new JButton("Metric/Imperial");

public bmiCalculator()
{
bmiField.setEditable(false);
messageField.setEditable(false);
bmiButton.addActionListener(new BMIListener());
textPanel.add(heightLebal);
textPanel.add(heightField);
textPanel.add(weightLebal);
textPanel.add(weightField);
buttonPanel.add(bmiButton);
buttonPanel.add(tranButton);
messagePanel.add(bmiLebal);
messagePanel.add(bmiField);
messagePanel.add(messageField);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(textPanel, BorderLayout.WEST);
mainPanel.add(buttonPanel, BorderLayout.EAST);
mainPanel.add(messagePanel, BorderLayout.SOUTH);
add(mainPanel);
}

private class BMIListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double meters = Double.parseDouble(heightField.getText());
double kilograms = Double.parseDouble(weightField.getText());
int bmi = (int)computeBMI( kilograms,meters);
bmiField.setText("" + bmi);
if(bmi<18)
messageField.setText("You are underweight");
else if(18<=bmi && bmi<25)
{
messageField.setText("You are normal");
}
else if(bmi>=25 && bmi<=30)
{
messageField.setText("You are considered overweight");
}
else if(bmi>30)
{
messageField.setText("You are considered obese");
}
}
}

public double computeBMI(double weight, double height)
{
return weight / (height * height);
}

public static void main(String[] args)
{
JFrame myFrame = new A();
myFrame.setTitle("BMI Calculator");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(400,300);
myFrame.setVisible(true);
}
}
// int bmi = (int)computeBMI(meters, kilograms);
改为int bmi = (int)computeBMI( kilograms,meters);
JButton tranButton = new JButton("Metric/Imperial")没有事件监听
你的功能是确定人的身高与体重的平衡吧 “公制和英制的转换??”
建议你安一个java用于编写gui程序包
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友2fcbbc2
2012-10-16
知道答主
回答量:69
采纳率:0%
帮助的人:18.3万
展开全部
看看
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2012-10-17
展开全部
学习
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式