ghost8067
asked on
Mortgage calculator can't display radio buttons
I am trying to change my mortgage calculator program from a GUI that accepts loan amount, interest rate and term to one that only accepts loan amount and allows you to select a radio button to select one of 3 pre-determined rates and terms. I have removed references to my other input data items and tried to add radio buttons that represent each loan. While I haven't written any code for the buttons yet, I was trying to get them as a first step to display on my GUI. I added the code for the buttons and group, but when I compile and run I do not see them. Any assistance would be appreciated. In relation to the same issue, is there a way to specify coordinates for the radio buttons as in the label belowZ?
headerJLabel.setBounds(16, 16, 300, 46);
Thanks,
Jon
//Week2 Jan Schab
/* Mortgage Monyhly Payment Calculator with graphical user interface
Programmer: J.Schab
Date: June 4,2006
Filename: Mortgage.java
Purpose: This project accepts the mortgage amount, the annual interest
rate, and the term of the loan in years from the user. The program
then calculates the monthly mortgage payment and displays it in currency format.
There is a quit button to exit the application, and a clear button to
clear user entered fields in order to calculate a new mortgage. The program
will also clear the payment field should the user press any key while positioned
in any input textfield.
*/
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.text.NumberFormat;
import java.util.Locale;
import java.text.*;
class Mortgage extends JFrame
{
private JLabel headerJLabel;
Border rowborder = new EmptyBorder( 3, 10, 3, 10 );
// JLabel and JTextField for the mortgage amount
private JLabel amountJLabel;
private JTextField amountJTextField;
// JLabel for monthly mortgage payment
private JLabel monthpayJLabel;
// JTextArea for displaying results
private JTextArea outputJTextArea;
// JButton to initiate calculations
private JButton calculateJButton;
// JButton to clear entries
private JButton clearJButton;
// JButton to quit
private JButton quitJButton;
// no-argument constructor
public Mortgage()
{
createUserInterface();
}
// create and position the GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// set northLabel using borderlayout
headerJLabel = new JLabel();
headerJLabel.setBounds(16, 16, 300, 46);
headerJLabel.setText("Ente r Values then Calculate for Monthly Payment");
contentPane.add(headerJLab el);
// enable specific positioning of GUI components
contentPane.setLayout(null );
// set up amountJLabel
amountJLabel = new JLabel();
amountJLabel.setBounds(116 , 76, 104, 26);
amountJLabel.setText("Mort gage amount:");
contentPane.add(amountJLab el);
// set up amountJTextField
amountJTextField = new JTextField();
amountJTextField.setBounds (244, 76, 56, 26);
amountJTextField.setHorizo ntalAlignm ent(JTextF ield.RIGHT );
contentPane.add(amountJTex tField);
amountJTextField.addKeyLis tener(
new KeyAdapter() // anonymous inner class
{
// event handler for key pressed in amountJTextField
public void keyPressed(KeyEvent event)
{
amountJTextFieldkeyPressed (event);
}
} // end anonymous inner class
); // end call to addKeyListener
// added code below for radio buttons
JPanel radioPanel = new JPanel();
JRadioButton aButton = new JRadioButton("7 Years at 5.35%" , true);
JRadioButton bButton = new JRadioButton("15 Years at 5.50%" , false);
JRadioButton cButton = new JRadioButton("30 Years at 5.75%", false);
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(aButton);
bgroup.add(bButton);
bgroup.add(cButton);
radioPanel.setLayout(new FlowLayout(FlowLayout.LEFT , 4, 4 ));
radioPanel.add(aButton);
radioPanel.add(bButton);
radioPanel.add(cButton);
contentPane.add(radioPanel );
//radioPanel.setMaximumSiz e( new Dimension( 10000, radioPanel.getMinimumSize( ).height ) );
radioPanel.setBorder( rowborder );
// end added code
// set up calculateJButton
calculateJButton = new JButton();
calculateJButton.setBounds (16, 196, 90, 26);
calculateJButton.setText(" Calculate" );
contentPane.add(calculateJ Button);
calculateJButton.addAction Listener(
new ActionListener() // anonymous inner class
{
// event handler called when calculateJButton is pressed
public void actionPerformed(ActionEven t event)
{
calculateJButtonActionPerf ormed(even t);
}
} // end anonymous inner class
); // end call to addActionListener
// set up clearJButton
clearJButton = new JButton();
clearJButton.setBounds(16, 240, 90, 26);
clearJButton.setText("Clea r");
contentPane.add(clearJButt on);
clearJButton.addActionList ener(
new ActionListener() // anonymous inner class
{
// event handler called when clearJButton is pressed
public void actionPerformed(ActionEven t event)
{
clearJButtonActionPerforme d(event);
}
} // end anonymous inner class
); // end call to addActionListener
// set up quitJButton
quitJButton = new JButton();
quitJButton.setBounds(140, 240, 90, 26);
quitJButton.setText("Quit" );
contentPane.add(quitJButto n);
quitJButton.addActionListe ner(
new ActionListener() // anonymous inner class
{
// event handler called when quitJButton is pressed
public void actionPerformed(ActionEven t event)
{
quitJButtonActionPerformed (event);
}
} // end anonymous inner class
); // end call to addActionListener
// set up outputJTextArea
outputJTextArea = new JTextArea();
contentPane.add(outputJTex tArea);
outputJTextArea.setBounds( 244, 196, 56, 26);
outputJTextArea.setEditabl e(false);
//set up monthpayJLabel
monthpayJLabel = new JLabel();
monthpayJLabel.setBounds(1 20, 196, 156, 26);
monthpayJLabel.setText("Mo nthly Payment:");
contentPane.add(monthpayJL abel);
// set properties of application’s window
setTitle("Mortgage Calculator"); // set title bar text
setSize(338, 350); // set window's size
setVisible(true); // display window
} // end method createUserInterface
// called when user presses key in amountJTextField
private void amountJTextFieldkeyPressed (KeyEvent event)
{
outputJTextArea.setText("" ); // clear outputJTextArea
}
// method called when user clicks calculateJButton
private void calculateJButtonActionPerf ormed(Acti onEvent event)
{
try
{
//int term = Integer.parseInt(termJText Field.getT ext());
// length of the loan in years
//int monthCount = (term * 12); // Calculate months from years
//int amount = Integer.parseInt(amountJTe xtField.ge tText());
//double yearlyRate = Double.parseDouble(interes tJTextFiel d.getText( ))
// ;
//double interestRateMonthly = (yearlyRate / 12) / 100;
// Calculate monthly interest
//double monthlyPayment; // monthly payment amount variable
// Calculate monthly payment
//monthlyPayment = (amount * interestRateMonthly) / (1-Math.pow(1 +
// interestRateMonthly, - monthCount));
DecimalFormat currency = new DecimalFormat("$0.00");
// Format output
//outputJTextArea.setText( currency.f ormat(mont hlyPayment ));
// Output payment amount
}
catch (NumberFormatException ex){}
}
// method called when user clicks clearJButton
private void clearJButtonActionPerforme d(ActionEv ent event)
{
try
{
outputJTextArea.setText("" ); // clear outputJTextArea
amountJTextField.setText(" "); // clear amountJTextField
//interestJTextField.setTe xt(""); // clear interestJTextField
//termJTextField.setText(" "); // clear termJTextField
}
catch (NumberFormatException ex){}
}
// method called when user clicks quitJButton
private void quitJButtonActionPerformed (ActionEve nt event)
{
try
{
System.exit(0);
}
catch (NumberFormatException ex){}
}
// main method
public static void main(String[]args)
{
Mortgage application = new Mortgage();
application.setDefaultClos eOperation (JFrame.EX IT_ON_CLOS E);
application.setSize(400, 300);
application.setVisible(tru e);
} // end method main
} // end class Mortgage
headerJLabel.setBounds(16,
Thanks,
Jon
//Week2 Jan Schab
/* Mortgage Monyhly Payment Calculator with graphical user interface
Programmer: J.Schab
Date: June 4,2006
Filename: Mortgage.java
Purpose: This project accepts the mortgage amount, the annual interest
rate, and the term of the loan in years from the user. The program
then calculates the monthly mortgage payment and displays it in currency format.
There is a quit button to exit the application, and a clear button to
clear user entered fields in order to calculate a new mortgage. The program
will also clear the payment field should the user press any key while positioned
in any input textfield.
*/
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.text.NumberFormat;
import java.util.Locale;
import java.text.*;
class Mortgage extends JFrame
{
private JLabel headerJLabel;
Border rowborder = new EmptyBorder( 3, 10, 3, 10 );
// JLabel and JTextField for the mortgage amount
private JLabel amountJLabel;
private JTextField amountJTextField;
// JLabel for monthly mortgage payment
private JLabel monthpayJLabel;
// JTextArea for displaying results
private JTextArea outputJTextArea;
// JButton to initiate calculations
private JButton calculateJButton;
// JButton to clear entries
private JButton clearJButton;
// JButton to quit
private JButton quitJButton;
// no-argument constructor
public Mortgage()
{
createUserInterface();
}
// create and position the GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// set northLabel using borderlayout
headerJLabel = new JLabel();
headerJLabel.setBounds(16,
headerJLabel.setText("Ente
contentPane.add(headerJLab
// enable specific positioning of GUI components
contentPane.setLayout(null
// set up amountJLabel
amountJLabel = new JLabel();
amountJLabel.setBounds(116
amountJLabel.setText("Mort
contentPane.add(amountJLab
// set up amountJTextField
amountJTextField = new JTextField();
amountJTextField.setBounds
amountJTextField.setHorizo
contentPane.add(amountJTex
amountJTextField.addKeyLis
new KeyAdapter() // anonymous inner class
{
// event handler for key pressed in amountJTextField
public void keyPressed(KeyEvent event)
{
amountJTextFieldkeyPressed
}
} // end anonymous inner class
); // end call to addKeyListener
// added code below for radio buttons
JPanel radioPanel = new JPanel();
JRadioButton aButton = new JRadioButton("7 Years at 5.35%" , true);
JRadioButton bButton = new JRadioButton("15 Years at 5.50%" , false);
JRadioButton cButton = new JRadioButton("30 Years at 5.75%", false);
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(aButton);
bgroup.add(bButton);
bgroup.add(cButton);
radioPanel.setLayout(new FlowLayout(FlowLayout.LEFT
radioPanel.add(aButton);
radioPanel.add(bButton);
radioPanel.add(cButton);
contentPane.add(radioPanel
//radioPanel.setMaximumSiz
radioPanel.setBorder( rowborder );
// end added code
// set up calculateJButton
calculateJButton = new JButton();
calculateJButton.setBounds
calculateJButton.setText("
contentPane.add(calculateJ
calculateJButton.addAction
new ActionListener() // anonymous inner class
{
// event handler called when calculateJButton is pressed
public void actionPerformed(ActionEven
{
calculateJButtonActionPerf
}
} // end anonymous inner class
); // end call to addActionListener
// set up clearJButton
clearJButton = new JButton();
clearJButton.setBounds(16,
clearJButton.setText("Clea
contentPane.add(clearJButt
clearJButton.addActionList
new ActionListener() // anonymous inner class
{
// event handler called when clearJButton is pressed
public void actionPerformed(ActionEven
{
clearJButtonActionPerforme
}
} // end anonymous inner class
); // end call to addActionListener
// set up quitJButton
quitJButton = new JButton();
quitJButton.setBounds(140,
quitJButton.setText("Quit"
contentPane.add(quitJButto
quitJButton.addActionListe
new ActionListener() // anonymous inner class
{
// event handler called when quitJButton is pressed
public void actionPerformed(ActionEven
{
quitJButtonActionPerformed
}
} // end anonymous inner class
); // end call to addActionListener
// set up outputJTextArea
outputJTextArea = new JTextArea();
contentPane.add(outputJTex
outputJTextArea.setBounds(
outputJTextArea.setEditabl
//set up monthpayJLabel
monthpayJLabel = new JLabel();
monthpayJLabel.setBounds(1
monthpayJLabel.setText("Mo
contentPane.add(monthpayJL
// set properties of application’s window
setTitle("Mortgage Calculator"); // set title bar text
setSize(338, 350); // set window's size
setVisible(true); // display window
} // end method createUserInterface
// called when user presses key in amountJTextField
private void amountJTextFieldkeyPressed
{
outputJTextArea.setText(""
}
// method called when user clicks calculateJButton
private void calculateJButtonActionPerf
{
try
{
//int term = Integer.parseInt(termJText
// length of the loan in years
//int monthCount = (term * 12); // Calculate months from years
//int amount = Integer.parseInt(amountJTe
//double yearlyRate = Double.parseDouble(interes
// ;
//double interestRateMonthly = (yearlyRate / 12) / 100;
// Calculate monthly interest
//double monthlyPayment; // monthly payment amount variable
// Calculate monthly payment
//monthlyPayment = (amount * interestRateMonthly) / (1-Math.pow(1 +
// interestRateMonthly, - monthCount));
DecimalFormat currency = new DecimalFormat("$0.00");
// Format output
//outputJTextArea.setText(
// Output payment amount
}
catch (NumberFormatException ex){}
}
// method called when user clicks clearJButton
private void clearJButtonActionPerforme
{
try
{
outputJTextArea.setText(""
amountJTextField.setText("
//interestJTextField.setTe
//termJTextField.setText("
}
catch (NumberFormatException ex){}
}
// method called when user clicks quitJButton
private void quitJButtonActionPerformed
{
try
{
System.exit(0);
}
catch (NumberFormatException ex){}
}
// main method
public static void main(String[]args)
{
Mortgage application = new Mortgage();
application.setDefaultClos
application.setSize(400, 300);
application.setVisible(tru
} // end method main
} // end class Mortgage
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hey setting the layout to null and using the bounds are not reccommended in swings! better go with GBL which is much flexible!
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.text.NumberFormat;
import java.util.Locale;
import java.text.*;
class Mortgage extends JFrame
{
private JLabel headerJLabel;
Border rowborder = new EmptyBorder( 3, 10, 3, 10 );
// JLabel and JTextField for the mortgage amount
private JLabel amountJLabel;
private JTextField amountJTextField;
// JLabel for monthly mortgage payment
private JLabel monthpayJLabel;
// JTextArea for displaying results
private JTextArea outputJTextArea;
// JButton to initiate calculations
private JButton calculateJButton;
// JButton to clear entries
private JButton clearJButton;
// JButton to quit
private JButton quitJButton;
// no-argument constructor
public Mortgage()
{
createUserInterface();
}
// create and position the GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
JPanel contentPane = new JPanel( new GridBagLayout() ) ;
// set northLabel using borderlayout
headerJLabel = new JLabel();
// headerJLabel.setBounds(16,
headerJLabel.setText("Ente
contentPane.add( headerJLabel, new GridBagConstraints( 0, 0, 3, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
// enable specific positioning of GUI components
// contentPane.setLayout(null
// set up amountJLabel
amountJLabel = new JLabel();
// amountJLabel.setBounds(116
amountJLabel.setText("Mort
// contentPane.add(amountJLab
contentPane.add( amountJLabel, new GridBagConstraints( 1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 10 ), 0, 0 ) );
// set up amountJTextField
amountJTextField = new JTextField();
// amountJTextField.setBounds
amountJTextField.setHorizo
// contentPane.add(amountJTex
contentPane.add( amountJTextField, new GridBagConstraints( 2, 1, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
amountJTextField.addKeyLis
new KeyAdapter() // anonymous inner class
{
// event handler for key pressed in amountJTextField
public void keyPressed(KeyEvent event)
{
amountJTextFieldkeyPressed
}
} // end anonymous inner class
); // end call to addKeyListener
// added code below for radio buttons
JPanel radioPanel = new JPanel();
JRadioButton aButton = new JRadioButton("7 Years at 5.35%" , true);
JRadioButton bButton = new JRadioButton("15 Years at 5.50%" , false);
JRadioButton cButton = new JRadioButton("30 Years at 5.75%", false);
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(aButton);
bgroup.add(bButton);
bgroup.add(cButton);
radioPanel.setLayout( new GridBagLayout() );
radioPanel.add(aButton);
radioPanel.add(bButton);
radioPanel.add(cButton);
// contentPane.add(radioPanel
contentPane.add( radioPanel, new GridBagConstraints( 0, 2, 3, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
//radioPanel.setMaximumSiz
radioPanel.setBorder( rowborder );
// end added code
// set up calculateJButton
calculateJButton = new JButton();
// calculateJButton.setBounds
calculateJButton.setText("
// contentPane.add(calculateJ
contentPane.add( calculateJButton, new GridBagConstraints( 0, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 10 ), 0, 0 ) );
calculateJButton.addAction
new ActionListener() // anonymous inner class
{
// event handler called when calculateJButton is pressed
public void actionPerformed(ActionEven
{
calculateJButtonActionPerf
}
} // end anonymous inner class
); // end call to addActionListener
//set up monthpayJLabel
monthpayJLabel = new JLabel();
// monthpayJLabel.setBounds(1
monthpayJLabel.setText("Mo
// contentPane.add(monthpayJL
contentPane.add( monthpayJLabel, new GridBagConstraints( 1, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 10 ), 0, 0 ) );
// set up outputJTextArea
outputJTextArea = new JTextArea();
// contentPane.add(outputJTex
contentPane.add( outputJTextArea, new GridBagConstraints( 2, 3, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
// outputJTextArea.setBounds(
outputJTextArea.setEditabl
JPanel btnPanel = new JPanel( new FlowLayout() ) ;
// set up clearJButton
clearJButton = new JButton();
// clearJButton.setBounds(16,
clearJButton.setText("Clea
// contentPane.add(clearJButt
btnPanel.add( clearJButton );
clearJButton.addActionList
new ActionListener() // anonymous inner class
{
// event handler called when clearJButton is pressed
public void actionPerformed(ActionEven
{
clearJButtonActionPerforme
}
} // end anonymous inner class
); // end call to addActionListener
// set up quitJButton
quitJButton = new JButton();
// quitJButton.setBounds(140,
quitJButton.setText("Quit"
// contentPane.add(quitJButto
btnPanel.add( quitJButton );
quitJButton.addActionListe
new ActionListener() // anonymous inner class
{
// event handler called when quitJButton is pressed
public void actionPerformed(ActionEven
{
quitJButtonActionPerformed
}
} // end anonymous inner class
); // end call to addActionListener
getContentPane().add( contentPane, BorderLayout.CENTER ) ;
getContentPane().add( btnPanel, BorderLayout.SOUTH ) ;
// set properties of application’s window
setTitle("Mortgage Calculator"); // set title bar text
//setSize(338, 350); // set window's size
pack() ;
setVisible(true); // display window
} // end method createUserInterface
// called when user presses key in amountJTextField
private void amountJTextFieldkeyPressed
{
outputJTextArea.setText(""
}
// method called when user clicks calculateJButton
private void calculateJButtonActionPerf
{
try
{
//int term = Integer.parseInt(termJText
// length of the loan in years
//int monthCount = (term * 12); // Calculate months from years
//int amount = Integer.parseInt(amountJTe
//double yearlyRate = Double.parseDouble(interes
// ;
//double interestRateMonthly = (yearlyRate / 12) / 100;
// Calculate monthly interest
//double monthlyPayment; // monthly payment amount variable
// Calculate monthly payment
//monthlyPayment = (amount * interestRateMonthly) / (1-Math.pow(1 +
// interestRateMonthly, - monthCount));
DecimalFormat currency = new DecimalFormat("$0.00");
// Format output
//outputJTextArea.setText(
// Output payment amount
}
catch (NumberFormatException ex){}
}
// method called when user clicks clearJButton
private void clearJButtonActionPerforme
{
try
{
outputJTextArea.setText(""
amountJTextField.setText("
//interestJTextField.setTe
//termJTextField.setText("
}
catch (NumberFormatException ex){}
}
// method called when user clicks quitJButton
private void quitJButtonActionPerformed
{
try
{
System.exit(0);
}
catch (NumberFormatException ex){}
}
// main method
public static void main(String[]args)
{
Mortgage application = new Mortgage();
application.setDefaultClos
application.setSize(400, 300);
application.setVisible(tru
} // end method main
} // end class Mortgage