1. Create a GUI based loan application that will calculate total payment and mon
ID: 3772876 • Letter: 1
Question
1. Create a GUI based loan application that will calculate total payment and monthly payment based on the following inputs. a) Loan Amount : JTextField b) Number of years : (either 5 or 10 years) JRadioButton c) Interest Rate : (12% or 15 % or 18 %) JComboBox d) Calculate : JButton e) Clear : JButton f) Monthly Payment : JTextField g) Total Payment : JTextField Widgets a,b,c,d,e appear on panel1. Widgets f,g appear on panel2. Panel1 should be on left hand side of the frame and panel2 on the right hand side. Use simple interest formula to calculate the two values Monthly payment and Total Payment. Use of clear and calculate buttons are self-explanatory. Good to keep design clean and use shades of one particular color for widgets.
2. Mouse Event Handling – Implement a simple GUI window that will track mouse actions as follows. a) Within the panel if Mouse moves from one location to the other, you need to print the x and y co-ordinates of old and new location on console. Also calculate the distance it was moved. (remember the distance between two points formula) b) For every click, count the number of left clicks and number of right clicks. Print the values on console. c) For each left click, the panel background color is changed to blue. d) For each right click, the panel background color is changed to green. Deliverables : Zip all .java files and respective packages into one along with a doc with screenshot and upload on portal. You have one day.
Explanation / Answer
1)
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.*;
import java.awt.event.*;
public class TestCalc extends JFrame implements ActionListener
{
// instance variables
private JPanel p;
private JLabel label1, label2;
private JTextField jTextField1, jTextField2;
private JButton showTableButton;
private JTextArea textArea;
private JScrollPane scrollPane;
public TestCalc()
{
// call the super constructor and set the window title
super("Loan Calculator");
// create a panel
p = new JPanel();
//Create the first label.
label1 = new JLabel("Loan Amount", JLabel.LEFT);
p.add( label1);
//Set the position of its text, relative to its icon:
// label1.setVerticalTextPosition(JLabel.BO…
// label1.setHorizontalTextPosition(JLa…
// construct textfield with 10 columns
jTextField1 = new JTextField( 10 );
p.add( jTextField1 );
//Create the other labels.
label2 = new JLabel("Number of Years");
p.add(label2);
// construct textfield with default text
jTextField2 = new JTextField(3);
p.add(jTextField2 );
label1.setToolTipText("A label containing both image and text");
label2.setToolTipText("A label containing only text");
showTableButton = new JButton("Show Table" );
showTableButton.addActionListener(this);
p.add(showTableButton);
textArea = new JTextArea(20, 40);
textArea.setEditable(false);
scrollPane = new JScrollPane(textArea);
p.add( scrollPane );
add(p);
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize(520, 360); // set frame size
setVisible(true); // display frame
}
private void showTable()
{
String temploan = jTextField1.getText();
// double principle;
//int lifeofloan;
textArea.setText(temploan);
}
public void actionPerformed(ActionEvent event)
{
Object object = event.getSource();
if (object == showTableButton)
{
showTable();
}
}
public static void main(String args[])
{
TestCalc window = new TestCalc(); // create window
} // end main
} // end class TestCalc
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.