drisden1
asked on
GUI Java Mortgage Calculator
Ok folks,
I have submitted this before and decided to play and modify it again. I have added an exit button to it and it seems I did everything correctly. But, there's always a but, I can't see the exit button in the window when I run it. can someone please help me find out what in the world I forgot. Maybe it is to many kids and easter candu but I am at a loss. Please any help would be great!!!! Thanks in advance.
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;
public class MortgageCalculator2 extends JApplet {
JLabel label_1;
JLabel label_2;
JLabel label_3;
JTextField textfield_1;
JTextField textfield_2;
JTextField textfield_3;
JButton calculate;
JLabel label_4;
JButton start_Over;
JLabel label_5;
JButton exit_Exit;
JLabel label_6;
public static void main(String args[]) {
JFrame f = new JFrame("Mortgage Loan Calculator");
MortgageCalculator2 calc = new MortgageCalculator2();
f.getContentPane().add(cal c);
calc.init();
f.pack();
f.setVisible(true);
}
public void init() {
MortgageCalculator2Layout customLayout = new MortgageCalculator2Layout( );
Container con = getContentPane();
con.setFont(new Font("Helvetica", Font.PLAIN, 12));
con.setLayout(customLayout );
label_1 = new JLabel("Loan Amount");
con.add(label_1);
label_2 = new JLabel("Interest Rate");
con.add(label_2);
label_3 = new JLabel("Term");
con.add(label_3);
textfield_1 = new JTextField("");
con.add(textfield_1);
textfield_2 = new JTextField("");
con.add(textfield_2);
textfield_3 = new JTextField("");
con.add(textfield_3);
JButton calculate = new JButton("Calculate");
con.add(calculate);
label_4 = new JLabel("Payment");
con.add(label_4);
JButton start_Over = new JButton("Start Over");
con.add(start_Over);
label_5 = new JLabel("$ ");
con.add(label_5);
JButton exit_Exit = new JButton("Exit");
con.add(exit_Exit);
label_6 = new JLabel("Exit");
con.add(label_6);
setSize(getPreferredSize() );
//Add listener to "Calculate" button
calculate.addActionListene r(new ActionListener() {
public void actionPerformed(ActionEven t h)
{
// Initialize variables
double amount = Double.parseDouble(textfie ld_1.getTe xt());
double irate = Double.parseDouble(textfie ld_2.getTe xt());
irate=irate/100;
double term = Double.parseDouble(textfie ld_3.getTe xt());
//Equation for calculation of monthly payment
double payment=Math.round((amount *irate/12* Math.pow(i rate/12+1, term*12))/ (Math.pow( irate/12+1 ,term*12)- 1));
label_5.setText("$ "+payment);
}
});
//Add listener to "Start Over" button
start_Over.addActionListen er(new ActionListener() {
public void actionPerformed(ActionEven t h)
{
textfield_1.setText("");
textfield_2.setText("");
textfield_3.setText("");
label_5.setText("$");
}
});
//Add listener to "Exit" button
exit_Exit.addActionListene r(new ActionListener() {
public void actionPerformed(ActionEven t h)
{
textfield_1.setText("");
textfield_2.setText("");
textfield_3.setText("");
label_5.setText("$");
label_6.setText("");
}
});
}
class MortgageCalculator2Layout implements LayoutManager {
public MortgageCalculator2Layout( ) {
}
public void addLayoutComponent(String name, Component comp) {
}
public void removeLayoutComponent(Comp onent comp) {
}
public Dimension preferredLayoutSize(Contai ner parent) {
Dimension dim = new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 539 + insets.left + insets.right;
dim.height = 394 + insets.top + insets.bottom;
return dim;
}
public Dimension minimumLayoutSize(Containe r parent) {
Dimension dim = new Dimension(0, 0);
return dim;
}
public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible()) {c.setBounds(insets.left+8 ,insets.to p+8,96,24) ;}
c = parent.getComponent(1);
if (c.isVisible()) {c.setBounds(insets.left+8 ,insets.to p+40,96,24 );}
c = parent.getComponent(2);
if (c.isVisible()) {c.setBounds(insets.left+8 ,insets.to p+72,96,24 );}
c = parent.getComponent(3);
if (c.isVisible()) {c.setBounds(insets.left+1 12,insets. top+8,112, 24);}
c = parent.getComponent(4);
if (c.isVisible()) {c.setBounds(insets.left+1 12,insets. top+40,112 ,24);}
c = parent.getComponent(5);
if (c.isVisible()) {c.setBounds(insets.left+1 12,insets. top+72,112 ,24);}
c = parent.getComponent(6);
if (c.isVisible()) {c.setBounds(insets.left+1 12,insets. top+104,11 2,24);}
c = parent.getComponent(7);
if (c.isVisible()) {c.setBounds(insets.left+8 ,insets.to p+136,96,2 4);}
c = parent.getComponent(8);
if (c.isVisible()) {c.setBounds(insets.left+1 12,insets. top+192,11 2,24);}
c = parent.getComponent(9);
if (c.isVisible()) {c.setBounds(insets.left+1 12,insets. top+136,72 ,24);}
}
}
}
I have submitted this before and decided to play and modify it again. I have added an exit button to it and it seems I did everything correctly. But, there's always a but, I can't see the exit button in the window when I run it. can someone please help me find out what in the world I forgot. Maybe it is to many kids and easter candu but I am at a loss. Please any help would be great!!!! Thanks in advance.
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;
public class MortgageCalculator2 extends JApplet {
JLabel label_1;
JLabel label_2;
JLabel label_3;
JTextField textfield_1;
JTextField textfield_2;
JTextField textfield_3;
JButton calculate;
JLabel label_4;
JButton start_Over;
JLabel label_5;
JButton exit_Exit;
JLabel label_6;
public static void main(String args[]) {
JFrame f = new JFrame("Mortgage Loan Calculator");
MortgageCalculator2 calc = new MortgageCalculator2();
f.getContentPane().add(cal
calc.init();
f.pack();
f.setVisible(true);
}
public void init() {
MortgageCalculator2Layout customLayout = new MortgageCalculator2Layout(
Container con = getContentPane();
con.setFont(new Font("Helvetica", Font.PLAIN, 12));
con.setLayout(customLayout
label_1 = new JLabel("Loan Amount");
con.add(label_1);
label_2 = new JLabel("Interest Rate");
con.add(label_2);
label_3 = new JLabel("Term");
con.add(label_3);
textfield_1 = new JTextField("");
con.add(textfield_1);
textfield_2 = new JTextField("");
con.add(textfield_2);
textfield_3 = new JTextField("");
con.add(textfield_3);
JButton calculate = new JButton("Calculate");
con.add(calculate);
label_4 = new JLabel("Payment");
con.add(label_4);
JButton start_Over = new JButton("Start Over");
con.add(start_Over);
label_5 = new JLabel("$ ");
con.add(label_5);
JButton exit_Exit = new JButton("Exit");
con.add(exit_Exit);
label_6 = new JLabel("Exit");
con.add(label_6);
setSize(getPreferredSize()
//Add listener to "Calculate" button
calculate.addActionListene
public void actionPerformed(ActionEven
{
// Initialize variables
double amount = Double.parseDouble(textfie
double irate = Double.parseDouble(textfie
irate=irate/100;
double term = Double.parseDouble(textfie
//Equation for calculation of monthly payment
double payment=Math.round((amount
label_5.setText("$ "+payment);
}
});
//Add listener to "Start Over" button
start_Over.addActionListen
public void actionPerformed(ActionEven
{
textfield_1.setText("");
textfield_2.setText("");
textfield_3.setText("");
label_5.setText("$");
}
});
//Add listener to "Exit" button
exit_Exit.addActionListene
public void actionPerformed(ActionEven
{
textfield_1.setText("");
textfield_2.setText("");
textfield_3.setText("");
label_5.setText("$");
label_6.setText("");
}
});
}
class MortgageCalculator2Layout implements LayoutManager {
public MortgageCalculator2Layout(
}
public void addLayoutComponent(String name, Component comp) {
}
public void removeLayoutComponent(Comp
}
public Dimension preferredLayoutSize(Contai
Dimension dim = new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 539 + insets.left + insets.right;
dim.height = 394 + insets.top + insets.bottom;
return dim;
}
public Dimension minimumLayoutSize(Containe
Dimension dim = new Dimension(0, 0);
return dim;
}
public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible()) {c.setBounds(insets.left+8
c = parent.getComponent(1);
if (c.isVisible()) {c.setBounds(insets.left+8
c = parent.getComponent(2);
if (c.isVisible()) {c.setBounds(insets.left+8
c = parent.getComponent(3);
if (c.isVisible()) {c.setBounds(insets.left+1
c = parent.getComponent(4);
if (c.isVisible()) {c.setBounds(insets.left+1
c = parent.getComponent(5);
if (c.isVisible()) {c.setBounds(insets.left+1
c = parent.getComponent(6);
if (c.isVisible()) {c.setBounds(insets.left+1
c = parent.getComponent(7);
if (c.isVisible()) {c.setBounds(insets.left+8
c = parent.getComponent(8);
if (c.isVisible()) {c.setBounds(insets.left+1
c = parent.getComponent(9);
if (c.isVisible()) {c.setBounds(insets.left+1
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Though sciuri has given you good advice, thats not the issue you are facing - the issue is because you have not set the layoutContainer correctly.
As you have already done all the code, I am pasting in the working version of the code as per my above comments
Make sure you implement the Exit action listener correctly
Make sure you implement the Exit action listener correctly
package test;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListe ner;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MG1 extends JApplet
{
JLabel label_1;
JLabel label_2;
JLabel label_3;
JTextField textfield_1;
JTextField textfield_2;
JTextField textfield_3;
JButton calculate;
JLabel label_4;
JButton start_Over;
JLabel label_5;
JButton exit_Exit;
JLabel label_6;
public static void main(String args[])
{
JFrame f = new JFrame("Mortgage Loan Calculator");
MG1 calc = new MG1();
f.getContentPane().add(cal c);
calc.init();
f.pack();
f.setVisible(true);
}
public void init()
{
MortgageCalculator2Layout customLayout = new MortgageCalculator2Layout( );
Container con = getContentPane();
con.setFont(new Font("Helvetica", Font.PLAIN, 12));
con.setLayout(customLayout );
label_1 = new JLabel("Loan Amount");
con.add(label_1);
label_2 = new JLabel("Interest Rate");
con.add(label_2);
label_3 = new JLabel("Term");
con.add(label_3);
textfield_1 = new JTextField("");
con.add(textfield_1);
textfield_2 = new JTextField("");
con.add(textfield_2);
textfield_3 = new JTextField("");
con.add(textfield_3);
calculate = new JButton("Calculate");
con.add(calculate);
label_4 = new JLabel("Payment");
con.add(label_4);
start_Over = new JButton("Start Over");
con.add(start_Over);
label_5 = new JLabel("$ ");
con.add(label_5);
exit_Exit = new JButton("Exit");
con.add(exit_Exit);
label_6 = new JLabel("Exit");
con.add(label_6);
setSize(getPreferredSize() );
//Add listener to "Calculate" button
calculate.addActionListene r(new ActionListener()
{
public void actionPerformed(ActionEven t h)
{
// Initialize variables
double amount = Double.parseDouble(textfie ld_1.getTe xt());
double irate = Double.parseDouble(textfie ld_2.getTe xt());
irate = irate / 100;
double term = Double.parseDouble(textfie ld_3.getTe xt());
//Equation for calculation of monthly payment
double payment = Math.round((amount * irate / 12 * Math.pow(irate / 12 + 1, term * 12)) / (Math.pow(irate / 12 + 1, term * 12) - 1));
label_5.setText("$ " + payment);
}
});
//Add listener to "Start Over" button
start_Over.addActionListen er(new ActionListener()
{
public void actionPerformed(ActionEven t h)
{
textfield_1.setText("");
textfield_2.setText("");
textfield_3.setText("");
label_5.setText("$");
}
});
//Add listener to "Exit" button
exit_Exit.addActionListene r(new ActionListener()
{
public void actionPerformed(ActionEven t h)
{
textfield_1.setText("");
textfield_2.setText("");
textfield_3.setText("");
label_5.setText("$");
}
});
}
class MortgageCalculator2Layout implements LayoutManager
{
public MortgageCalculator2Layout( )
{
}
public void addLayoutComponent(String name, Component comp)
{
}
public void removeLayoutComponent(Comp onent comp)
{
}
public Dimension preferredLayoutSize(Contai ner parent)
{
Dimension dim = new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 539 + insets.left + insets.right;
dim.height = 394 + insets.top + insets.bottom;
return dim;
}
public Dimension minimumLayoutSize(Containe r parent)
{
Dimension dim = new Dimension(0, 0);
return dim;
}
public void layoutContainer(Container parent)
{
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 8, 96, 24);
}
c = parent.getComponent(1);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 40, 96, 24);
}
c = parent.getComponent(2);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 72, 96, 24);
}
c = parent.getComponent(3);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 8, 112, 24);
}
c = parent.getComponent(4);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 40, 112, 24);
}
c = parent.getComponent(5);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 72, 112, 24);
}
c = parent.getComponent(6);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 104, 112, 24);
}
c = parent.getComponent(7);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 136, 96, 24);
}
c = parent.getComponent(8);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 192, 112, 24);
}
c = parent.getComponent(9);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 136, 72, 24);
}
c = parent.getComponent(10);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 220, 112, 24);
}
c = parent.getComponent(11);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 220, 112, 24);
}
}
}
}
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent
import java.awt.event.ActionListe
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MG1 extends JApplet
{
JLabel label_1;
JLabel label_2;
JLabel label_3;
JTextField textfield_1;
JTextField textfield_2;
JTextField textfield_3;
JButton calculate;
JLabel label_4;
JButton start_Over;
JLabel label_5;
JButton exit_Exit;
JLabel label_6;
public static void main(String args[])
{
JFrame f = new JFrame("Mortgage Loan Calculator");
MG1 calc = new MG1();
f.getContentPane().add(cal
calc.init();
f.pack();
f.setVisible(true);
}
public void init()
{
MortgageCalculator2Layout customLayout = new MortgageCalculator2Layout(
Container con = getContentPane();
con.setFont(new Font("Helvetica", Font.PLAIN, 12));
con.setLayout(customLayout
label_1 = new JLabel("Loan Amount");
con.add(label_1);
label_2 = new JLabel("Interest Rate");
con.add(label_2);
label_3 = new JLabel("Term");
con.add(label_3);
textfield_1 = new JTextField("");
con.add(textfield_1);
textfield_2 = new JTextField("");
con.add(textfield_2);
textfield_3 = new JTextField("");
con.add(textfield_3);
calculate = new JButton("Calculate");
con.add(calculate);
label_4 = new JLabel("Payment");
con.add(label_4);
start_Over = new JButton("Start Over");
con.add(start_Over);
label_5 = new JLabel("$ ");
con.add(label_5);
exit_Exit = new JButton("Exit");
con.add(exit_Exit);
label_6 = new JLabel("Exit");
con.add(label_6);
setSize(getPreferredSize()
//Add listener to "Calculate" button
calculate.addActionListene
{
public void actionPerformed(ActionEven
{
// Initialize variables
double amount = Double.parseDouble(textfie
double irate = Double.parseDouble(textfie
irate = irate / 100;
double term = Double.parseDouble(textfie
//Equation for calculation of monthly payment
double payment = Math.round((amount * irate / 12 * Math.pow(irate / 12 + 1, term * 12)) / (Math.pow(irate / 12 + 1, term * 12) - 1));
label_5.setText("$ " + payment);
}
});
//Add listener to "Start Over" button
start_Over.addActionListen
{
public void actionPerformed(ActionEven
{
textfield_1.setText("");
textfield_2.setText("");
textfield_3.setText("");
label_5.setText("$");
}
});
//Add listener to "Exit" button
exit_Exit.addActionListene
{
public void actionPerformed(ActionEven
{
textfield_1.setText("");
textfield_2.setText("");
textfield_3.setText("");
label_5.setText("$");
}
});
}
class MortgageCalculator2Layout implements LayoutManager
{
public MortgageCalculator2Layout(
{
}
public void addLayoutComponent(String name, Component comp)
{
}
public void removeLayoutComponent(Comp
{
}
public Dimension preferredLayoutSize(Contai
{
Dimension dim = new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 539 + insets.left + insets.right;
dim.height = 394 + insets.top + insets.bottom;
return dim;
}
public Dimension minimumLayoutSize(Containe
{
Dimension dim = new Dimension(0, 0);
return dim;
}
public void layoutContainer(Container parent)
{
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 8, 96, 24);
}
c = parent.getComponent(1);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 40, 96, 24);
}
c = parent.getComponent(2);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 72, 96, 24);
}
c = parent.getComponent(3);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 8, 112, 24);
}
c = parent.getComponent(4);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 40, 112, 24);
}
c = parent.getComponent(5);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 72, 112, 24);
}
c = parent.getComponent(6);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 104, 112, 24);
}
c = parent.getComponent(7);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 136, 96, 24);
}
c = parent.getComponent(8);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 192, 112, 24);
}
c = parent.getComponent(9);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 136, 72, 24);
}
c = parent.getComponent(10);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 220, 112, 24);
}
c = parent.getComponent(11);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 220, 112, 24);
}
}
}
}
ASKER
keyurkarnik,
Got it and I see what I forgot.....but here is a question. The exit button only clears the solution not exit the program. What did I forget?
Got it and I see what I forgot.....but here is a question. The exit button only clears the solution not exit the program. What did I forget?
//Add listener to "Exit" button
exit_Exit.addActionListene r(new ActionListener()
{
public void actionPerformed(ActionEven t h)
{
textfield_1.setText("");
textfield_2.setText("");
textfield_3.setText("");
label_5.setText("$");
System.exit(0); // THIS !!
}
});
;JOOP!
exit_Exit.addActionListene
{
public void actionPerformed(ActionEven
{
textfield_1.setText("");
textfield_2.setText("");
textfield_3.setText("");
label_5.setText("$");
System.exit(0); // THIS !!
}
});
;JOOP!
ASKER
sciuriware,
Thanks for the suggestion but it does not exit the program, all it does is clear the inputs. Here is the revised code with all suggestions:
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListe ner;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MortgageCalculator2 extends JApplet
{
JLabel label_1;
JLabel label_2;
JLabel label_3;
JTextField textfield_1;
JTextField textfield_2;
JTextField textfield_3;
JButton calculate;
JLabel label_4;
JButton start_Over;
JLabel label_5;
JButton exit_Exit;
JLabel label_6;
public static void main(String args[])
{
JFrame f = new JFrame("Mortgage Loan Calculator");
MG1 calc = new MG1();
f.getContentPane().add(cal c);
calc.init();
f.pack();
f.setVisible(true);
}
public void init()
{
MortgageCalculator2Layout customLayout = new MortgageCalculator2Layout( );
Container con = getContentPane();
con.setFont(new Font("Helvetica", Font.PLAIN, 12));
con.setLayout(customLayout );
label_1 = new JLabel("Loan Amount");
con.add(label_1);
label_2 = new JLabel("Interest Rate");
con.add(label_2);
label_3 = new JLabel("Term");
con.add(label_3);
textfield_1 = new JTextField("");
con.add(textfield_1);
textfield_2 = new JTextField("");
con.add(textfield_2);
textfield_3 = new JTextField("");
con.add(textfield_3);
calculate = new JButton("Calculate");
con.add(calculate);
label_4 = new JLabel("Payment");
con.add(label_4);
start_Over = new JButton("Start Over");
con.add(start_Over);
label_5 = new JLabel("$ ");
con.add(label_5);
exit_Exit = new JButton("Exit");
con.add(exit_Exit);
label_6 = new JLabel("Exit");
con.add(label_6);
setSize(getPreferredSize() );
//Add listener to "Calculate" button
calculate.addActionListene r(new ActionListener()
{
public void actionPerformed(ActionEven t h)
{
// Initialize variables
double amount = Double.parseDouble(textfie ld_1.getTe xt());
double irate = Double.parseDouble(textfie ld_2.getTe xt());
irate = irate / 100;
double term = Double.parseDouble(textfie ld_3.getTe xt());
//Equation for calculation of monthly payment
double payment = Math.round((amount * irate / 12 * Math.pow(irate / 12 + 1, term * 12)) / (Math.pow(irate / 12 + 1, term * 12) - 1));
label_5.setText("$ " + payment);
}
});
//Add listener to "Start Over" button
start_Over.addActionListen er(new ActionListener()
{
public void actionPerformed(ActionEven t h)
{
textfield_1.setText("");
textfield_2.setText("");
textfield_3.setText("");
label_5.setText("$");
}
});
//Add listener to "Exit" button
exit_Exit.addActionListene r(new ActionListener()
{
public void actionPerformed(ActionEven t h)
{
textfield_1.setText("");
textfield_2.setText("");
textfield_3.setText("");
label_6.setText("");
System.exit(6);
}
});
}
class MortgageCalculator2Layout implements LayoutManager
{
public MortgageCalculator2Layout( )
{
}
public void addLayoutComponent(String name, Component comp)
{
}
public void removeLayoutComponent(Comp onent comp)
{
}
public Dimension preferredLayoutSize(Contai ner parent)
{
Dimension dim = new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 539 + insets.left + insets.right;
dim.height = 394 + insets.top + insets.bottom;
return dim;
}
public Dimension minimumLayoutSize(Containe r parent)
{
Dimension dim = new Dimension(0, 0);
return dim;
}
public void layoutContainer(Container parent)
{
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 8, 96, 24);
}
c = parent.getComponent(1);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 40, 96, 24);
}
c = parent.getComponent(2);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 72, 96, 24);
}
c = parent.getComponent(3);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 8, 112, 24);
}
c = parent.getComponent(4);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 40, 112, 24);
}
c = parent.getComponent(5);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 72, 112, 24);
}
c = parent.getComponent(6);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 104, 112, 24);
}
c = parent.getComponent(7);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 136, 96, 24);
}
c = parent.getComponent(8);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 192, 112, 24);
}
c = parent.getComponent(9);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 136, 72, 24);
}
c = parent.getComponent(10);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 220, 112, 24);
}
c = parent.getComponent(11);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 220, 112, 24);
}
}
}
}
Thanks for the suggestion but it does not exit the program, all it does is clear the inputs. Here is the revised code with all suggestions:
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent
import java.awt.event.ActionListe
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MortgageCalculator2 extends JApplet
{
JLabel label_1;
JLabel label_2;
JLabel label_3;
JTextField textfield_1;
JTextField textfield_2;
JTextField textfield_3;
JButton calculate;
JLabel label_4;
JButton start_Over;
JLabel label_5;
JButton exit_Exit;
JLabel label_6;
public static void main(String args[])
{
JFrame f = new JFrame("Mortgage Loan Calculator");
MG1 calc = new MG1();
f.getContentPane().add(cal
calc.init();
f.pack();
f.setVisible(true);
}
public void init()
{
MortgageCalculator2Layout customLayout = new MortgageCalculator2Layout(
Container con = getContentPane();
con.setFont(new Font("Helvetica", Font.PLAIN, 12));
con.setLayout(customLayout
label_1 = new JLabel("Loan Amount");
con.add(label_1);
label_2 = new JLabel("Interest Rate");
con.add(label_2);
label_3 = new JLabel("Term");
con.add(label_3);
textfield_1 = new JTextField("");
con.add(textfield_1);
textfield_2 = new JTextField("");
con.add(textfield_2);
textfield_3 = new JTextField("");
con.add(textfield_3);
calculate = new JButton("Calculate");
con.add(calculate);
label_4 = new JLabel("Payment");
con.add(label_4);
start_Over = new JButton("Start Over");
con.add(start_Over);
label_5 = new JLabel("$ ");
con.add(label_5);
exit_Exit = new JButton("Exit");
con.add(exit_Exit);
label_6 = new JLabel("Exit");
con.add(label_6);
setSize(getPreferredSize()
//Add listener to "Calculate" button
calculate.addActionListene
{
public void actionPerformed(ActionEven
{
// Initialize variables
double amount = Double.parseDouble(textfie
double irate = Double.parseDouble(textfie
irate = irate / 100;
double term = Double.parseDouble(textfie
//Equation for calculation of monthly payment
double payment = Math.round((amount * irate / 12 * Math.pow(irate / 12 + 1, term * 12)) / (Math.pow(irate / 12 + 1, term * 12) - 1));
label_5.setText("$ " + payment);
}
});
//Add listener to "Start Over" button
start_Over.addActionListen
{
public void actionPerformed(ActionEven
{
textfield_1.setText("");
textfield_2.setText("");
textfield_3.setText("");
label_5.setText("$");
}
});
//Add listener to "Exit" button
exit_Exit.addActionListene
{
public void actionPerformed(ActionEven
{
textfield_1.setText("");
textfield_2.setText("");
textfield_3.setText("");
label_6.setText("");
System.exit(6);
}
});
}
class MortgageCalculator2Layout implements LayoutManager
{
public MortgageCalculator2Layout(
{
}
public void addLayoutComponent(String name, Component comp)
{
}
public void removeLayoutComponent(Comp
{
}
public Dimension preferredLayoutSize(Contai
{
Dimension dim = new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 539 + insets.left + insets.right;
dim.height = 394 + insets.top + insets.bottom;
return dim;
}
public Dimension minimumLayoutSize(Containe
{
Dimension dim = new Dimension(0, 0);
return dim;
}
public void layoutContainer(Container parent)
{
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 8, 96, 24);
}
c = parent.getComponent(1);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 40, 96, 24);
}
c = parent.getComponent(2);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 72, 96, 24);
}
c = parent.getComponent(3);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 8, 112, 24);
}
c = parent.getComponent(4);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 40, 112, 24);
}
c = parent.getComponent(5);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 72, 112, 24);
}
c = parent.getComponent(6);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 104, 112, 24);
}
c = parent.getComponent(7);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 136, 96, 24);
}
c = parent.getComponent(8);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 192, 112, 24);
}
c = parent.getComponent(9);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 136, 72, 24);
}
c = parent.getComponent(10);
if (c.isVisible())
{
c.setBounds(insets.left + 112, insets.top + 220, 112, 24);
}
c = parent.getComponent(11);
if (c.isVisible())
{
c.setBounds(insets.left + 8, insets.top + 220, 112, 24);
}
}
}
}
Sorry, System.exit() doesn't work for an Applet.
ASKER
sciuriware,
So, is it possible to use an exit command in this program or do I have to change everything? I've tried a few things and nothing seems to work.
So, is it possible to use an exit command in this program or do I have to change everything? I've tried a few things and nothing seems to work.
http://forum.java.sun.com/thread.jspa?threadID=329456&messageID=2970381
Try this, or just set the visible attribute to false or something. You cannot call system.exit thats for sure
Try this, or just set the visible attribute to false or something. You cannot call system.exit thats for sure
More or less independent of this, replace:
setSize(getPreferredSize()
by:
pack();
;JOOP!