[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Mortgage Calculator GUI

Posted on 2005-05-13
5
Medium Priority
?
465 Views
Last Modified: 2008-01-09
Good Evening all. I am am getting the following error when I try to compile my program: "Week2_GUILayout is not abstract and does not override abstract method minimumLayoutSize (java.awt.Container) in java.awt.LayoutManager". Any ideas on how I can fix this? This is the first GUI I have done.  Also, is it necessary to have a container, or can I use a Flow Layout? Would that effect the outcome of my program? Other that a Container for the layout, what would work better?

import javax. swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.applet.Applet;


public class Week2_GUI extends JApplet{
      JLabel label_a;
      JLabel label_b;
      JLabel label_c;
      JTextField textfield_a;
      JTextField textfield_b;
      JTextField textfield_c;
      JButton calculate;
      JLabel label_d;
      JButton Start_Again;
      JLabel label_e;
      
      public static void main(String args[]){
            JFrame f = new JFrame("Mortgage Payment Calculator");
            Week2_GUI calc = new Week2_GUI();
            f.getContentPane().add(calc);
            calc.init();
            f.pack();
            f.setVisible(true);
            
      }
      public void init(){
Week2_GUILayout customLayout = new Week2_GUILayout();

      Container con = getContentPane();
      con.setLayout(customLayout);

      label_a = new JLabel("Amount of Loan");
      con.add(label_a);

      label_b = new JLabel("Interest Rate");
      con.add(label_b);

      label_c = new JLabel("Term of Loan");
      con.add(label_c);

      textfield_a = new JTextField("");
      con.add(textfield_a);

      textfield_b = new JTextField("");
      con.add(textfield_b);
      
      textfield_c = new JTextField("");
      con.add(textfield_c);
      
      JButton calculate = new JButton("Calculate");
      con.add(calculate);
      
      label_d = new JLabel("Payment Amount");
      con.add(label_d);
      
      JButton Start_Again = new JButton("Start Again");
      con.add(Start_Again);
      
      label_e = new JLabel("$");
      con.add(label_e);
      
      setSize(getPreferredSize());
      
      calculate.addActionListener(new ActionListener(){
            public void actionPerformed (ActionEvent h)
            {
                  double amount = Double.parseDouble(textfield_a.getText());
                  double interest = Double.parseDouble(textfield_b.getText());
                  interest = interest/100;
                  double term = Double.parseDouble(textfield_c.getText());
                  
                  double payment=Math.round((amount*interest/12*Math.pow(interest/12+1,term*12))/(Math.pow(interest/12+1,term*12)-1));
                  label_e.setText("$"+payment);
                  
            }
      });
      
      Start_Again.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent h)
            {
                  textfield_a.setText("");
                  textfield_b.setText("");
                  textfield_c.setText("");
                  
                  label_e.setText("$");
            }
      });
      
}

}

class Week2_GUILayout implements LayoutManager{
      public Week2_GUILayout(){
}
public void addLayoutComponent(String name, Component comp){
}

public void removeLayoutComponent(Component comp){
}

public Dimension preferredLayoutSize(Container parent){
      Dimension t = new Dimension(0,0);
      
      Insets i = parent.getInsets();
      t.width = 539 + i.left + i.right;
      t.height = 394 + i.top + i.bottom;
      
      return t;
      
}

public void layoutContainer(Container parent){
      Insets i = parent.getInsets();
      
      Component w;
      w = parent.getComponent(0);
        if (w.isVisible()) {w.setBounds(i.left+8,i.top+8,96,24);}
        w = parent.getComponent(1);
        if (w.isVisible()) {w.setBounds(i.left+8,i.top+40,96,24);}
        w = parent.getComponent(2);
        if (w.isVisible()) {w.setBounds(i.left+8,i.top+72,96,24);}
        w = parent.getComponent(3);
        if (w.isVisible()) {w.setBounds(i.left+112,i.top+8,112,24);}
        w = parent.getComponent(4);
        if (w.isVisible()) {w.setBounds(i.left+112,i.top+40,112,24);}
        w = parent.getComponent(5);
        if (w.isVisible()) {w.setBounds(i.left+112,i.top+72,112,24);}
        w = parent.getComponent(6);
        if (w.isVisible()) {w.setBounds(i.left+112,i.top+104,112,24);}
        w = parent.getComponent(7);
        if (w.isVisible()) {w.setBounds(i.left+8,i.top+136,96,24);}
        w = parent.getComponent(8);
        if (w.isVisible()) {w.setBounds(i.left+112,i.top+192,112,24);}
        w = parent.getComponent(9);
        if (w.isVisible()) {w.setBounds(i.left+112,i.top+136,72,24);}
       
      }
     
  }



0
Comment
Question by:magmaya
  • 3
  • 2
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 14001393

Add a method minimumLayoutSize(java.awt.Container) to the Week2_GUILayout class

0
 
LVL 92

Expert Comment

by:objects
ID: 14001395
fillowing should do:

public Dimension minimumLayoutSize(Container container)
{
   return preferredLayoutSize(container);
}
0
 

Author Comment

by:magmaya
ID: 14003706
Thank you! My program compiled well. For my mortgage payment formula. I don't like rounding up the payment. I was looking at http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-cents.html, and I am trying to figure out how to have the full payment amount show... (w/ 2 decimal places) ex: $1113.67.  Any suggestions?
0
 

Author Comment

by:magmaya
ID: 14003798
Also, would I change my GUI to use FlowLayout instead of hard coding the positions in the container?
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 14004072
Use a NumberFormat

NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.println(nf.format(amount));
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month18 days, 2 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question