📄 interestcalculator.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class InterestCalculator extends JFrame
{
private JLabel principalJLabel;
private JTextField principalJTextField;
private JLabel interestRateJLabel;
private JTextField interestRateJTextField;
private JLabel yearsJLabel;
private JSpinner yearsJSpinner;
private JLabel yearlyBalanceJLabel;
private JTextArea yearlyBalanceJTextArea;
private JScrollPane yearlyBalanceJScrollPane;
private JButton calculateJButton;
public InterestCalculator()
{
createUserInterface();
}
private void createUserInterface()
{
Container contentPane = getContentPane();
contentPane.setLayout(null);
principalJLabel = new JLabel();
principalJLabel.setBounds(16,16,56,24);
principalJLabel.setText("Principal:");
contentPane.add(principalJLabel);
principalJTextField = new JTextField();
principalJTextField.setBounds(100,16,100,24);
principalJTextField.setHorizontalAlignment(JTextField.RIGHT);
contentPane.add(principalJTextField);
interestRateJLabel = new JLabel();
interestRateJLabel.setBounds(16,56,80,24);
interestRateJLabel.setText("Interest rate:");
contentPane.add(interestRateJLabel);
interestRateJTextField = new JTextField();
interestRateJTextField.setBounds(100,56,100,24);
interestRateJTextField.setHorizontalAlignment(JTextField.RIGHT);
contentPane.add(interestRateJTextField);
yearsJLabel = new JLabel();
yearsJLabel.setBounds(16,96,48,24);
yearsJLabel.setText("Years:");
contentPane.add(yearsJLabel);
yearsJSpinner = new JSpinner(new SpinnerNumberModel(1,1,10,1));
yearsJSpinner.setBounds(100,96,100,24);
contentPane.add(yearsJSpinner);
yearlyBalanceJLabel = new JLabel();
yearlyBalanceJLabel.setBounds(16,136,150,24);
yearlyBalanceJLabel.setText("Yearly account balance:");
contentPane.add(yearlyBalanceJLabel);
yearlyBalanceJTextArea = new JTextArea();
yearlyBalanceJTextArea.setEditable(false);
yearlyBalanceJScrollPane = new JScrollPane(yearlyBalanceJTextArea);
yearlyBalanceJScrollPane.setBounds(16,160,300,92);
contentPane.add(yearlyBalanceJScrollPane);
calculateJButton = new JButton();
calculateJButton.setBounds(216,16,100,24);
calculateJButton.setText("Calculate");
contentPane.add(calculateJButton);
calculateJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
calculateJButtonActionPerformed(event);
}
}
);
setTitle("Interset Calculator");
setSize(340,296);
setVisible(true);
}
private void calculateJButtonActionPerformed(ActionEvent event)
{
double principal = Double.parseDouble(principalJTextField.getText());
double rate = Double.parseDouble(interestRateJTextField.getText());
Integer integerObject = (Integer)yearsJSpinner.getValue();
int year = integerObject.intValue();
yearlyBalanceJTextArea.setText("Year\tAmount on Deposit");
DecimalFormat dollars = new DecimalFormat("$0.00");
for(int count = 1; count <= year; count++)
{
double amount = principal * Math.pow((1 + rate / 100),count);
yearlyBalanceJTextArea.append("\n" + count + "\t" + dollars.format
(amount));
}
}
public static void main(String[]args)
{
InterestCalculator application = new InterestCalculator();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -