java - 在 Java 中格式化 tempTextField。

标签 java swing user-interface coding-style

好的,这是我最新的挑战。我正在为学校写这篇文章。到目前为止,我已经能够让 GUI 以我想要的方式“基本”运行,但是我在集成格式化文本字段时遇到了一些麻烦。我无法理解的是为什么程序不接受 5.75 的利率 - 它默认为 5 - 当计算模块在我将其逻辑插入我的 GUI 程序之前在不同的程序中运行良好时。话虽如此,我正在发布我的代码,任何输入都会有所帮助。提前致谢!

代码:

import java.text.DecimalFormat;
import java.text.NumberFormat;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MortgageCalculatorGUI.java
 *
 * Created on Dec 18, 2011, 9:01:24 PM
 */
/**
 *
 * @author Roy Kosch
 */
public class MortgageCalculatorGUI extends javax.swing.JFrame {

    /** Creates new form MortgageCalculatorGUI */
    public MortgageCalculatorGUI() {
        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() {

        tempTextField1 = new javax.swing.JTextField();
        tempTextField2 = new javax.swing.JTextField();
        tempTextField3 = new javax.swing.JTextField();
        interestLabel = new javax.swing.JLabel();
        termLabel = new javax.swing.JLabel();
        loanLabel = new javax.swing.JLabel();
        calculateButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        quitButton = new javax.swing.JButton();
        paymentLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Kosch Mortgage Calculator");

        tempTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tempTextField1ActionPerformed(evt);
            }
        });

        tempTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tempTextField2ActionPerformed(evt);
            }
        });

        interestLabel.setText("Interest");

        termLabel.setText("Loan Term");

        loanLabel.setText("Loan Amount");

        calculateButton.setText("Calculate");
        calculateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calculateButtonActionPerformed(evt);
            }
        });

        backButton.setText("Go Back");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        quitButton.setText("QUIT");
        quitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitButtonActionPerformed(evt);
            }
        });

        paymentLabel.setText("Your payment is:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(77, 77, 77)
                        .addComponent(tempTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(interestLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(115, 115, 115)
                        .addComponent(quitButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(77, 77, 77)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(tempTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tempTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(termLabel)
                            .addComponent(loanLabel)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(66, 66, 66)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(paymentLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(calculateButton)
                                .addGap(18, 18, 18)
                                .addComponent(backButton)))))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {backButton, calculateButton, interestLabel, loanLabel, quitButton, tempTextField1, tempTextField2, tempTextField3, termLabel});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tempTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(interestLabel))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tempTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(termLabel))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tempTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(loanLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(paymentLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(calculateButton)
                    .addComponent(backButton))
                .addGap(18, 18, 18)
                .addComponent(quitButton)
                .addGap(39, 39, 39))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {backButton, calculateButton, interestLabel, loanLabel, quitButton, tempTextField1, tempTextField2, tempTextField3, termLabel});

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

    private void tempTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
    }                                              

    private void tempTextField2ActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
    }                                              

    private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
        {
        NumberFormat formatter = new DecimalFormat("#0.00");

        double principalLoan = (int)((Double.parseDouble(tempTextField3.getText())));
        double loanInterest = (int)((Double.parseDouble(tempTextField1.getText())));
        double loanTerm = (int)((Double.parseDouble(tempTextField2.getText())));

        double compoundMultiplier = loanInterest/1200;
        double termMultiplier = loanTerm*(12);
        double exponent = -1*(termMultiplier);

        double a = 1+compoundMultiplier;
        double b = Math.pow(a,exponent);
        double c = 1 - b;
        double d = compoundMultiplier/c;

        double mortgagePayment = d*principalLoan;


        paymentLabel.setText("Your Payment is: $" + formatter.format(mortgagePayment));

        }
    }                                               

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:

    }                                          

    private void quitButtonActionPerformed(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(MortgageCalculatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MortgageCalculatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MortgageCalculatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MortgageCalculatorGUI.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 MortgageCalculatorGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton backButton;
    private javax.swing.JButton calculateButton;
    private javax.swing.JLabel interestLabel;
    private javax.swing.JLabel loanLabel;
    private javax.swing.JLabel paymentLabel;
    private javax.swing.JButton quitButton;
    private javax.swing.JTextField tempTextField1;
    private javax.swing.JTextField tempTextField2;
    private javax.swing.JTextField tempTextField3;
    private javax.swing.JLabel termLabel;
    // End of variables declaration
}

最佳答案

我找不到 formatted text field在你的代码中。但是我发现下面一行

double loanInterest = (int) ((Double.parseDouble(tempTextField1.getText())));

Double 转换为 int 的地方。也许你的意思是

double loanInterest = Double.parseDouble(tempTextField1.getText());

关于java - 在 Java 中格式化 tempTextField。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8558996/

相关文章:

java - 如何证明 i++ 不是原子的

java - JScrollPane get() 返回错误值?

java - 文本重复

Java this.dispose 调用时不关闭窗口

eclipse - 我可以让 Ubuntu 上的 Eclipse 看起来更紧凑吗?

java - JComboBox 在设置自定义 UI 后丢失键盘事件

C# 用户控件继承

java - 使用显示 :table jSTL tag 的内存使用问题

java - 如何从多项式字符串中获取系数和指数?

java - 从 Android 应用程序传递值并显示在控制台上