Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author rukes
- */
- public class Vzorecek extends javax.swing.JFrame {
- /**
- * Creates new form Vzorecek
- */
- public Vzorecek() {
- initComponents();
- setLocationRelativeTo(null);
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- hmotnost = new javax.swing.JTextField();
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- vyska = new javax.swing.JTextField();
- jLabel3 = new javax.swing.JLabel();
- jLabel4 = new javax.swing.JLabel();
- jButton1 = new javax.swing.JButton();
- vystup = new javax.swing.JTextField();
- jLabel5 = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("Výpočet BMI");
- setMinimumSize(new java.awt.Dimension(350, 200));
- setResizable(false);
- jLabel1.setText("Zadej hmotnost:");
- jLabel2.setText("Zadej výšku:");
- jLabel3.setText("kg");
- jLabel4.setText("cm");
- jButton1.setText("Počítej");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- vystup.setEditable(false);
- jLabel5.setText("BMI:");
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel1)
- .addComponent(jLabel2)
- .addComponent(jButton1))
- .addGap(51, 51, 51)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel5)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(vystup, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(vyska, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
- .addComponent(hmotnost))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel3)
- .addComponent(jLabel4))))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(hmotnost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel1)
- .addComponent(jLabel3))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel2)
- .addComponent(vyska, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel4))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jButton1)
- .addComponent(vystup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel5))
- .addGap(28, 28, 28))
- );
- setBounds(0, 0, 320, 196);
- }// </editor-fold>
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- String vstupHmotnost = hmotnost.getText();
- if(vstupHmotnost == null || vstupHmotnost.isEmpty()){
- JOptionPane.showMessageDialog(this, "Nezadal jsi hmotnost", "Chyba!", JOptionPane.ERROR_MESSAGE);
- return;
- }
- double hmot = 0;
- try {
- hmot = Double.parseDouble(vstupHmotnost.replace(",", "."));
- } catch (Exception ex){
- JOptionPane.showMessageDialog(this, "Nezadal jsi správně hmotnost", "Chyba!", JOptionPane.ERROR_MESSAGE);
- return;
- }
- String vstupVyska = vyska.getText();
- if(vstupVyska == null || vstupVyska.isEmpty()){
- JOptionPane.showMessageDialog(this, "Nezadal jsi výšku", "Chyba!", JOptionPane.ERROR_MESSAGE);
- return;
- }
- double vys = 0;
- try{
- vys = Double.parseDouble(vstupVyska.replace(",", "."));
- } catch (Exception ex){
- JOptionPane.showMessageDialog(this, "Nezadal jsi správně výšku", "Chyba!", JOptionPane.ERROR_MESSAGE);
- return;
- }
- if(vys <= 0){
- JOptionPane.showMessageDialog(this, "Nelze spočítat!", "Chyba!", JOptionPane.ERROR_MESSAGE);
- return;
- }
- vys = vys / 100;
- double bmi = hmot / (vys * vys);
- String vysledek = String.format("%.2f", bmi);
- vystup.setText(vysledek);
- JOptionPane.showMessageDialog(this, "Tvoje BMI: " + vysledek, "Výpočet", JOptionPane.INFORMATION_MESSAGE);
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Windows".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(Vzorecek.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(Vzorecek.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(Vzorecek.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(Vzorecek.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new Vzorecek().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JTextField hmotnost;
- private javax.swing.JButton jButton1;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JLabel jLabel4;
- private javax.swing.JLabel jLabel5;
- private javax.swing.JTextField vyska;
- private javax.swing.JTextField vystup;
- // End of variables declaration
- }
Add Comment
Please, Sign In to add comment