Problem in displaying results in label in java [closed]

0
package cursoemvideo;

/**
 *
 * @author DANIEL
 */

public class TelaCalculadora extends javax.swing.JFrame {

    /**
     * Creates new form TelaCalculadora
     */
    public TelaCalculadora() {
        initComponents();
        pnpPainel.setVisible(false);
    }

    /**
     * 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() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        spnValor = new javax.swing.JSpinner();
        btnCalc = new javax.swing.JButton();
        pnpPainel = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        lblRes = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lblCubo = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        lblRaiz = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        lblRaiz2 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        lblAbs = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("SUPER CALCULADORA");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        jLabel2.setText("Informe um Valor");

        spnValor.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        spnValor.setModel(new javax.swing.SpinnerNumberModel(0, -50, 50, 1));

        btnCalc.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        btnCalc.setText("Calcular");
        btnCalc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCalcActionPerformed(evt);
            }
        });

        pnpPainel.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Resto da divisão por 2");

        lblRes.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        lblRes.setText("0");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setText("Elevado ao Cubo");

        lblCubo.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        lblCubo.setText("0");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel7.setText("Raiz Quadrada");

        lblRaiz.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        lblRaiz.setText("0");

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel9.setText("Raiz Cubica");

        lblRaiz2.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        lblRaiz2.setText("0");

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel11.setText("Valor Absoluto");

        lblAbs.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        lblAbs.setText("0");

        javax.swing.GroupLayout pnpPainelLayout = new javax.swing.GroupLayout(pnpPainel);
        pnpPainel.setLayout(pnpPainelLayout);
        pnpPainelLayout.setHorizontalGroup(
            pnpPainelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnpPainelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnpPainelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(pnpPainelLayout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblAbs))
                    .addGroup(pnpPainelLayout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblRaiz2))
                    .addGroup(pnpPainelLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(73, 73, 73)
                        .addComponent(lblRes))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnpPainelLayout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblCubo))
                    .addGroup(pnpPainelLayout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblRaiz)))
                .addContainerGap(55, Short.MAX_VALUE))
        );
        pnpPainelLayout.setVerticalGroup(
            pnpPainelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnpPainelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnpPainelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lblRes))
                .addGap(18, 18, 18)
                .addGroup(pnpPainelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(lblCubo))
                .addGap(18, 18, 18)
                .addGroup(pnpPainelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(lblRaiz))
                .addGap(18, 18, 18)
                .addGroup(pnpPainelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(lblRaiz2))
                .addGap(18, 18, 18)
                .addGroup(pnpPainelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(lblAbs))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(94, 94, 94)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(19, 19, 19)
                        .addComponent(spnValor, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(btnCalc)
                .addGap(45, 45, 45))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnpPainel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(spnValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCalc))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addComponent(pnpPainel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22))
        );

        pack();
    }// </editor-fold>                        

    private void btnCalcActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        // Aqui abaixo são os calculos para execução do programa
        pnpPainel.setVisible(true);
        int valor = Integer.parseInt(spnValor.getModel().toString());
        int resto = valor%2;
        lblRes.setText(Integer.toString(resto));

        double cubo = Math.pow(resto, 3);
        lblCubo.setText(Double.toString(cubo));

        double raizq = Math.sqrt(valor);
        lblRaiz.setText(Double.toString(raizq));

        double raizcubo = Math.cbrt(valor);
        lblRaiz2.setText(Double.toString(raizcubo)); 

        int abs = Math.abs(valor);      
        lblAbs.setText(Integer.toString(abs));


    }                                       

    /**
     * @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 ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaCalculadora.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaCalculadora.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaCalculadora.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaCalculadora.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 TelaCalculadora().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnCalc;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel lblAbs;
    private javax.swing.JLabel lblCubo;
    private javax.swing.JLabel lblRaiz;
    private javax.swing.JLabel lblRaiz2;
    private javax.swing.JLabel lblRes;
    private javax.swing.JPanel pnpPainel;
    private javax.swing.JSpinner spnValor;
    // End of variables declaration                   
}

The results of the equations is to appear in the labels that are 0.

    
asked by anonymous 18.09.2018 / 19:12

1 answer

1

Change this line

int valor = Integer.parseInt(spnValor.getModel().toString());

by this:

int valor = (Integer)spnValor.getModel().getValue();

What you're doing is retrieving SpinnerModel and converting it to String. Obviously this object is not a numeric value that can be converted to integer.

What you should do is to retrieve the value selected in SpinnerModel through the getValue() " and do the conversion via cast, since in the constructor of your SpinnerNumberModel you defined integer bounds. So it's okay to do the cast in this case.

    
18.09.2018 / 20:06