1. Engineering
  2. Computer Science
  3. java in an earlier assignment you wrote a program that...

Question: java in an earlier assignment you wrote a program that...

Question details

Java:

In an earlier assignment, you wrote a program that sets up a GUI interface for a simple calculator, as illustrated below. This assignment will involve modifying the earlier program so that the sum of the first and second value is actually calculated when the user clicks on the calculate button.

Simple Calculator First Value:I Second Value: Sum is: Calculate

Previous Code:

import java.awt.*;
import javax.swing.*;

public class LoanCalculator {
public static void main(String[] args) {

JFrame frame = new JFrame("Loan Calculator");
JLabel lbl1=new JLabel("Annial Intrest Rate:");
JLabel lbl2=new JLabel("Number of Years:");
JLabel lbl3=new JLabel("Loan Ammount:");
JLabel lbl4=new JLabel("Monthly Payment:");
JLabel lbl5=new JLabel("Total Payment:");
JTextField txt1=new JTextField();
  
JTextField txt2=new JTextField();
JTextField txt3=new JTextField();
JTextField txt4=new JTextField();
JTextField txt5=new JTextField();
JButton btn1 = new JButton("Calculate");
  
// create grid layout with 6 rows , 2 columns with horizontal
// and vertical gap set to 10

JPanel panel = new JPanel(new GridLayout(6,2,10,10));
// add Jlable,Jtextfirld and JButton to the panel
panel.add(lbl1);
panel.add(txt1);
panel.add(lbl2);
panel.add(txt2);
panel.add(lbl3);
panel.add(txt3);
panel.add(lbl4);
panel.add(txt4);
panel.add(lbl5);
panel.add(txt5);
panel.add(btn1);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,250);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution