我的GUI代码出了什么问题?

时间:2013-11-04 16:29:49

标签: java swing user-interface

很抱歉代码很多,但我觉得有必要。我想知道我的代码有什么问题。在GUI界面方面,我是初学者。当我运行我的程序时,窗口弹出,当我陈词滥调时计算整个程序冻结,我甚至无法弄清楚它有什么问题。你们看到我可能做错了吗

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>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new MortgageCalculation3UI().setVisible(true);
        }
    });
}
// 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                   
}

1 个答案:

答案 0 :(得分:1)

问题在于您的btnCalculateActionPerformed(java.awt.event.ActionEvent evt)方法:

String runAgain = "y";

while (runAgain.equals("y")) {
    // do stuff here
}

while循环条件始终评估为true。计算完成后,您必须将变量更改为其他变量。对于这种情况,我认为最好使用boolean代替String。我想知道为什么需要这样做:一旦完成计算,再次运行就没有意义了。将结果呈现给用户。如果他想再次运行计算,他会再次按下按钮。

请注意,您正在UI线程中运行计算,这是一件坏事:如果它们花费的时间太长,您的应用程序会冻结。也许你可以take a look at Swing Workers