
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
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.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
public class BMI extends JFrame implements ActionListener{
private JPanel contentPane;
private JTextField getweight;
private JTextField getheight;
private JTextArea BMIhzi;
private JTextArea pinjia;
private JButton jishuan;
private JButton clear;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BMI frame = new BMI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public BMI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(600, 300, 522, 331);
contentPane = new JPanel();
contentPane.setForeground(Color.RED);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("\u8BF7\u8F93\u5165\u60A8\u7684\u4F53\u91CD\u548C\u8EAB\u9AD8\uFF0C\u4EE5\u4FBF\u8BA1\u7B97\u60A8\u7684BMI\u503C");
lblNewLabel.setForeground(Color.RED);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("宋体", Font.BOLD, 18));
lblNewLabel.setBounds(64, 10, 432, 43);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("\u4F53\u91CD\uFF1A\uFF08\u516C\u65A4\uFF09");
lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 17));
lblNewLabel_1.setBounds(15, 70, 130, 30);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("\u8EAB\u9AD8\uFF1A\uFF08\u7C73\uFF09");
lblNewLabel_2.setFont(new Font("宋体", Font.BOLD, 17));
lblNewLabel_2.setBounds(15, 110, 130, 30);
contentPane.add(lblNewLabel_2);
JLabel lblNewLabel_3 = new JLabel("BMI\u503C\uFF1A");
lblNewLabel_3.setFont(new Font("宋体", Font.BOLD, 17));
lblNewLabel_3.setBounds(15, 150, 130, 30);
contentPane.add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel("\u8BC4\u4EF7\uFF1A");
lblNewLabel_4.setFont(new Font("宋体", Font.BOLD, 17));
lblNewLabel_4.setBounds(15, 190, 130, 30);
contentPane.add(lblNewLabel_4);
getweight = new JTextField();
getweight.setFont(new Font("宋体", Font.BOLD, 17));
getweight.setBounds(150, 70, 300, 30);
contentPane.add(getweight);
getweight.setColumns(10);
getheight = new JTextField();
getheight.setFont(new Font("宋体", Font.BOLD, 17));
getheight.setBounds(150, 110, 300, 30);
contentPane.add(getheight);
getheight.setColumns(10);
BMIhzi = new JTextArea();
BMIhzi.setFont(new Font("Monospaced", Font.BOLD, 17));
BMIhzi.setBounds(150, 150, 300, 30);
contentPane.add(BMIhzi);
pinjia = new JTextArea();
pinjia.setFont(new Font("Monospaced", Font.BOLD, 17));
pinjia.setBounds(150, 190, 300, 30);
contentPane.add(pinjia);
jishuan = new JButton("\u8BA1\u7B97");
jishuan.addActionListener(this);
jishuan.setFont(new Font("宋体", Font.BOLD, 18));
jishuan.setBounds(110, 240, 150, 30);
contentPane.add(jishuan);
clear = new JButton("\u91CD\u7F6E");
clear.setFont(new Font("宋体", Font.BOLD, 18));
clear.addActionListener(this);
clear.setBounds(280, 240, 150, 30);
contentPane.add(clear);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
Object i=arg0.getSource();
if(i==jishuan)
{
try
{
double a=Double.parseDouble(getweight.getText());
double b=Double.parseDouble(getheight.getText());
}
catch(NumberFormatException e)
{
System.out.println("输入错误!!!");
}
double a=Double.parseDouble(getweight.getText());
double b=Double.parseDouble(getheight.getText());
double c=a/(b*b);
BMIhzi.setText(""+c);
if(c<18.5)
{
pinjia.setText("体重过轻");
}
else if(c>=18.5&&c<24)
{
pinjia.setText("体重正常");
}
else if(c>=24&&c<27)
{
pinjia.setText("体重过重");
}
else if(c>=27&&c<30)
{
pinjia.setText("轻度肥胖");
}
else if(c>=30&&c<35)
{
pinjia.setText("中度肥胖");
}
else if(c>=35)
{
pinjia.setText("重度肥胖");
}
}
else if(i==clear)
{
getweight.setText("");
getheight.setText("");
pinjia.setText("");
BMIhzi.setText("");
}
}
}

这是一个使用Java编写的BMI(身体质量指数)计算应用程序。它包含GUI组件如JFrame、JTextField、JButton等,用于输入体重和身高,并计算及显示BMI值。

922

被折叠的 条评论
为什么被折叠?



