Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java NetBeans Error

  • 16-06-2013 8:11pm
    #1
    Registered Users, Registered Users 2 Posts: 80 ✭✭


    Guys,

    I'm fairly new to java and NetBeans especially. I'm trying to do a small Calculator GUI (Just Adding and subtracting Numbers) But i keep getting this error alongside 2 parts of my code.. (Highlighted in Red)

    The Error says: Method getText in classs jLabel cannot be applied to given types;

    required: no arguments
    found: String
    reason: actual and formal argument differ in length


    Sorry if this is a simple fix, but its doing my head in knowing its probably something stupid i'm missing.

    Thanks in advance for any help!
    import javax.swing.JOptionPane;
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author Robert
     */
    public class CalculatorForm extends javax.swing.JFrame {
    
        /**
         * Creates new form CalculatorForm
         */
        public CalculatorForm() {
            initComponents();
        }
    
        /**
         * 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() {
    
            firstNumberText = new javax.swing.JTextField();
            secondNumberText = new javax.swing.JTextField();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            plusButton = new javax.swing.JButton();
            minusButton = new javax.swing.JButton();
            answerLabel = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Simple Calculator");
    
            jLabel1.setText("First number");
    
            jLabel2.setText("Second number");
    
            plusButton.setText("+");
            plusButton.setToolTipText("Add");
            plusButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    plusButtonActionPerformed(evt);
                }
            });
    
            minusButton.setText("-");
            minusButton.setToolTipText("Subtract");
            minusButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    minusButtonActionPerformed(evt);
                }
            });
    
            answerLabel.setFont(new java.awt.Font("Script MT Bold", 0, 24)); // NOI18N
            answerLabel.setText("The Answer is:");
            answerLabel.setToolTipText("null");
    
            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(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(plusButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 109, Short.MAX_VALUE)
                            .addComponent(minusButton))
                        .addComponent(secondNumberText)
                        .addComponent(firstNumberText))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(answerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2)
                        .addComponent(jLabel1))
                    .addContainerGap(54, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addGap(13, 13, 13)
                    .addComponent(firstNumberText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(12, 12, 12)
                    .addComponent(jLabel2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(secondNumberText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(33, 33, 33)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(plusButton)
                        .addComponent(minusButton))
                    .addGap(34, 34, 34)
                    .addComponent(answerLabel)
                    .addContainerGap(71, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
        private void plusButtonActionPerformed(java.awt.event.ActionEvent evt) {
            int number1, number2;
    
            try {
                number1 = Integer.parseInt(
                        this.firstNumberText.getText());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Bad First Number", "ERROR", JOptionPane.ERROR_MESSAGE);
                return;
            }
    
            try {
                number2 = Integer.parseInt(
                        this.secondNumberText.getText());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Bad Second Number", "ERROR", JOptionPane.ERROR_MESSAGE);
                return;
            }
    
            int answer = number1 + number2;
            [COLOR="red"]this.answerLabel.getText([/COLOR]
                    "The answer is:  " + answer);
        
        }
    
        private void minusButtonActionPerformed(java.awt.event.ActionEvent evt) {
            int number1, number2;
    
            try {
                number1 = Integer.parseInt(
                        this.firstNumberText.getText());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Bad First Number", "ERROR", JOptionPane.ERROR_MESSAGE);
                return;
            }
    
            try {
                number2 = Integer.parseInt(
                        this.secondNumberText.getText());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Bad Second Number", "ERROR", JOptionPane.ERROR_MESSAGE);
                return;
            }
    
            int answer = number1 - number2;
           [COLOR="Red"] this.answerLabel.getText([/COLOR]
                    "The answer is: " + answer );
        }
    
        
        
        
          
        /**
         * @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(CalculatorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(CalculatorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(CalculatorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(CalculatorForm.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 CalculatorForm().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JLabel answerLabel;
        private javax.swing.JTextField firstNumberText;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JButton minusButton;
        private javax.swing.JButton plusButton;
        private javax.swing.JTextField secondNumberText;
        // End of variables declaration
    }
    
    Tagged:


Comments

  • Closed Accounts Posts: 2,113 ✭✭✭SilverScreen


    Should you not be using this.answerLabel.setText("The answer is: " + answer ); to output the answer to the answerLabel?


Advertisement