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.

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