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.

130 lines
4.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. 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 = (int) (weight / (height*height));
  97. JOptionPane.showMessageDialog(null,
  98. "Ihr BMI: " + bmi,
  99. "Ergebnis",
  100. JOptionPane.WARNING_MESSAGE);
  101. }
  102. }
  103. });
  104. frame.add(bmi_rechnen);
  105. frame.setVisible(true);
  106. }
  107. public static void main(String[] args) {
  108. FitnessTest_M M = new FitnessTest_M();
  109. }
  110. }