CI 2019 von Daniel, Eugen und Michael
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
7.4 KiB

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyAdapter;
  8. import java.awt.event.KeyEvent;
  9. public class FitnessTest_M extends JFrame {
  10. JFrame frame = null;
  11. JMenuItem menuItem_1 = null;
  12. JMenuBar menuBar = null;
  13. JMenu menu = null;
  14. JLabel height_label = null;
  15. JTextField height_tf = null;
  16. JLabel weight_label = null;
  17. JTextField weight_tf = null;
  18. JLabel height_ungueltig = null;
  19. JLabel weight_ungueltig = null;
  20. JButton bmi_rechnen = null;
  21. JLabel question_label1 = null;
  22. JLabel question_label2 = null;
  23. JLabel question_label3 = null;
  24. JLabel question_label4 = null;
  25. JTextField question_tf1 = null;
  26. JTextField question_tf2 = null;
  27. JTextField question_tf3 = null;
  28. JTextField question_tf4 = null;
  29. JButton bmi_confirm = null;
  30. JButton question1_confirm = null;
  31. JButton question2_confirm = null;
  32. JButton question3_confirm = null;
  33. JButton question4_confirm = null;
  34. public FitnessTest_M() {
  35. frame = new JFrame("Fitness Test");
  36. frame.setLayout(null);
  37. frame.setSize(1920, 1080);
  38. menuBar = new JMenuBar();
  39. frame.setJMenuBar(menuBar);
  40. menu = new JMenu("Option");
  41. menuItem_1 = new JMenuItem("Exit");
  42. menuBar.add(menu);
  43. menu.add(menuItem_1);
  44. menuItem_1.addActionListener(new ActionListener() {
  45. public void actionPerformed(ActionEvent a) {
  46. if(a.getSource() == menuItem_1) {
  47. System.exit(0);
  48. }
  49. }
  50. });
  51. height_ungueltig = new JLabel();
  52. height_ungueltig.setForeground(Color.red);
  53. height_label = new JLabel("Geben sie hier ihre Koerpergroesse ein (in Meter)");
  54. height_label.setBounds(10, 10, 280, 30);
  55. height_tf = new JTextField(40);
  56. height_tf.setBounds(10, 40, 280, 30);
  57. height_tf.addKeyListener(new KeyAdapter() {
  58. @Override
  59. public void keyPressed(KeyEvent e) {
  60. try {
  61. float height = Float.parseFloat(height_tf.getText());
  62. height_ungueltig.setText("");
  63. } catch (NumberFormatException e1) {
  64. height_ungueltig.setText("Ungueltige Nummer");
  65. height_ungueltig.setBounds(10, 70, 280, 20);
  66. }
  67. }
  68. });
  69. weight_ungueltig = new JLabel();
  70. weight_ungueltig.setForeground(Color.red);
  71. weight_label = new JLabel("Geben sie hier ihr Gewicht ein (in Kilogramm)");
  72. weight_label.setBounds(320, 10, 300, 30);
  73. weight_tf = new JTextField(40);
  74. weight_tf.setBounds(320, 40, 300, 30);
  75. weight_tf.addKeyListener(new KeyAdapter() {
  76. @Override
  77. public void keyPressed(KeyEvent w) {
  78. try {
  79. float weight = Float.parseFloat(weight_tf.getText());
  80. weight_ungueltig.setText("");
  81. } catch (NumberFormatException w1) {
  82. weight_ungueltig.setText("Ungueltige Nummer");
  83. weight_ungueltig.setBounds(320, 70, 300, 20);
  84. }
  85. }
  86. });
  87. question_label1 = new JLabel("Trainieren sie: \n A: Regelmässig B: Ab und zu C: Gar nicht");
  88. question_label1.setBounds(660, 10, 500, 60);
  89. question_label2 = new JLabel("Essen sie: \n A: 1-2 mal am Tag B: 3-4 mal am Tag C: 5+ mal am Tag");
  90. question_label2.setBounds(1170, 10, 500, 60);
  91. question_label3 = new JLabel("Rauchen sie?: \n A: Sehr oft B: Ab und zu C: Nie");
  92. question_label3.setBounds(510, 150, 500, 60);
  93. question_label4 = new JLabel("Trinken sie regelmässig Alkohol?: \n A: Sehr oft B: Ab und zu C: Nie");
  94. question_label4.setBounds(10, 150, 500, 60);
  95. question_tf1 = new JTextField(10);
  96. question_tf1.setBounds(660, 70, 500, 30);
  97. question_tf2 = new JTextField(10);
  98. question_tf2.setBounds(1170, 70, 500, 30);
  99. question_tf3 = new JTextField(10);
  100. question_tf3.setBounds(520, 210, 500, 30);
  101. question_tf4 = new JTextField(10);
  102. question_tf4.setBounds(10, 210, 500, 30);
  103. bmi_confirm = new JButton("BMI Bestätigen");
  104. bmi_confirm.setBounds(400, 90, 150, 30);
  105. question1_confirm = new JButton("Bestätigen");
  106. question1_confirm.setBounds(660, 100, 150, 30);
  107. question2_confirm = new JButton("Bestätigen");
  108. question2_confirm.setBounds(1170, 100, 150, 30);
  109. question3_confirm = new JButton("Bestätigen");
  110. question3_confirm.setBounds(520, 240, 150, 30);
  111. question4_confirm = new JButton("Bestätigen");
  112. question4_confirm.setBounds(10, 240, 150, 30);
  113. frame.add(height_label);
  114. frame.add(height_tf);
  115. frame.add(height_ungueltig);
  116. frame.add(weight_label);
  117. frame.add(weight_tf);
  118. frame.add(weight_ungueltig);
  119. frame.add(question_label1);
  120. frame.add(question_label2);
  121. frame.add(question_label3);
  122. frame.add(question_label4);
  123. frame.add(question_tf1);
  124. frame.add(question_tf2);
  125. frame.add(question_tf3);
  126. frame.add(question_tf4);
  127. frame.add(bmi_confirm);
  128. frame.add(question1_confirm);
  129. frame.add(question2_confirm);
  130. frame.add(question3_confirm);
  131. frame.add(question4_confirm);
  132. bmi_rechnen = new JButton("BMI Berechnen");
  133. bmi_rechnen.setBounds(100, 90, 150, 30);
  134. bmi_rechnen.addActionListener(new ActionListener() {
  135. public void actionPerformed(ActionEvent b) {
  136. if(b.getSource() == bmi_rechnen) {
  137. float height = Float.parseFloat(height_tf.getText());
  138. float weight = Float.parseFloat(weight_tf.getText());
  139. int bmi = calculateBmi(height, weight);
  140. if(bmi < 19) {
  141. JOptionPane.showMessageDialog(null,
  142. "Ihr BMI: " + bmi + " \nUntergewicht!",
  143. "Ergebnis",
  144. JOptionPane.WARNING_MESSAGE);
  145. }
  146. if(bmi >= 19 && bmi <= 25) {
  147. JOptionPane.showMessageDialog(null,
  148. "Ihr BMI: " + bmi + " \nNormalgewicht!",
  149. "Ergebnis",
  150. JOptionPane.WARNING_MESSAGE);
  151. }
  152. if(bmi > 26 && bmi <= 30) {
  153. JOptionPane.showMessageDialog(null,
  154. "Ihr BMI: " + bmi + " \nLeichtes Übergewicht!",
  155. "Ergebnis",
  156. JOptionPane.WARNING_MESSAGE);
  157. }
  158. if(bmi >= 31) {
  159. JOptionPane.showMessageDialog(null,
  160. "Ihr BMI: " + bmi + " \nÜbergewicht!",
  161. "Ergebnis",
  162. JOptionPane.WARNING_MESSAGE);
  163. }
  164. }
  165. }
  166. });
  167. frame.add(bmi_rechnen);
  168. frame.setVisible(true);
  169. }
  170. public static void main(String[] args) {
  171. FitnessTest_M M = new FitnessTest_M();
  172. }
  173. public int calculateBmi(float height, float weight) {
  174. return (int) (weight / (height*height));
  175. }
  176. }