-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBmiGui2.java
More file actions
148 lines (122 loc) · 3.02 KB
/
BmiGui2.java
File metadata and controls
148 lines (122 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Bmi{
private double weight,height;
public Bmi(){}
public Bmi(double w,double h){
setWeight(w);
setHeight(h);
}
public void setHeight(double ht){
if(ht>=0.9){
height=ht;
}
}
public void setWeight(double wt){
if(wt>=35){
weight=wt;
}
}
public double getWeight(){
return weight;
}
public double getHeight(){
return height;
}
public double getValue(){
return weight/height/height;
}
public String status(){
double bmi=getValue();
return bmi>24 ? "過重" : bmi<18.5 ? "過輕" : "標準";
}
public String toString(){
return String.format("<html>BMI值:%.2f<br>體重狀態:%s<html>",getValue(),status());
}
}
class BmiGui2 {
JFrame frame;
JLabel lb1,lb2,lb3;
JTextField tf1,tf2;
JButton btn1,btn2;
public BmiGui2(){
frame=new JFrame("BMI");
frame.setLayout(null);
lb1=new JLabel("請輸入體重(公斤)");
lb1.setBounds(5,5,100,20);
frame.add(lb1);
tf1=new JTextField("0");
tf1.setBounds(110,5,100,20);
frame.add(tf1);
lb2=new JLabel("請輸入身高(公分)");
lb2.setBounds(5,30,100,20);
frame.add(lb2);
tf2=new JTextField("0");
tf2.setBounds(110,30,100,20);
frame.add(tf2);
btn1=new JButton("求BMI");
btn1.setBounds(5,55,100,20);
frame.add(btn1);
btn1.addActionListener(
(ActionEvent e)->{
double kg=0,m=0;
try{
kg = Double.parseDouble(tf1.getText());
m = Double.parseDouble(tf2.getText())/100;
lb3.setText(new Bmi(kg,m).toString());
btn1.setEnabled(false);
btn2.setEnabled(true);
}catch(NumberFormatException ex){
JOptionPane.showMessageDialog(null,"NO Enter!!","Enter Error",JOptionPane.ERROR_MESSAGE);
}
}
);
btn2=new JButton("清除");
btn2.setBounds(110,55,100,20);
frame.add(btn2);
btn2.addActionListener(
(ActionEvent e)->{
tf1.setText("0");
tf2.setText("0");
lb3.setText("");
btn1.setEnabled(true);
btn2.setEnabled(false);
}
);
btn2.setEnabled(false);
lb3=new JLabel("");
lb3.setBounds(5,80,200,40);
frame.add(lb3);
frame.setSize(400,400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] arg){
new BmiGui2();
}
/*
ActionListener listener = new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==btn1){
double kg=0,m=0;
try{
kg = Double.parseDouble(tf1.getText());
m = Double.parseDouble(tf2.getText())/100;
lb3.setText(new Bmi(kg,m).toString());
btn1.setEnabled(false);
btn2.setEnabled(true);
}catch(NumberFormatException ex){
JOptionPane.showMessageDialog(null,"NO Enter!!","Enter Error",JOptionPane.ERROR_MESSAGE);
}
}else if(e.getSource()==btn2){
tf1.setText("0");
tf2.setText("0");
lb3.setText("");
btn1.setEnabled(true);
btn2.setEnabled(false);
}
}
};
*/
}