📄 fat.java
字号:
/**
*该类用于肥胖度的测试
*只要输入你的身高和体重
*便可以轻松计算机出你是肥胖度即(BMI)
*@author 曾文秋
*version JDK 1.5
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.net.URL;
import java.sql.*;
import javax.swing.table.*;
import java.util.*;
import java.awt.geom.*;
/**@see Fat
Fat 类是主类继承了JFrame
*/
public class Fat extends JFrame
{
Container cp;/*定义容器变量*/
/**用于输入姓名*/
JTextField YName;
/**用来保存输入的体重*/
JTextField Weight;
/**用来保存输入的身高*/
JTextField Height;
/**要查找的人的姓名*/
JTextField FName;
JTextField deleteName;
/**点击存储你的信息*/
JButton record;
//全部用来显示提示信息
JLabel label1;
JLabel label2;
JLabel label3;
JLabel label4;
JLabel label5;
JLabel label6;
JLabel label7;
JLabel label8;
JLabel label9;
JLabel label10;
JLabel label11;
JLabel label12;
JLabel label13;
ImageIcon licon;//标尺图象
/**用于模拟肥胖度测试*/
JButton simulate;
/**清除界面*/
JButton clear;
/**用于模拟数据库里所有人的肥胖度测试*/
JButton simulateAll;
/**用于查找特定的人,以显示他的体重和身高*/
JButton find;
JButton caculateBMI;//用于计算BMI
JButton delete;//删除数据库的一个指定的人
JButton deleteAll;//删除数据库里所有的人
JScrollPane scrollPane;
JTable table;//用语保存当前的人的姓名,性别,身高,体重,BMI,以及评价
/**创建二个滑动条*/
JSlider sliderW;
JSlider sliderH;
/**在屏幕上画线的启始点的坐标*/
private int sX1;
private int sY1;
/**在屏幕上画线的结束点的坐标*/
private int sX2;
private int sY2;
private Statement comm;
private ResultSet rst;//数据库查询返回结果记录集
/**性别选择组件*/
JRadioButton men;
JRadioButton woman;
ButtonGroup cbg;
/**表示性别信息*/
private String sex;//store the sex message
/**用来保存瘦,正常,超重,一度肥胖,二度肥胖的人的个数*/
public static int [] a=new int[5];
double BMI;//肥胖度
/**@see RadioExam*/
/**内嵌类 实现ActionListener接口,
用于处理JRadioButton的鼠标点击事件,
实现性别的选择
*/
public class RadioExam implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="Male"){
sex=new String("Male");
}
else{
sex=new String("Female");
}
}
}
public Fat(){
cp=getContentPane(); /*创建容器变量*/
setLayout(null);//取消自动布局
this.setSize(900,730);//设置窗口大小
this.setLocation(60,17);
this.setTitle("肥胖度测量");//设置窗口题目
ImageIcon image=new ImageIcon("top.gif");
this.setIconImage(image.getImage());//设置图标
ButtonGroup cbg=new ButtonGroup();
men=new JRadioButton("Male");//创建男
men.setFont(new Font("Courier",Font.PLAIN,15));//设置字体
cp.add(men);
men.reshape(42,20,100,25);//把按钮放在(42,20),大小100*25
//ImageIcon im=new ImageIcon("zwq.gif");
//Graphics g=getGraphics();
//g.drawImage(im.getImage(),0,450,this);
woman=new JRadioButton("Female"); //创建女
woman.setFont(new Font("Courier",Font.PLAIN,15));//设置字体
cp.add(woman);
woman.reshape(142,20,100,25);//同上
cbg.add(men);//将按钮加入按钮组
cbg.add(woman);
men.addActionListener(new RadioExam());//增加按钮事件监听
woman.addActionListener(new RadioExam());
//label1,label2,label3,label5,label6,label7,label8,label9,label10,label11,label12,label13
//全是用来表示提示信息
//label4用来放刻度的图片
label1=new JLabel("YourName is:");
label1.setBounds(45,55,100,25);//跟reshape()功能一样
cp.add(label1);
label1.setFont(new Font("Courier",Font.PLAIN,15));
label2=new JLabel("Weight(Kg):");
cp.add(label2);
label2.setBounds(45,85,100,25);
label2.setFont(new Font("Courier",Font.PLAIN,15));
label3=new JLabel("Height(cm):");
cp.add(label3);
label3.setBounds(45,115,100,25);
label3.setFont(new Font("Courier",Font.PLAIN,15));
licon=new ImageIcon("Fatness.gif");
label4=new JLabel(licon);
cp.add(label4);
label4.reshape(301,83,499,548);
label5=new JLabel("Store record");
cp.add(label5);
label5.setBounds(45,180,100,25);
label5.setFont(new Font("Courier",Font.PLAIN,15));
label6=new JLabel("simulate all");
cp.add(label6);
label6.setBounds(45,210,100,25);
label6.setFont(new Font("Courier",Font.PLAIN,15));
label7=new JLabel("foundName");
cp.add(label7);
label7.setBounds(45,240,100,25);
label7.setFont(new Font("Courier",Font.PLAIN,15));
label8=new JLabel("find");
cp.add(label8);
label8.setBounds(45,270,100,25);
label8.setFont(new Font("Courier",Font.PLAIN,15));
label9=new JLabel("caculateBMI");
cp.add(label9);
label9.setBounds(45,145,100,25);
label9.setFont(new Font("Courier",Font.PLAIN,15));
label11=new JLabel("deleteName");
cp.add(label11);
label11.setBounds(45,300,100,25);
label11.setFont(new Font("Courier",Font.PLAIN,15));
label11=new JLabel("delete");
cp.add(label11);
label11.setBounds(45,330,100,25);
label11.setFont(new Font("Courier",Font.PLAIN,15));
label12=new JLabel("deleteAll");
cp.add(label12);
label12.setBounds(45,360,100,25);
label12.setFont(new Font("Courier",Font.PLAIN,15));
label13=new JLabel(" 一开始靠在刻度旁边的滑动条是隐藏的,"+
"在模拟肥胖度前,请用鼠标点击刻度旁边以显示滑动条来改变刻度");
cp.add(label13);
label13.setBounds(0,0,1024,25);
YName=new JTextField();//创建一个文本区域
cp.add(YName);//把它加到容器中
YName.setBounds(145,55,100,25);//设置位置个大小,分别为(145,55),100*25;
YName.setFont(new Font("Courier",Font.PLAIN,15));//设置字体
Weight=new JTextField();//同上
cp.add(Weight);
Weight.setBounds(145,85,100,25);
Weight.setFont(new Font("Courier",Font.PLAIN,15));
Height=new JTextField();//同上
cp.add(Height);
Height.setBounds(145,115,100,25);
Height.setFont(new Font("Courier",Font.PLAIN,15));
FName=new JTextField();//同上
cp.add(FName);
FName.setBounds(145,240,100,25);
FName.setFont(new Font("Courier",Font.PLAIN,15));
deleteName=new JTextField();//同上
cp.add(deleteName);
deleteName.setBounds(145,300,100,25);
deleteName.setFont(new Font("Courier",Font.PLAIN,15));
record=new JButton("save");//创建一个按钮
record.setFont(new Font("Courier",Font.PLAIN,15));//设置按钮的显示字体
cp.add(record);//加入到容器中
record.setBounds(145,180,100,25);//设置放的位置和大小
record.setToolTipText("保存到数据库");//设置按钮的提示信息
record.addActionListener(new bAction());//增加按钮的事件监听
ImageIcon play=new ImageIcon("play.gif");
ImageIcon play1=new ImageIcon("play1.gif");
simulate=new JButton(play); //用一个图标创建一个按钮
simulate.setActionCommand("simulate"); //给按钮一个明确的行动命令,
simulate.setRolloverEnabled(true); //使得按钮图标能变动
simulate.setPressedIcon(play1); //设置鼠标按下是的图标
cp.add(simulate);
simulate.setBounds(330,30,75,28); //设置模拟按钮的方位个大小
simulate.setToolTipText("模拟肥胖度"); //设置按钮提示信息
simulate.addActionListener(new bAction());
ImageIcon stop1=new ImageIcon("stop1.gif");
ImageIcon stop2=new ImageIcon("Stop2.gif");
clear=new JButton(stop1); //用一个图标创建一个按钮
clear.setActionCommand("clear"); //给按钮一个明确的行动命令
clear.setRolloverEnabled(true); //使得按钮图标能变动
clear.setPressedIcon(stop2); //设置鼠标按下是的图标
cp.add(clear);
clear.setBounds(700,30,75,28);
clear.setToolTipText("清除");
clear.addActionListener(new bAction());
simulateAll=new JButton("simulateAll");//同上
simulateAll.setFont(new Font("Courier",Font.PLAIN,15));
cp.add(simulateAll);
simulateAll.setBounds(145,210,100,25);
simulateAll.setToolTipText("模拟全部人");
simulateAll.addActionListener(new bAction());
find=new JButton("Find");//同上
find.setFont(new Font("Courier",Font.PLAIN,15));
cp.add(find);
find.setBounds(145,270,100,25);
find.setToolTipText("查找");
find.addActionListener(new bAction());
caculateBMI=new JButton("caculateBMI");//同上
caculateBMI.setFont(new Font("Courier",Font.PLAIN,15));
cp.add(caculateBMI);
caculateBMI.setBounds(145,145,100,25);
caculateBMI.setToolTipText("计算BMI");
caculateBMI.addActionListener(new bAction());
delete=new JButton("delete");//同上
delete.setFont(new Font("Courier",Font.PLAIN,15));
cp.add(delete);
delete.setBounds(145,330,100,25);
delete.setToolTipText("删除特定的一个人");
delete.addActionListener(new bAction());
deleteAll=new JButton("deleteAll");//同上
deleteAll.setFont(new Font("Courier",Font.PLAIN,15));
cp.add(deleteAll);
deleteAll.setBounds(145,360,100,25);
deleteAll.setToolTipText("删除全部人");
deleteAll.addActionListener(new bAction());
//显示滑动条
sliderW=new JSlider();
sliderW.setOrientation(1);//使滑动条竖起来
cp.add(sliderW);
sliderW.setBounds(345,125,14,500);//设置位置和大小
sliderW.addChangeListener(new SAction());//注册事件监听
sliderH=new JSlider();
sliderH.setOrientation(1);
cp.add(sliderH);
sliderH.setBounds(728,125,14,500);
sliderH.addChangeListener(new SAction());//注册事件监听
//draw();
//这是类中类,继承AWT类哭中的WindowAdapter类,
//定义事件处理的方法windowClosing,关闭窗口并结束程序
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void draw(){
ImageIcon im=new ImageIcon("zwq.gif");
Graphics g=getGraphics();//获得graphics
g.drawImage(im.getImage(),50,500,this);//画图画
g.setColor(Color.red); //设置画笔的颜色为红色
g.drawLine(sX1,sY1,sX2,sY2); //在刻度尺上画线
}
class SAction implements ChangeListener{//滑动条滑动事件处理
public void stateChanged(ChangeEvent ce){
if(ce.getSource()==sliderW){//如果是移动体重
Weight.setText(String.valueOf(sliderW.getValue()));
}
if(ce.getSource()==sliderH){//移动是身高
Height.setText(String.valueOf(sliderH.getValue()));
}
}
}
//定义bAction继承AWT类库中的ActionListener类
//定义事件处理的方法actionPerformed,实现按钮点击动作
class bAction implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="caculateBMI"){//如果鼠标点击的按钮"caculateBMI"
try{//包含异常处理
double weight=Double.parseDouble(Weight.getText().trim());//把字符串转换成double
double height=Double.parseDouble(Height.getText().trim());
BMI=(10000*weight)/(height*height);//计算BMI
if(sex.equals("Male")){ //判断sex的值是不是等于"Male"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -