⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 carpayment.java

📁 适合与JAVA初级程序员 适合JAVA初学者编程
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class CarPayment extends JFrame
{

      private JLabel priceJLabel;
      private JTextField priceJTextField;
      private JLabel downPaymentJLabel;
      private JTextField downPaymentJTextField;
      private JLabel interestJLabel;
      private JTextField interestJTextField;
      private JButton calculateJButton;
      private JTextArea paymentsJTextArea;
      
      public CarPayment()
      {
          createUserInterface();
      }

      private void createUserInterface()
      {

          Container contentPane = getContentPane();
          contentPane.setLayout(null); 
           
          priceJLabel = new JLabel();
          priceJLabel.setBounds(40,24,80,21);
          priceJLabel.setText("Price:");
          contentPane.add(priceJLabel);

          priceJTextField = new JTextField();
          priceJTextField.setBounds(184,24,56,21);
          priceJTextField.setHorizontalAlignment(JTextField.RIGHT);
          contentPane.add(priceJTextField);

          downPaymentJLabel = new JLabel();
          downPaymentJLabel.setBounds(40,56,96,21);
          downPaymentJLabel.setText("Down payment:");
          contentPane.add(downPaymentJLabel);

          downPaymentJTextField = new JTextField();
          downPaymentJTextField.setBounds(184,56,56,21);
          downPaymentJTextField.setHorizontalAlignment(JTextField.RIGHT);
          contentPane.add(downPaymentJTextField);

          interestJLabel = new JLabel();
          interestJLabel.setBounds(40,88,120,21);
          interestJLabel.setText("Annual interest rate:");
          contentPane.add(interestJLabel);
        
          interestJTextField = new JTextField();
          interestJTextField.setBounds(184,88,56,21);
          interestJTextField.setHorizontalAlignment(JTextField.RIGHT);
          contentPane.add(interestJTextField);
 
          calculateJButton = new JButton();
          calculateJButton.setBounds(92,128,94,24);
          calculateJButton.setText("Calculate");
          contentPane.add(calculateJButton);
          calculateJButton.addActionListener(

             new ActionListener()
             {
                 public void actionPerformed(ActionEvent event)
                 {

                     calculateJButtonActionPerformed(event);
                 }

             }

          );

         paymentsJTextArea = new JTextArea();
         paymentsJTextArea.setBounds(28,168,232,90);
         paymentsJTextArea.setEditable(false);
         contentPane.add(paymentsJTextArea);

         setTitle("Car Payment Calculator");
         setSize(288,302);
         setVisible(true);

     }

     private void calculateJButtonActionPerformed(ActionEvent event)
     {

         int years = 2;
         int months;
         double monthlyPayment;
          
         paymentsJTextArea.setText("");
         paymentsJTextArea.append("Months\tMonthly Payments");

         int price = Integer.parseInt(priceJTextField.getText());
         int downPayment = Integer.parseInt(downPaymentJTextField.getText());
         double interest = Double.parseDouble(interestJTextField.getText());
         int loanAmount = price - downPayment;
         double monthlyInterest = interest / 1200;
         DecimalFormat currency = new DecimalFormat("$0.00");
         while(years <= 5)
         {
            
             months = 12 * years;
             monthlyPayment = calculateMonthlyPayment(monthlyInterest,months,loanAmount);
             
             paymentsJTextArea.append("\n" + months + "\t" + 
                         currency.format(monthlyPayment));
             years++;

         }
         }

         private double calculateMonthlyPayment( double monthlyInterest,
            int months,int loanAmount)
         {

            double base = Math.pow(1 + monthlyInterest,months);
            return loanAmount * monthlyInterest / (1 - (1 / base));
         }

         public static void main(String[]args)
         {

            CarPayment application = new CarPayment();
            application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         }

}
             






































 

 




          

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -