Java GUI program determining retail price, does not finish running

In summary, the conversation is about a programming assignment where the program runs but does not display the desired results. The code is provided and it includes a driver and class file. The class file contains a Retail Price Calculator class with a CalcButtonListener class for handling button clicks. The driver creates an instance of the class and the code is checked for any errors. It is mentioned that the JButton does not have any listeners and the conversation ends with a note to use code tags in future posts.
  • #1
opaquin
10
0

Homework Statement


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.

Driver:
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();

}
}
Class:
/*
* 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
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

//specify what happens when the close button is clicked
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//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
panel1.add(dealerCostLabel);
panel2.add(markupLabel);

//add textFields to panel
panel3.add(dealerCostTextField);
panel4.add(retailTextField);

//add buttons to panels
panel5.add(calcButton);

//add panel to frame's content pane
add(panel1);
add(panel2);
add(panel3);
add(panel4);
add(panel5);

//display window
setVisible(true);
}

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);
}
}

}
 
Physics news on Phys.org
  • #2
opaquin said:

Homework Statement


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.

Driver:
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();

}
}



Class:
/*
* 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
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

//specify what happens when the close button is clicked
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//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
panel1.add(dealerCostLabel);
panel2.add(markupLabel);

//add textFields to panel
panel3.add(dealerCostTextField);
panel4.add(retailTextField);

//add buttons to panels
panel5.add(calcButton);

//add panel to frame's content pane
add(panel1);
add(panel2);
add(panel3);
add(panel4);
add(panel5);

//display window
setVisible(true);
}

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);
}
}

}

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
 
  • #3
thank you! Not going to make that mistake again.

future code posts will have code tags, sorry 'bout that.
 

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

What could be causing my Java GUI program to not finish running?

There could be several reasons why your Java GUI program is not finishing running. Some common causes include coding errors, memory leaks, or compatibility issues with your device or operating system. It is also possible that the program is stuck in an infinite loop or experiencing a deadlock.

How can I determine the retail price in my Java GUI program?

The retail price in your Java GUI program can be determined by using logical calculations and inputting the necessary data, such as wholesale cost and markup percentage. You can also create a method or function specifically for calculating the retail price.

Is there a way to debug my Java GUI program to find the issue?

Yes, there are several debugging tools and techniques that can help you identify and fix any issues in your Java GUI program. These include using breakpoints, stepping through the code, and using logging statements to track the flow of the program. You can also use a debugger tool provided by your IDE.

My Java GUI program runs fine on my computer, but not on others. Why?

This could be due to compatibility issues with different devices or operating systems. Make sure you have tested your program on various platforms and devices to ensure it runs smoothly. It is also possible that there are differences in the Java version or libraries used on different devices.

How can I improve the performance of my Java GUI program?

To improve the performance of your Java GUI program, you can optimize your code by removing unnecessary loops or using more efficient algorithms. You can also use multithreading to run tasks simultaneously and reduce the overall execution time. Additionally, make sure to handle exceptions and errors properly to avoid slowing down the program.

Similar threads

Replies
1
Views
2K
Replies
1
Views
2K
Replies
9
Views
3K
Replies
4
Views
3K
Replies
2
Views
4K
Replies
2
Views
6K
Replies
2
Views
3K
Replies
4
Views
12K
Back
Top