1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Java GUI program determining retail price, does not finish running

  1. Dec 3, 2012 #1
    1. The problem statement, all variables and given/known data
    Just trying to finish up this assignment, no errors. I run the program, enter a value and a percentage, then click calculate. Program just runs, never displays results. Check my code below, let me know if something is wrong.

    package ch7pc1;
    import javax.swing.*;

    public class CH7PC1

    * @param args the command line arguments
    public static void main(String[] args)
    //create instance of class
    RetailPriceCalculator rpc = new RetailPriceCalculator();


    * Retail Price Calculator Class
    package ch7pc1;
    import java.awt.GridLayout;
    import javax.swing.*;
    import java.awt.event.*;

    class RetailPriceCalculator extends JFrame
    private final int WINDOW_WIDTH = 350;
    private final int WINDOW_HEIGHT = 250;
    //create two textFields fields
    JTextField dealerCostTextField = new JTextField (10);
    JTextField retailTextField = new JTextField (10);

    public RetailPriceCalculator()
    //set window title
    setTitle("Retail Price Calculator");

    //set size of window

    //specify what happens when the close button is clicked

    //add GridLayout manager to content pane
    setLayout(new GridLayout(3,2));

    //create two labels
    JLabel dealerCostLabel = new JLabel("Dealer cost $");
    JLabel markupLabel = new JLabel("Markup Percentage");

    //create one button
    JButton calcButton = new JButton("Calculate");

    //create panels
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JPanel panel3 = new JPanel();
    JPanel panel4 = new JPanel();
    JPanel panel5 = new JPanel();
    JPanel panel6 = new JPanel();

    //add labels to panel

    //add textFields to panel

    //add buttons to panels

    //add panel to frame's content pane

    //display window

    private class CalcButtonListener implements ActionListener
    public void actionPerformed(ActionEvent e)
    String input1, input2; //hold user's input
    double amount, convert; //cost of item

    //get text enter by user in text field
    input1 = dealerCostTextField.getText();

    //get text enter by user in text field
    input2 = retailTextField.getText();

    //convert input2 to percent
    convert = (Double.parseDouble(input2) *.01)+1;

    //convert input to retail price
    amount = Double.parseDouble(input1)*convert;

    //display the result
    JOptionPane.showMessageDialog(null, "The retail price is $" + amount);

  2. jcsd
  3. Dec 3, 2012 #2


    User Avatar
    Homework Helper
    Gold Member

    How does your JButton know to call the ActionPerformed method of CalcButtonListener when you haven't added any listeners to calcButton? :wink:

    Aside: please use code tags to wrap your in future posts, it preserves indentations and makes it much easier to read
  4. Dec 3, 2012 #3
    thank you! Not gonna make that mistake again.

    future code posts will have code tags, sorry 'bout that.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook