Problem displaying results in java label

Asked

Viewed 177 times

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                   
}

inserir a descrição da imagem aqui

The results of the equations are to appear on the labels that are with 0.

  • 4

    You can post the code as text instead of image?

  • 4

    Provide a [mcve] so that we can test. Posting image doesn’t help at all to understand the problem.

  • 3

    It would be interesting if you explained what you wish to do.

  • posted codes

  • Miss explain what you are trying to do, edit again by adding this.

1 answer

1


Change this line

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

for this:

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

What you’re doing is recovering the SpinnerModel and converting it to String. Obviously this object is not a numerical value that can be converted to integer.

What you should do is recover the selected value in SpinnerModel through the method getValue() and do the conversion via cast, since in your constructor SpinnerNumberModel you have set boundaries of the whole type. Therefore, it is no problem to do the cast direct in this case.

inserir a descrição da imagem aqui

  • That brother thanks there,!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.