Rukes

Untitled

May 11th, 2020
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.20 KB | None | 0 0
  1.  
  2. import javax.swing.JOptionPane;
  3.  
  4. /*
  5.  * To change this license header, choose License Headers in Project Properties.
  6.  * To change this template file, choose Tools | Templates
  7.  * and open the template in the editor.
  8.  */
  9.  
  10. /**
  11.  *
  12.  * @author rukes
  13.  */
  14. public class Vzorecek extends javax.swing.JFrame {
  15.  
  16.     /**
  17.      * Creates new form Vzorecek
  18.      */
  19.     public Vzorecek() {
  20.         initComponents();
  21.         setLocationRelativeTo(null);
  22.     }
  23.  
  24.     /**
  25.      * This method is called from within the constructor to initialize the form.
  26.      * WARNING: Do NOT modify this code. The content of this method is always
  27.      * regenerated by the Form Editor.
  28.      */
  29.     @SuppressWarnings("unchecked")
  30.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  31.     private void initComponents() {
  32.  
  33.         hmotnost = new javax.swing.JTextField();
  34.         jLabel1 = new javax.swing.JLabel();
  35.         jLabel2 = new javax.swing.JLabel();
  36.         vyska = new javax.swing.JTextField();
  37.         jLabel3 = new javax.swing.JLabel();
  38.         jLabel4 = new javax.swing.JLabel();
  39.         jButton1 = new javax.swing.JButton();
  40.         vystup = new javax.swing.JTextField();
  41.         jLabel5 = new javax.swing.JLabel();
  42.  
  43.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  44.         setTitle("Výpočet BMI");
  45.         setMinimumSize(new java.awt.Dimension(350, 200));
  46.         setResizable(false);
  47.  
  48.         jLabel1.setText("Zadej hmotnost:");
  49.  
  50.         jLabel2.setText("Zadej výšku:");
  51.  
  52.         jLabel3.setText("kg");
  53.  
  54.         jLabel4.setText("cm");
  55.  
  56.         jButton1.setText("Počítej");
  57.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  58.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  59.                 jButton1ActionPerformed(evt);
  60.             }
  61.         });
  62.  
  63.         vystup.setEditable(false);
  64.  
  65.         jLabel5.setText("BMI:");
  66.  
  67.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  68.         getContentPane().setLayout(layout);
  69.         layout.setHorizontalGroup(
  70.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  71.             .addGroup(layout.createSequentialGroup()
  72.                 .addContainerGap()
  73.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  74.                     .addComponent(jLabel1)
  75.                     .addComponent(jLabel2)
  76.                     .addComponent(jButton1))
  77.                 .addGap(51, 51, 51)
  78.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  79.                     .addGroup(layout.createSequentialGroup()
  80.                         .addComponent(jLabel5)
  81.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  82.                         .addComponent(vystup, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
  83.                     .addGroup(layout.createSequentialGroup()
  84.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  85.                             .addComponent(vyska, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
  86.                             .addComponent(hmotnost))
  87.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  88.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  89.                             .addComponent(jLabel3)
  90.                             .addComponent(jLabel4))))
  91.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  92.         );
  93.         layout.setVerticalGroup(
  94.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  95.             .addGroup(layout.createSequentialGroup()
  96.                 .addGap(18, 18, 18)
  97.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  98.                     .addComponent(hmotnost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  99.                     .addComponent(jLabel1)
  100.                     .addComponent(jLabel3))
  101.                 .addGap(18, 18, 18)
  102.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  103.                     .addComponent(jLabel2)
  104.                     .addComponent(vyska, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  105.                     .addComponent(jLabel4))
  106.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
  107.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  108.                     .addComponent(jButton1)
  109.                     .addComponent(vystup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  110.                     .addComponent(jLabel5))
  111.                 .addGap(28, 28, 28))
  112.         );
  113.  
  114.         setBounds(0, 0, 320, 196);
  115.     }// </editor-fold>                        
  116.  
  117.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  118.         // TODO add your handling code here:
  119.         String vstupHmotnost = hmotnost.getText();
  120.         if(vstupHmotnost == null || vstupHmotnost.isEmpty()){
  121.             JOptionPane.showMessageDialog(this, "Nezadal jsi hmotnost", "Chyba!", JOptionPane.ERROR_MESSAGE);
  122.             return;
  123.         }
  124.         double hmot = 0;
  125.         try {
  126.             hmot = Double.parseDouble(vstupHmotnost.replace(",", "."));
  127.         } catch (Exception ex){
  128.             JOptionPane.showMessageDialog(this, "Nezadal jsi správně hmotnost", "Chyba!", JOptionPane.ERROR_MESSAGE);
  129.             return;
  130.         }
  131.        
  132.         String vstupVyska = vyska.getText();
  133.         if(vstupVyska == null || vstupVyska.isEmpty()){
  134.             JOptionPane.showMessageDialog(this, "Nezadal jsi výšku", "Chyba!", JOptionPane.ERROR_MESSAGE);
  135.             return;
  136.         }
  137.         double vys = 0;
  138.         try{
  139.             vys = Double.parseDouble(vstupVyska.replace(",", "."));
  140.         } catch (Exception ex){
  141.             JOptionPane.showMessageDialog(this, "Nezadal jsi správně výšku", "Chyba!", JOptionPane.ERROR_MESSAGE);
  142.             return;
  143.         }
  144.         if(vys <= 0){
  145.             JOptionPane.showMessageDialog(this, "Nelze spočítat!", "Chyba!", JOptionPane.ERROR_MESSAGE);
  146.             return;
  147.         }
  148.        
  149.         vys = vys / 100;
  150.         double bmi = hmot / (vys * vys);
  151.        
  152.         String vysledek = String.format("%.2f", bmi);
  153.        
  154.         vystup.setText(vysledek);
  155.         JOptionPane.showMessageDialog(this, "Tvoje BMI: " + vysledek, "Výpočet", JOptionPane.INFORMATION_MESSAGE);
  156.     }                                        
  157.  
  158.     /**
  159.      * @param args the command line arguments
  160.      */
  161.     public static void main(String args[]) {
  162.         /* Set the Nimbus look and feel */
  163.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  164.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  165.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  166.          */
  167.         try {
  168.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  169.                 if ("Windows".equals(info.getName())) {
  170.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  171.                     break;
  172.                 }
  173.             }
  174.         } catch (ClassNotFoundException ex) {
  175.             java.util.logging.Logger.getLogger(Vzorecek.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  176.         } catch (InstantiationException ex) {
  177.             java.util.logging.Logger.getLogger(Vzorecek.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  178.         } catch (IllegalAccessException ex) {
  179.             java.util.logging.Logger.getLogger(Vzorecek.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  180.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  181.             java.util.logging.Logger.getLogger(Vzorecek.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  182.         }
  183.         //</editor-fold>
  184.  
  185.         /* Create and display the form */
  186.         java.awt.EventQueue.invokeLater(new Runnable() {
  187.             public void run() {
  188.                 new Vzorecek().setVisible(true);
  189.             }
  190.         });
  191.     }
  192.  
  193.     // Variables declaration - do not modify                    
  194.     private javax.swing.JTextField hmotnost;
  195.     private javax.swing.JButton jButton1;
  196.     private javax.swing.JLabel jLabel1;
  197.     private javax.swing.JLabel jLabel2;
  198.     private javax.swing.JLabel jLabel3;
  199.     private javax.swing.JLabel jLabel4;
  200.     private javax.swing.JLabel jLabel5;
  201.     private javax.swing.JTextField vyska;
  202.     private javax.swing.JTextField vystup;
  203.     // End of variables declaration                  
  204. }
Add Comment
Please, Sign In to add comment