Results 1 to 4 of 4
  1. #1
    Cyprusice is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default Help; Missing JButtons

    Can some one please help me figure out why my JButtons dont show up in my Applet? Every thing compiles and runs fine, but with no buttons

    import java.text.*; // Import Text Formatting
    import java.text.DecimalFormat;
    import java.math.*; //
    import java.io.*;
    import java.awt.*; // Import Layout
    import java.awt.event.*; // Import Event Handlers
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*; // Import GUI Components

    public class Mortgage2 extends JApplet
    implements ActionListener {
    //Declares and builds the variables, labels, fields, and buttons needed for the program
    private double AMNT;
    private double TERM;
    private double APR;
    private double PAY;
    JLabel Title;
    JLabel AMNTLBL;
    JLabel TERMLBL;
    JLabel APRLBL;
    JLabel PAYLBL;
    JTextField AMNTFLD;
    JTextField TERMFLD;
    JTextField APRFLD;
    JTextField PAYFLD;
    JButton CalculateBTN;
    JButton ResetBTN;

    public void init()
    {
    getContentPane().setLayout(null);
    setupGUI();
    }
    void setupGUI()
    {
    //Builds all labels required
    Title = new JLabel();
    Title.setLocation(101,4);
    Title.setSize(120,32);
    Title.setForeground( new Color(-16777216) );
    Title.setBackground( new Color(-6710887) );
    Title.setText("Mortgage Calculator");
    getContentPane().add(Title);

    AMNTLBL = new JLabel();
    AMNTLBL.setLocation(0,46);
    AMNTLBL.setSize(207,32);
    AMNTLBL.setForeground( new Color(-16777216) );
    AMNTLBL.setBackground( new Color(-6710887) );
    AMNTLBL.setText("Enter Loan Amount (Whole Dollars)");
    getContentPane().add(AMNTLBL);

    TERMLBL = new JLabel();
    TERMLBL.setLocation(0,79);
    TERMLBL.setSize(207,32);
    TERMLBL.setForeground( new Color(-16777216) );
    TERMLBL.setBackground( new Color(-6710887) );
    TERMLBL.setText("Enter Loans Term (Years)");
    getContentPane().add(TERMLBL);

    APRLBL = new JLabel();
    APRLBL.setLocation(0,112);
    APRLBL.setSize(207,32);
    APRLBL.setForeground( new Color(-16777216) );
    APRLBL.setBackground( new Color(-6710887) );
    APRLBL.setText("Enter APR");
    getContentPane().add(APRLBL);

    PAYLBL = new JLabel();
    PAYLBL.setLocation(0,190);
    PAYLBL.setSize(207,32);
    PAYLBL.setForeground( new Color(-16777216) );
    PAYLBL.setBackground( new Color(-6710887) );
    PAYLBL.setText("Your Monthly Payment");
    getContentPane().add(PAYLBL);

    //Builds all text fields required
    AMNTFLD = new JTextField();
    AMNTFLD.setLocation(227,46);
    AMNTFLD.setSize(100,32);
    AMNTFLD.setText("");
    AMNTFLD.setColumns(10);
    getContentPane().add(AMNTFLD);

    TERMFLD = new JTextField();
    TERMFLD.setLocation(227,79);
    TERMFLD.setSize(100,32);
    TERMFLD.setText("");
    TERMFLD.setColumns(10);
    getContentPane().add(TERMFLD);

    APRFLD = new JTextField();
    APRFLD.setLocation(227,112);
    APRFLD.setSize(100,32);
    APRFLD.setText("");
    APRFLD.setColumns(10);
    getContentPane().add(APRFLD);

    PAYFLD = new JTextField();
    PAYFLD.setLocation(226,190);
    PAYFLD.setSize(100,32);
    PAYFLD.setText("");
    PAYFLD.setColumns(10);
    getContentPane().add(PAYFLD);

    //Binds labels to fields
    AMNTLBL.setLabelFor(AMNTFLD);
    TERMLBL.setLabelFor(TERMFLD);
    APRLBL.setLabelFor(APRFLD);
    PAYLBL.setLabelFor(PAYFLD);

    //Builds all buttons required
    CalculateBTN = new JButton("Calculate");
    CalculateBTN.addActionListener(this);
    CalculateBTN.setActionCommand("Calculate");

    ResetBTN = new JButton("Reset");
    ResetBTN.addActionListener(this);
    ResetBTN.setActionCommand("Reset");

    //Builds JApplet window
    setSize(337,259);
    setBackground( new Color(-6710887) );
    setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {

    if (e.getActionCommand().equals(CalculateBTN.getActio nCommand())) {

    try {
    AMNT = validateNumber(AMNTFLD.getText(), "Loans Amount");
    AMNTFLD.setText(CurrencyComposition(AMNT));
    APR = validateNumber(APRFLD.getText(), "APR");
    TERM = validateNumber(TERMFLD.getText(), "Loans Term");
    double PAY = CalcPAY(AMNT, APR, TERM);
    PAYFLD.setText(CurrencyComposition(PAY));
    }

    catch (Exception t1) {
    PAYFLD.setText(null);
    };

    } else if (e.getActionCommand().equals(ResetBTN.getActionCom mand())) {
    AMNTFLD.setText(null);
    APRFLD.setText(null);
    TERMFLD.setText(null);
    PAYFLD.setText(null);
    }

    }


    // Calculates the monthly payment
    private double CalcPAY(double AMNT, double APR, double TERM) {

    double MAPR = (APR / 100) / 12; // Formula for J
    double MTERM = TERM * 12; // Formula for N
    double PAY = AMNT * (MAPR / (1 - Math.pow((1 + MAPR), -TERM)));

    return PAY;
    }

    private double validateNumber(String numericString, String fieldName) throws Exception {

    double numberValue = 0;
    try {
    // make sure we have a number
    if (numericString.isEmpty())
    throw new Exception();

    // strip out currency symbol
    if (numericString.indexOf('$')> -1)
    {
    String newString = numericString.replace("$","");
    numericString = newString;
    }
    // transform string into double
    numberValue = NumberFormat.getNumberInstance().parse(numericStri ng).doubleValue();

    // no negative numbers
    if (numberValue < 0)
    throw new Exception();


    } catch (Exception e) {
    // Display Error Message in it's own pane
    JOptionPane.showMessageDialog(this, "Invalid value in " + fieldName + ". \nPlease check your entry.", "Data Entry Error", JOptionPane.ERROR_MESSAGE);

    throw e;
    }


    return numberValue;
    }

    // Formats the numeric values to readable currency
    private String CurrencyComposition(double input) {

    DecimalFormat df = new DecimalFormat("$###,###,###.00");
    df.setMaximumFractionDigits(2);
    String formattedString = df.format(input);
    return formattedString;
    }
    }

  2. #2
    Cyprusice is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default

    I figured out my issue with the buttons, but I am pretty sure my equation for loan calculations is wrong.... Please help if you want.

    import java.text.; // Import Text Formatting
    import java.text.DecimalFormat;
    import java.math.; //
    import java.io.;
    import java.awt.; // Import Layout
    import java.awt.event.; // Import Event Handlers
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.; // Import GUI Components

    public class Mortgage2 extends JApplet
    implements ActionListener {
    //Declares and builds the variables, labels, fields, and buttons needed for the program
    private double AMNT;
    private double TERM;
    private double APR;
    private double PAY;
    JLabel Title;
    JLabel AMNTLBL;
    JLabel TERMLBL;
    JLabel APRLBL;
    JLabel PAYLBL;
    JTextField AMNTFLD;
    JTextField TERMFLD;
    JTextField APRFLD;
    JTextField PAYFLD;
    JButton CalculateBTN;
    JButton ResetBTN;

    public void init()
    {
    getContentPane().setLayout(null);
    setupGUI();
    }
    void setupGUI()
    {
    //Builds all labels required
    Title = new JLabel();
    Title.setLocation(101,4);
    Title.setSize(120,32);
    Title.setForeground( new Color(-16777216) );
    Title.setBackground( new Color(-6710887) );
    Title.setText("Mortgage Calculator");
    getContentPane().add(Title);

    AMNTLBL = new JLabel();
    AMNTLBL.setLocation(0,46);
    AMNTLBL.setSize(207,32);
    AMNTLBL.setForeground( new Color(-16777216) );
    AMNTLBL.setBackground( new Color(-6710887) );
    AMNTLBL.setText("Enter Loan Amount (Whole Dollars)");
    getContentPane().add(AMNTLBL);

    TERMLBL = new JLabel();
    TERMLBL.setLocation(0,79);
    TERMLBL.setSize(207,32);
    TERMLBL.setForeground( new Color(-16777216) );
    TERMLBL.setBackground( new Color(-6710887) );
    TERMLBL.setText("Enter Loans Term (Years)");
    getContentPane().add(TERMLBL);

    APRLBL = new JLabel();
    APRLBL.setLocation(0,112);
    APRLBL.setSize(207,32);
    APRLBL.setForeground( new Color(-16777216) );
    APRLBL.setBackground( new Color(-6710887) );
    APRLBL.setText("Enter APR");
    getContentPane().add(APRLBL);

    PAYLBL = new JLabel();
    PAYLBL.setLocation(0,190);
    PAYLBL.setSize(207,32);
    PAYLBL.setForeground( new Color(-16777216) );
    PAYLBL.setBackground( new Color(-6710887) );
    PAYLBL.setText("Your Monthly Payment");
    getContentPane().add(PAYLBL);

    //Builds all text fields required
    AMNTFLD = new JTextField();
    AMNTFLD.setLocation(227,46);
    AMNTFLD.setSize(100,32);
    AMNTFLD.setText("");
    AMNTFLD.setColumns(10);
    getContentPane().add(AMNTFLD);

    TERMFLD = new JTextField();
    TERMFLD.setLocation(227,79);
    TERMFLD.setSize(100,32);
    TERMFLD.setText("");
    TERMFLD.setColumns(10);
    getContentPane().add(TERMFLD);

    APRFLD = new JTextField();
    APRFLD.setLocation(227,112);
    APRFLD.setSize(100,32);
    APRFLD.setText("");
    APRFLD.setColumns(10);
    getContentPane().add(APRFLD);

    PAYFLD = new JTextField();
    PAYFLD.setLocation(226,190);
    PAYFLD.setSize(100,32);
    PAYFLD.setText("");
    PAYFLD.setColumns(10);
    getContentPane().add(PAYFLD);

    //Binds labels to fields
    AMNTLBL.setLabelFor(AMNTFLD);
    TERMLBL.setLabelFor(TERMFLD);
    APRLBL.setLabelFor(APRFLD);
    PAYLBL.setLabelFor(PAYFLD);

    //Builds all buttons required
    CalculateBTN = new JButton("Calculate");
    CalculateBTN.setLocation(29,150);
    CalculateBTN.setSize(100,32);
    getContentPane().add(CalculateBTN);
    CalculateBTN.addActionListener(this);
    CalculateBTN.setActionCommand("Calculate");

    ResetBTN = new JButton("Reset");
    ResetBTN.setLocation(200,150);
    ResetBTN.setSize(100,32);
    getContentPane().add(ResetBTN);
    ResetBTN.addActionListener(this);
    ResetBTN.setActionCommand("Reset");

    //Builds JApplet window
    setSize(337,259);
    setBackground( new Color(-6710887) );
    setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {

    if (e.getActionCommand().equals(CalculateBTN.getActio nCommand())) {

    try {
    AMNT = validateNumber(AMNTFLD.getText(), "Loans Amount");
    AMNTFLD.setText(CurrencyComposition(AMNT));
    APR = validateNumber(APRFLD.getText(), "APR");
    TERM = validateNumber(TERMFLD.getText(), "Loans Term");
    double PAY = CalcPAY(AMNT, APR, TERM);
    PAYFLD.setText(CurrencyComposition(PAY));
    }

    catch (Exception t1) {
    PAYFLD.setText(null);
    };

    } else if (e.getActionCommand().equals(ResetBTN.getActionCom mand())) {
    AMNTFLD.setText(null);
    APRFLD.setText(null);
    TERMFLD.setText(null);
    PAYFLD.setText(null);
    }

    }


    // Calculates the monthly payment
    private double CalcPAY(double AMNT, double APR, double TERM) {

    double MAPR = (APR / 100) / 12; // Formula for J
    double MTERM = TERM 12; // Formula for N
    double PAY = AMNT (MAPR / (1 - Math.pow((1 MAPR), -TERM)));

    return PAY;
    }

    private double validateNumber(String numericString, String fieldName) throws Exception {

    double numberValue = 0;
    try {
    // make sure we have a number
    if (numericString.isEmpty())
    throw new Exception();

    // strip out currency symbol
    if (numericString.indexOf('$')> -1)
    {
    String newString = numericString.replace("$","");
    numericString = newString;
    }
    // transform string into double
    numberValue = NumberFormat.getNumberInstance().parse(numericStri ng).doubleValue();

    // no negative numbers
    if (numberValue < 0)
    throw new Exception();


    } catch (Exception e) {
    // Display Error Message in it's own pane
    JOptionPane.showMessageDialog(this, "Invalid value in " fieldName + ". \nPlease check your entry.", "Data Entry Error", JOptionPane.ERROR_MESSAGE);

    throw e;
    }


    return numberValue;
    }

    // Formats the numeric values to readable currency
    private String CurrencyComposition(double input) {

    DecimalFormat df = new DecimalFormat("$###,###,###.00");
    df.setMaximumFractionDigits(2);
    String formattedString = df.format(input);
    return formattedString;
    }
    }

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You never add the buttons to anything.

    By the way you should get into the habit of starting variable names with a lower case letter and using camelCase. ALLCAPS has a meaning that you don't intend.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

Similar Threads

  1. JButtons and If statements
    By calculuslord in forum New To Java
    Replies: 6
    Last Post: 10-20-2009, 11:52 PM
  2. Help with JButtons...
    By ashton in forum New To Java
    Replies: 8
    Last Post: 01-26-2009, 10:38 AM
  3. JButtons
    By jadaleus in forum Advanced Java
    Replies: 4
    Last Post: 10-17-2008, 03:49 AM
  4. 2D Array of JButtons
    By stevemcc in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 12:42 AM
  5. JButtons
    By fgasimzade in forum SWT / JFace
    Replies: 1
    Last Post: 12-25-2007, 06:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •