Can you add clear the data Button and Exit Button( to exit from the output dialo
ID: 3720884 • Letter: C
Question
Can you add clear the data Button and Exit Button( to exit from the output dialog box when the final output is displayed) in the Output Dialog box in the following codes of Java below?
Loan.java
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.*;
import javax.swing.*;
public class Loan extends JFrame implements ActionListener
{
JLabel l1,l2,l3;
JTextField t1,t2,t3;
JButton b1;
public Loan()
{
setTitle("Loan");
setSize(500,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
l1=new JLabel("Loan Amount");
l2=new JLabel("Number of Years");
l3=new JLabel("Annual Interest Rate (in %)");
t1=new JTextField(25);
t2=new JTextField(25);
t3=new JTextField(25);
b1=new JButton("Calculate");
l1.setBounds(100,50,100,30);
l2.setBounds(100,100,100,30);
l3.setBounds(100,150,100,30);
t1.setBounds(250,50,100,30);
t2.setBounds(250,100,100,30);
t3.setBounds(250,150,100,30);
b1.setBounds(120,200,200,30);
add(l1);
add(l2);
add(l3);
add(t1);
add(t2);
add(t3);
add(b1);
b1.addActionListener(this);
setVisible(true);
}
public static void printTable(double principal,double annualInterestRate, int numYears)
{
double amount=principal;
double interestPaid, principalPaid, newBalance;
double monthlyInterestRate, monthlyPayment;
int month;
int numMonths = numYears * 12;
double totalInterest=0;
// calculate monthly rate
monthlyInterestRate = annualInterestRate / 12;
monthlyPayment = monthlyPayment(principal, monthlyInterestRate, numYears);
//generate a tabel of values for payment, principal, interest ,unpaid balance
//interest to date
for (month = 1; month <= numMonths; month++)
{
// calculate interest paid
interestPaid = principal * (monthlyInterestRate / 100);
//calculate the principal paid
principalPaid = monthlyPayment - interestPaid;
//calculate the new balance
newBalance = principal - principalPaid;
totalInterest+=interestPaid;
principal = newBalance;
}
DecimalFormat df=new DecimalFormat("#,###.00");
JOptionPane.showMessageDialog(null,"Total amount paid = "+df.format(amount+totalInterest));
}
/**The method monthlyPayment that returns the monthly payment of the loan amount,
* monthly interest rate and number of years*/
static double monthlyPayment(double loanAmount, double monthlyInterestRate, int numberOfYears) {
//convert interest rate to decimal value
monthlyInterestRate =monthlyInterestRate/ 100.0;
return loanAmount * monthlyInterestRate /( 1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12) );
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
double loanAmount = Double.parseDouble(t1.getText().trim());
int numYears = Integer.parseInt(t2.getText().trim());
double annualInterestRate = Double.parseDouble(t3.getText().trim());
printTable(loanAmount, annualInterestRate, numYears);
}
}
public static void main(String[] args)
{
new Loan();
}
}//end of class
Explanation / Answer
Hi,
To exit the JFrame, you can use below code-
jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
To clear all the JTextFields, you can use below code-
JButton b = new JButton("Clear");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
t1.setText("");
//t1.setText(null); //or we can use this method
t2.setText("");
t3.setText("");
}
});
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.