Finish the JAVA graphical application with three labeled text fields, one each f
ID: 3765660 • Letter: F
Question
Finish the JAVA graphical application with three labeled text fields, one each for the initial amount of savings account, the annual interest rate, and the number of years. Add a button “Calculate” and a read-only text area to display the balance of the savings account after the end of each year.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class SavingsCalculatorFrameTester {
public static void main(String[] args) {
JFrame frame=new SavingsCalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static class SavingsCalculatorFrame extends JFrame
{
private static final long serialVersionUID = 1L;
private JTextField initialBalanceField;
private JTextField annualRateField;
private JTextField yearsField;
private JTextArea balanceArea;
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 400;
public SavingsCalculatorFrame()
{
setSize(FRAME_WIDTH, FRAME_HEIGHT);
add(createControlPanel());
}
private JPanel createControlPanel()
{
final int FIELD_WIDTH = 10;
final int AREA_WIDTH = 20;
final int AREA_HEIGHT = 10;
//Text fields and Text Area
initialBalanceField = new
annualRateField = new
yearsField = new
balanceArea = new
balanceArea.setEditable(false);
JPanel panel = new JPanel();
// add to panel labels and text fields and text area
return panel;
}
private JButton createCalculateButton()
{
JButton calculateButton = new
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
for (int year = 1; year <= nyears; year++)
{
}
}
}
ActionListener listener = new ButtonListener();
calculateButton.addActionListener(listener);
return calculateButton;
}
}
}
Explanation / Answer
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class SavingsCalculatorFrameTester {
public static void main(String[] args) {
JFrame frame=new SavingsCalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static class SavingsCalculatorFrame extends JFrame
{
private static final long serialVersionUID = 1L;
private JTextField initialBalanceField;
private JTextField annualRateField;
private JTextField yearsField;
private JTextArea balanceArea;
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 400;
public SavingsCalculatorFrame()
{
setSize(FRAME_WIDTH, FRAME_HEIGHT);
add(createControlPanel());
}
private JPanel createControlPanel()
{
final int FIELD_WIDTH = 10;
final int AREA_WIDTH = 20;
final int AREA_HEIGHT = 10;
//Text fields and Text Area
initialBalanceField = new
annualRateField = new
yearsField = new
balanceArea = new
balanceArea.setEditable(false);
JPanel panel = new JPanel();
// add to panel labels and text fields and text area
return panel;
}
private JButton createCalculateButton()
{
JButton calculateButton = new
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
double init_amt,int_rate, nyear;
init_amt=Double.parseDouble(initialBalanceField.getText());
int_rate=Double.parseDouble(annualRateField.getText());
int_rate=Double.parseDouble(annualRateField.getText());
nyears=Double.parseDouble(yearsField.getText());
double yint,sa=0;
for (int year = 1; year <= nyears; year++)
{
yint=init_amt*int_rate/100;
init_amt=init_amt+yint;
}
balanceArea.setText(String.valueOf(init_amt));
}
}
ActionListener listener = new ButtonListener();
calculateButton.addActionListener(listener);
return calculateButton;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.