这次我们结合我上两篇关于简易计算器界面设计和通过BMI判断人的体型的文章来写一个“体型计算器”(我们初学者要学会综合的、系统的、规范的利用所学知识,完成需求)。同样,相关步骤给出注释以方便大家交流学习。
代码:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
abstract class People{
int weight;
int height;
public People(int weight, int height) {
this.weight = weight;
this.height = height;
}
abstract public float stature();
public String drawResult() {
System.out.println("体重:"+this.weight+"(KG) 身高:"+this.height+"(cm)");
String drawresult = new String();
float statureResult = this.weight/this.stature()-1;
if(statureResult>=-0.1&&statureResult<=0.1) {
drawresult = "标准";
}else if (statureResult<-0.1) {
drawresult ="偏瘦";
}else if (statureResult>0.1) {
drawresult ="偏重";
}else {
drawresult = "啥,应该不会运行到这里吧?";
}
return drawresult;
};
}
class Woman extends People {
Woman(int w, int h){
super(w, h);
}
@Override
public float stature() {
// 女性:(身高cm-70)×60﹪=标准体重
return (float) ((this.height-70.0)*0.6);
}
}
class Man extends People {
Man(int w, int h){
super(w, h);
}
@Override
public float stature() {
//男性:(身高cm-80)×70﹪=标准体重
return (float) ((this.height-80.0)*0.7);
}
}
public class CalculatorBody extends JFrame {
public CalculatorBody() {
// 创建窗口
final JFrame JWindow = new JFrame("体型计算器:");
// 设置为流动布局,居中 水平间距为0 垂直间距为6
JWindow.setLayout(new FlowLayout(1,0,6));
// 设置窗体尺寸为 宽340 高 240
JWindow.setSize(340,240);
// 设置窗口相对于指定组件的位置。如果组件当前未显示或者 null,则此窗口将置于屏幕的中央。
JWindow.setLocationRelativeTo(null);
//用户单击窗口的关闭按钮时程序执行的操作 WindowConstants.EXIT_ON_CLOSE 代表关闭退出
JWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 不可以改变大小
JWindow.setResizable(false);
// 设置字体
// Font FontInfo = new Font("宋体", Font.PLAIN, 20);
// 创建容器
JPanel Panel = new JPanel(new GridLayout(0,1));
// 定义按钮面板,并设置为网格布局,3列
JPanel HeightPanel2 = new JPanel(new GridLayout(0,3));
JPanel WeightPanel3 = new JPanel(new GridLayout(0,3));
JPanel SexPanel4 = new JPanel(new GridLayout(0,3));
JPanel ButtonPanel5 = new JPanel(new GridLayout(0,2));
JPanel LablePanel6 = new JPanel(new GridLayout(0,2));
JLabel CalculatorLable = new JLabel();
CalculatorLable.setText("体型计算器");
Panel.add(CalculatorLable);
JLabel HeightLable = new JLabel();
HeightLable.setText("Your height:");
HeightPanel2.add(HeightLable);
// 创建输入高度文本框
JTextArea HeightJText = new JTextArea(1,6);
HeightJText.setPreferredSize(new Dimension(100,30));
// HeightJText.setBorder(BorderFactory.createLineBorder(Color.black,1));
HeightPanel2.add(HeightJText);
// 身高单位标签
JLabel CmLabel = new JLabel();
CmLabel.setText("CM");
HeightPanel2.add(CmLabel);
JLabel WeightLabel = new JLabel();
WeightLabel.setText("Your Weight:");
WeightPanel3.add(WeightLabel);
// 创建输入体重文本框
JTextArea WeightJText = new JTextArea(1,6);
WeightJText.setPreferredSize(new Dimension(100,30));
WeightPanel3.add(WeightJText);
JLabel Kglabel = new JLabel();
Kglabel.setText("KG");
WeightPanel3.add(Kglabel);
JLabel SexLabel = new JLabel();
SexLabel.setText("Your Sex:");
SexPanel4.add(SexLabel);
ButtonGroup buttonGroup1 = new ButtonGroup();
JRadioButton RadioButtonWoman = new JRadioButton();
JRadioButton RadioButtonMan = new JRadioButton();
RadioButtonWoman.setSelected(true);
RadioButtonWoman.setText("女");
RadioButtonWoman.setMargin(new Insets(0, 0, 0, 0));
RadioButtonMan.setText("男");
RadioButtonMan.setMargin(new Insets(0, 0, 0, 0));
buttonGroup1.add(RadioButtonMan);
buttonGroup1.add(RadioButtonWoman);
SexPanel4.add(RadioButtonWoman);
SexPanel4.add(RadioButtonMan);
JLabel StatureLabel = new JLabel();
StatureLabel.setText("Your Stature:");
LablePanel6.add(StatureLabel);
JLabel StatureLabelInfo = new JLabel();
LablePanel6.add(StatureLabelInfo);
JButton Okbutton = new JButton();
JButton Cancelbutton = new JButton();
Okbutton.setText("OK");
Cancelbutton.setText("Cancel");
// OK按钮新增监听事件。
Okbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (RadioButtonWoman.isSelected()) {
StatureLabelInfo.setText(new Woman(Integer.parseInt(HeightJText.getText()),
Integer.parseInt(WeightJText.getText())).drawResult());
} else {
StatureLabelInfo.setText(new Man(Integer.parseInt(HeightJText.getText()),
Integer.parseInt(WeightJText.getText())).drawResult());
}
}
});
Cancelbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JWindow.dispose();
}});
ButtonPanel5.add(Okbutton);
ButtonPanel5.add(Cancelbutton);
// Panel顶部
JWindow.getContentPane().add(Panel,BorderLayout.NORTH);
// Panel中部
JWindow.getContentPane().add(HeightPanel2,BorderLayout.CENTER);
JWindow.getContentPane().add(WeightPanel3,BorderLayout.CENTER);
JWindow.getContentPane().add(SexPanel4,BorderLayout.CENTER);
JWindow.getContentPane().add(LablePanel6,BorderLayout.CENTER);
// Panel 设置到底部
JWindow.getContentPane().add(ButtonPanel5,BorderLayout.SOUTH);
JWindow.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CalculatorBody calculator = new CalculatorBody();
}
}
实现效果:
![](https://img-blog.csdnimg.cn/e8c0a0787a7345b7bb56fa50ea149e5c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5rex5bGx6buR55qu54yq,size_10,color_FFFFFF,t_70,g_se,x_16)
(请忽略博主的体型......)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)