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.

172 lines
6.2 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. public FitnessTest_M() {
  29. frame = new JFrame("Fitness Test");
  30. frame.setLayout(null);
  31. frame.setSize(1920, 1080);
  32. // panel_1 = new JPanel(new BorderLayout());
  33. // frame.setContentPane(panel_1);
  34. menuBar = new JMenuBar();
  35. frame.setJMenuBar(menuBar);
  36. menu = new JMenu("Option");
  37. menuItem_1 = new JMenuItem("Exit");
  38. menuBar.add(menu);
  39. menu.add(menuItem_1);
  40. menuItem_1.addActionListener(new ActionListener() {
  41. public void actionPerformed(ActionEvent a) {
  42. if(a.getSource() == menuItem_1) {
  43. System.exit(0);
  44. }
  45. }
  46. });
  47. height_ungueltig = new JLabel();
  48. height_ungueltig.setForeground(Color.red);
  49. height_label = new JLabel("Geben sie hier ihre Koerpergroesse ein (in Meter)");
  50. height_label.setBounds(10, 10, 280, 30);
  51. //panel_1.add(height_label);
  52. height_tf = new JTextField(40);
  53. height_tf.setBounds(10, 40, 280, 30);
  54. height_tf.addKeyListener(new KeyAdapter() {
  55. @Override
  56. public void keyPressed(KeyEvent e) {
  57. try {
  58. float height = Float.parseFloat(height_tf.getText());
  59. height_ungueltig.setText("");
  60. } catch (NumberFormatException e1) {
  61. height_ungueltig.setText("Ungueltige Nummer");
  62. height_ungueltig.setBounds(10, 70, 280, 20);
  63. }
  64. }
  65. });
  66. // panel_1.add(height_tf);
  67. weight_ungueltig = new JLabel();
  68. weight_ungueltig.setForeground(Color.red);
  69. weight_label = new JLabel("Geben sie hier ihr Gewicht ein (in Kilogramm)");
  70. weight_label.setBounds(320, 10, 300, 30);
  71. // panel_1.add(weight_label);
  72. weight_tf = new JTextField(40);
  73. weight_tf.setBounds(320, 40, 300, 30);
  74. weight_tf.addKeyListener(new KeyAdapter() {
  75. @Override
  76. public void keyPressed(KeyEvent w) {
  77. try {
  78. float weight = Float.parseFloat(weight_tf.getText());
  79. weight_ungueltig.setText("");
  80. } catch (NumberFormatException w1) {
  81. weight_ungueltig.setText("Ungueltige Nummer");
  82. weight_ungueltig.setBounds(320, 70, 300, 20);
  83. }
  84. }
  85. });
  86. // panel_1.add(weight_tf);
  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, 120, 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, 120, 500, 60);
  95. frame.add(height_label);
  96. frame.add(height_tf);
  97. frame.add(height_ungueltig);
  98. frame.add(weight_label);
  99. frame.add(weight_tf);
  100. frame.add(weight_ungueltig);
  101. frame.add(question_label1);
  102. frame.add(question_label2);
  103. frame.add(question_label3);
  104. frame.add(question_label4);
  105. bmi_rechnen = new JButton("BMI berechnen");
  106. bmi_rechnen.setBounds(200, 90, 200, 30);
  107. bmi_rechnen.addActionListener(new ActionListener() {
  108. public void actionPerformed(ActionEvent b) {
  109. if(b.getSource() == bmi_rechnen) {
  110. float height = Float.parseFloat(height_tf.getText());
  111. float weight = Float.parseFloat(weight_tf.getText());
  112. int bmi = calculateBmi(height, weight);
  113. if(bmi < 19) {
  114. JOptionPane.showMessageDialog(null,
  115. "Ihr BMI: " + bmi + " \nUntergewicht!",
  116. "Ergebnis",
  117. JOptionPane.WARNING_MESSAGE);
  118. }
  119. if(bmi >= 19 && bmi <= 25) {
  120. JOptionPane.showMessageDialog(null,
  121. "Ihr BMI: " + bmi + " \nNormalgewicht!",
  122. "Ergebnis",
  123. JOptionPane.WARNING_MESSAGE);
  124. }
  125. if(bmi > 26 && bmi <= 30) {
  126. JOptionPane.showMessageDialog(null,
  127. "Ihr BMI: " + bmi + " \nLeichtes Übergewicht!",
  128. "Ergebnis",
  129. JOptionPane.WARNING_MESSAGE);
  130. }
  131. if(bmi >= 31) {
  132. JOptionPane.showMessageDialog(null,
  133. "Ihr BMI: " + bmi + " \nÜbergewicht!",
  134. "Ergebnis",
  135. JOptionPane.WARNING_MESSAGE);
  136. }
  137. }
  138. }
  139. });
  140. frame.add(bmi_rechnen);
  141. frame.setVisible(true);
  142. }
  143. public static void main(String[] args) {
  144. FitnessTest_M M = new FitnessTest_M();
  145. }
  146. public int calculateBmi(float height, float weight) {
  147. return (int) (weight / (height*height));
  148. }
  149. }