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

📄 exercise14_3.java

📁 Introduction to java programming 一书中所有编程练习部分的源码
💻 JAVA
字号:
// Exercise14_3.java: Enable the Mortgage applet to run
// as application
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class Exercise14_3 extends JApplet implements ActionListener {
  // Declare and create text fields for interest rate
  // year, loan amount, monthly payment, and total payment
  private JTextField jtfInterestRate = new JTextField(10);
  private JTextField jtfYear = new JTextField(10);
  private JTextField jtfLoan = new JTextField(10);
  private JTextField jtfMonthlyPay = new JTextField(10);
  private JTextField jtfTotalPay = new JTextField(10);

  // Declare and create a Compute Mortgage button
  private JButton jbtCompute = new JButton("Compute Mortgage");

  // Main method
  public static void main(String[] args) {
    // Create a frame
    JFrame frame = new JFrame(
      "Running a program as applet and frame");

    // Create an instance of MortgageApplet
    Exercise14_3 applet = new Exercise14_3();

    // Add the applet instance to the frame
    frame.getContentPane().add(applet, BorderLayout.CENTER);

    // Invoke init() and start()
    applet.init();
    applet.start();

    // Display the frame
    frame.setSize(300, 300);
    frame.setVisible(true);
  }

  // Initialize user interface
  public Exercise14_3() {
    // Set properties on the text fields
    jtfMonthlyPay.setEditable(false);
    jtfTotalPay.setEditable(false);

    // Panel p1 to hold labels and text fields
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(5, 2));
    p1.add(new Label("Interest Rate"));
    p1.add(jtfInterestRate);
    p1.add(new Label("Years "));
    p1.add(jtfYear);
    p1.add(new Label("Loan Amount"));
    p1.add(jtfLoan);
    p1.add(new Label("Monthly Payment"));
    p1.add(jtfMonthlyPay);
    p1.add(new Label("Total Payment"));
    p1.add(jtfTotalPay);
    p1.setBorder(new
      TitledBorder("Enter interest rate, year and loan amount"));

    // Panel p2 to hold the button
    JPanel p2 = new JPanel();
    p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
    p2.add(jbtCompute);

    // Add the components to the applet
    getContentPane().add(p1, BorderLayout.CENTER);
    getContentPane().add(p2, BorderLayout.SOUTH);

    // Register listener
    jbtCompute.addActionListener(this);
  }

  // Handler for the "Compute" button
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == jbtCompute) {
      // Get values from text fields
      double interest =
        (Double.valueOf(jtfInterestRate.getText())).doubleValue();
      int year =
        (Integer.valueOf(jtfYear.getText())).intValue();
      double loanAmount =
        (Double.valueOf(jtfLoan.getText())).doubleValue();

      // Create a Loan object
      Loan loan = new Loan(interest, year, loanAmount);

      // Display monthly payment and total payment
      jtfMonthlyPay.setText(String.valueOf(loan.monthlyPayment()));
      jtfTotalPay.setText(String.valueOf(loan.totalPayment()));
    }
  }
}

⌨️ 快捷键说明

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