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.

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