求教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英尺。求帮忙 展开
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英尺。求帮忙 展开
展开全部
首先,bmiCalculator类名小写,违反java规约。
缺少返回语句的错误应该是
public BMI Calculator()这个方法。
此方法要求返回一个BMI类型的数据,但方法最厚并没有return语句。
另外 JButton tranButton = new JButton("Metric/Imperial")不会启用的原因可能是没有
给该按钮添加监听? tranButton.addActionListener(new BMIListener());
自己没写过Applet程序,所以以上均为推测。呵呵
缺少返回语句的错误应该是
public BMI Calculator()这个方法。
此方法要求返回一个BMI类型的数据,但方法最厚并没有return语句。
另外 JButton tranButton = new JButton("Metric/Imperial")不会启用的原因可能是没有
给该按钮添加监听? tranButton.addActionListener(new BMIListener());
自己没写过Applet程序,所以以上均为推测。呵呵
展开全部
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程序包
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程序包
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-10-17
展开全部
学习
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询