Java exercises 4

Requirements: Design and develop a visual financial calculator; it is required to be able to implement all the functions in Assignment 1, that is, it can calculate and display the sum of principal and interest in the window based on the annualized rate of return, principal, annuity, term and other parameters input by the user.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// 定义理财计算器类
class FinancialCalculator {
	// 计算本利和的方法
	public double calculateTotal(double principal, double annualPayment, double years, double annualInterestRate) {
		for (int i = 0; i < years; i++) {
			principal = principal * (1 + annualInterestRate) + annualPayment;
		}
		return principal;
	}
}

// 定义理财计算器GUI类
public class FinancialCalculatorGUI extends JFrame {
	private FinancialCalculator calculator; // 定义一个FinancialCalculator类型的成员变量

	public FinancialCalculatorGUI() {
		calculator = new FinancialCalculator(); // 初始化计算器对象

		setTitle("理财计算器"); // 设置窗口标题
		setSize(500, 500); // 设置窗口大小
		setAlwaysOnTop(true); // 设置窗口总是在最上层
		setLocationRelativeTo(null); // 设置窗口居中显示

		JPanel panel = new JPanel(); // 创建一个面板
		panel.setLayout(new GridLayout(5, 2)); // 设置面板布局为5行2列
		add(panel); // 将面板添加到窗口

		// 本金(元)标签与输入框
		JLabel principalLabel = new JLabel("本金(元):");
		JTextField principalField = new JTextField(10);
		principalLabel.setFont(new Font("仿宋", Font.PLAIN, 30));
		panel.add(principalLabel);
		panel.add(principalField);

		// 年金(元)标签与输入框
		JLabel annualPaymentLabel = new JLabel("年金(元):");
		JTextField annualPaymentField = new JTextField(10);
		annualPaymentLabel.setFont(new Font("仿宋", Font.PLAIN, 30));
		panel.add(annualPaymentLabel);
		panel.add(annualPaymentField);

		// 年限标签与输入框
		JLabel yearsLabel = new JLabel("年限:");
		JTextField yearsField = new JTextField(10);
		yearsLabel.setFont(new Font("仿宋", Font.PLAIN, 30));
		panel.add(yearsLabel);
		panel.add(yearsField);

		// 年收益率(%)标签与输入框
		JLabel interestRateLabel = new JLabel("年收益率(%):");
		JTextField interestRateField = new JTextField(10);
		interestRateLabel.setFont(new Font("仿宋", Font.PLAIN, 30));
		panel.add(interestRateLabel);
		panel.add(interestRateField);

		// 计算按钮与结果文本框
		JTextField resultField = new JTextField();
		JButton calculateButton = new JButton("计算");
		calculateButton.setBackground(Color.CYAN);
		panel.add(calculateButton);
		panel.add(resultField);

		// 为计算按钮添加事件监听器
		calculateButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// 获取用户输入的参数
				double principal = Double.parseDouble(principalField.getText());
				double annualPayment = Double.parseDouble(annualPaymentField.getText());
				double years = Double.parseDouble(yearsField.getText());
				double annualInterestRate = Double.parseDouble(interestRateField.getText()) / 100;

				// 调用计算器的方法计算本利和并显示结果
				double total = calculator.calculateTotal(principal, annualPayment, years, annualInterestRate);
				resultField.setText(String.format("%.2f", total));
			}
		});
		setVisible(true); // 设置窗口可见
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口关闭时退出程序
	}

	public static void main(String[] args) {
		new FinancialCalculatorGUI(); // 创建并显示理财计算器GUI
	}
}


The above code creates a financial calculator GUI program using the Swing library. Comments in the program detail the function of each component and the execution of the code. We encapsulate the calculation logic in the `FinancialCalculator` class and create objects of this class in the `FinancialCalculatorGUI` class. Doing this makes the code easier to read and maintain.
 

Guess you like

Origin blog.csdn.net/m0_62526778/article/details/130045308