Results 1 to 2 of 2
  1. #1
    Uchiha is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default First time GUI issues

    So this is my first time using the GUI and I'm having some trouble. When ever I try to run my program and I click calculate the whole program freezes and it doesn't give me any errors so I can't figure out what's wrong with it. I've talked to people I know and they have said that they don't use a while loop but when I take the while loop out of the program it just doesn't run. Now this is my first time using the GUI interface so I might have things completely wrong. Do you guys seee anything wrong that could fix this and and tips or advice is welcome.

    Here is my code:

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package my.mortgagecalculation3;
    
    import java.text.NumberFormat;
    import javax.swing.JOptionPane;
    import javax.swing.JFrame;
    
    /**
     *
     * @author Akira
     */
    public class MortgageCalculation3UI extends javax.swing.JFrame {
    
        /**
         * Creates new form MortgageCalculation3UI
         */
        public MortgageCalculation3UI() {
            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() {
    
            jPanel1 = new javax.swing.JPanel();
            javax.swing.JLabel lblMortgageCalculator = new javax.swing.JLabel();
            lblLoanAmount = new javax.swing.JLabel();
            lblInterestRate = new javax.swing.JLabel();
            lblNumberYears = new javax.swing.JLabel();
            lblMonthly = new javax.swing.JLabel();
            txtLoanAmount = new javax.swing.JTextField();
            txtInterestRate = new javax.swing.JTextField();
            txtNumberYears = new javax.swing.JTextField();
            txtMonthlyPayment = new javax.swing.JTextField();
            btnCalculate = new javax.swing.JButton();
            btnReset = new javax.swing.JButton();
            btnExit = new javax.swing.JButton();
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 109, Short.MAX_VALUE)
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 114, Short.MAX_VALUE)
            );
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            lblMortgageCalculator.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
            lblMortgageCalculator.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            lblMortgageCalculator.setText("Mortgage Calculator");
    
            lblLoanAmount.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
            lblLoanAmount.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
            lblLoanAmount.setLabelFor(lblLoanAmount);
            lblLoanAmount.setText("Loan Amount :");
    
            lblInterestRate.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
            lblInterestRate.setText("Interest Rate :");
            lblInterestRate.setToolTipText("");
            lblInterestRate.setName("lblInterestRate"); // NOI18N
    
            lblNumberYears.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
            lblNumberYears.setText("Years to pay :");
            lblNumberYears.setToolTipText("");
    
            lblMonthly.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
            lblMonthly.setText("Monthly Payment :");
    
            txtLoanAmount.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
            txtLoanAmount.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    txtLoanAmountActionPerformed(evt);
                }
            });
    
            txtInterestRate.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
    
            txtNumberYears.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
    
            txtMonthlyPayment.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
    
            btnCalculate.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
            btnCalculate.setText("Calculate");
            btnCalculate.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnCalculateActionPerformed(evt);
                }
            });
    
            btnReset.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
            btnReset.setText("Reset");
            btnReset.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnResetActionPerformed(evt);
                }
            });
    
            btnExit.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
            btnExit.setText("Exit");
            btnExit.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnExitActionPerformed(evt);
                }
            });
    
            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(lblMortgageCalculator, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(23, 23, 23)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(lblMonthly)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtMonthlyPayment))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(lblInterestRate, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(lblLoanAmount)
                                            .addComponent(lblNumberYears, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(34, 34, 34)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(txtLoanAmount)
                                            .addComponent(txtInterestRate)
                                            .addComponent(txtNumberYears, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE))))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(btnCalculate, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(btnReset, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGap(0, 28, Short.MAX_VALUE)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(19, 19, 19)
                    .addComponent(lblMortgageCalculator, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblLoanAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtLoanAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblInterestRate, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtInterestRate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblNumberYears, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtNumberYears, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(45, 45, 45)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblMonthly, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtMonthlyPayment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(btnCalculate, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
                        .addComponent(btnExit, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnReset, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(27, 27, 27))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {                                        
            System.exit(0);
        }                                       
    
        private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {                                         
            txtLoanAmount.setText("");
            txtInterestRate.setText("");
            txtNumberYears.setText("");
        }                                        
    
        private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {                                             
           
            JFrame frame = new JFrame();        
            double loanAmount = 0;
            double interestRate = 0;
            double numberYears = 0;
            double months;
            double monthlyPayment;
            double numerator;
            double denominator;
            double formula;
            String runAgain = "y";
            
                
            while (runAgain.equals("y")) {
                try {
                    loanAmount = 0;
                    interestRate = 0;
                    numberYears = 0;
         
            loanAmount = Double.parseDouble(txtLoanAmount.getText());
    
            interestRate = Double.parseDouble(txtInterestRate.getText());
    
            numberYears = Double.parseDouble(txtNumberYears.getText());
               
                        //if the user enters a negative number print out a error message, if not, continue calculations
         if ((loanAmount <= 0) || (interestRate <= 0) || (numberYears <= 0)) {
             
             JOptionPane.showMessageDialog(frame,
                     "You must enter positive numerical data!",
                     "Invalid Data!",
                      JOptionPane.ERROR_MESSAGE);
     
            } else {
                //convert the interest rate
               interestRate = interestRate / 100 / 12;
               
               //the number of years must be converted to months
               months = numberYears * 12;
    
               //numerator of the monthly payment formula
               numerator = (Math.pow(1 + interestRate, months));
               
               //denominator of the monthly payment formula
               denominator = ((numerator)-1);
               
               //the formula equals the numerator divided by the denominator
               formula = ( numerator / denominator );
               
               //monthly payment calculation
                monthlyPayment = (interestRate * loanAmount * formula);
    
                 //sytem output
                NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
                defaultFormat.format(monthlyPayment);
                txtMonthlyPayment.setText(Double.toString(monthlyPayment));
    
           }
                } catch (NumberFormatException e) {   
                    JOptionPane.showMessageDialog(frame,
                     "You must enter positive numerical data!",
                     "Invalid Data!",
                      JOptionPane.ERROR_MESSAGE);
    
                    
                }
    
                    }                       
             
        }                                            
    
        private void txtLoanAmountActionPerformed(java.awt.event.ActionEvent evt) {                                              
            // TODO add your handling code here:
        }                                             
    
        /**
         * @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(MortgageCalculation3UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(MortgageCalculation3UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(MortgageCalculation3UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(MortgageCalculation3UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton btnCalculate;
        private javax.swing.JButton btnExit;
        private javax.swing.JButton btnReset;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JLabel lblInterestRate;
        private javax.swing.JLabel lblLoanAmount;
        private javax.swing.JLabel lblMonthly;
        private javax.swing.JLabel lblNumberYears;
        private javax.swing.JTextField txtInterestRate;
        private javax.swing.JTextField txtLoanAmount;
        private javax.swing.JTextField txtMonthlyPayment;
        private javax.swing.JTextField txtNumberYears;
        // End of variables declaration                   
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default Re: First time GUI issues

    Sounds like an infinite loop. Does the code that is executed when the button have a while() loop with a condition that is ALWAYS true?

    Working with IDE generated code is a pain. I'll leave the search to you. Add some calls to the println method inside the loop that prints out the value of the variable that controls the looping to see if it is ever changed so the loop can end.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Code continues on for a long time - Recursion Issues
    By Captain Forge in forum New To Java
    Replies: 1
    Last Post: 03-30-2013, 04:50 PM
  2. Replies: 5
    Last Post: 02-29-2012, 10:07 AM
  3. Replies: 3
    Last Post: 12-07-2011, 07:00 AM
  4. Replies: 3
    Last Post: 08-23-2011, 12:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •